Active Directory: VBscript to enable oma (outlook mobile access) from an excel sheet

Posted April 6th, 2010 in outlookmobileaccess by dirk adamsky

This script enables oma (outlook mobile access) based on a list of smtp addresses in an excel sheet.

What the script does:

  • get the users smtp address from the excel sheet (c:\tempoma.xls)
  • the function FindDN finds the corresponding distinguished name of the user object
  • the function EnableOma enables oma for the user
  • the function LogPrint creates logging for all actions

Follow the next steps to run the script (admin rights needed):

  • create an excel sheet with a list of smtp addresses to be oma enabled
  • save the sheet as c:tempoma.xls
  • copy and paste the script in your favorite text editor
  • save the script (for example c:tempenableoma.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript enableoma.vbs” (without quotes) and enter

The script:

' Name : enableoma.vbs
' Description : script to enable oma (outlook mobile access) from an excel sheet
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 06-04-2010

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\tempoma.xls")
intRow = 2
Do Until objExcel.Cells(intRow,1).Value = ""
    strSmtpAddress = objExcel.Cells(intRow, 1).Value
    If strSmtpAddress <> "" Then
		strDN = FindDN(strSmtpAddress)
		EnableOma strDN
		Logprint "outlook mobile access is enabled for ; " & strSmtpAddress
	End If
	intRow = intRow + 1
Loop
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing

Function EnableOma(strAccount)
	Set objUser = GetObject ("LDAP://" & strAccount)
	objUser.Put "msExchOmaAdminWirelessEnable", "0"
	objUser.setinfo
	Set objUser = Nothing
End Function

Function LogPrint(Message)
Const ForAppending = 8
strDate = Replace(Date,"/","-")
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = ObjFSO.OpenTextFile(strDate & "-OmaEnabledAgain.csv", ForAppending, True)
    objTextFile.WriteLine Message
    objTextFile.Close
Set objTextFile = Nothing
Set ObjFSO = Nothing
End Function

Function FindDN(strSmtp)

	Set adoCommand = CreateObject("ADODB.Command")
	Set adoConnection = CreateObject("ADODB.Connection")
	adoConnection.Provider = "ADsDSOObject"
	adoConnection.Open "Active Directory Provider"
	adoCommand.ActiveConnection = adoConnection

	Set objRootDSE = GetObject("LDAP://RootDSE")
	strBase = "<LDAP://" & objRootDSE.Get("defaultNamingContext") & ">"

	' Filter on user objects.
	strFilter = "(mail=" & strSmtp & ")"

	' Comma delimited list of attribute values to retrieve.
	strAttributes = "distinguishedName"

	' Construct the LDAP syntax query.
	strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
	adoCommand.CommandText = strQuery
	adoCommand.Properties("Page Size") = 100
	adoCommand.Properties("Timeout") = 30
	adoCommand.Properties("Cache Results") = False

	' Run the query.
	Set adoRecordset = adoCommand.Execute
	FindDN = adoRecordset.Fields("distinguishedName").Value

	' Clean up.
	adoRecordset.Close
	adoConnection.Close

	Set objRootDSE = Nothing
	Set adoConnection = Nothing
	Set adoCommand = Nothing
End Function

When you have problems/questions please post a reply, you can also rate the script.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV

 

WMI: VBscript to ping servers from an excel sheet

Posted January 22nd, 2010 in ping by dirk adamsky

This script pings a list a list of servers from an excel sheet.

What the script does:

  • get the server hostname from the excel sheet (c:tempservers.xls)
  • the function CheckStatus does the wmi ping and returns true or false
  • output is the hostname and the ping status

Follow the next steps to run the script (no admin rights needed):

  • create an excel sheet with a list of hostnames
  • save the sheet as c:tempservers.xls
  • copy and paste the script in your favorite text editor
  • save the script (for example c:tempalive.vbs)
  • open a command prompt
  • go to “c:temp”
  • give “cscript alive.vbs” (without quotes) and enter
  • output is the hostnames and status

The script:

' Name : alive.vbs
' Description : script to ping servers from an excel sheet
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 22-01-2010

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:tempservers.xls")
intRow = 2
Do Until objExcel.Cells(intRow,1).Value = ""
    strHostname = objExcel.Cells(intRow, 2).Value
    If strHostname <> "" Then
		If CheckStatus(strHostname) = True Then
			Wscript.Echo strHostname & " ; is alive"
		Else
			Wscript.Echo strHostname & " ; is dead"
		End If
	End If
	intRow = intRow + 1
Loop
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing

Function CheckStatus(strAddress)
	Dim objPing, objRetStatus
	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strAddress & "'")
	For Each objRetStatus In objPing
        If IsNull(objRetStatus.StatusCode) Or objRetStatus.StatusCode <> 0 Then
			CheckStatus = False
        Else
			CheckStatus = True
        End If
    Next
	Set objPing = Nothing
End Function

Happy scripting.

Best regards,

dirk adamsky