WMI Service Query

The following script queries all servers in Active Directory and then displays service logon information through WMI. This can help plan for service account migration and password changes.

strNamingContext = "defaultNamingContext"
strConnectionString = "Provider=ADsDSOObject"
strFilter= "ObjectCategory='computer' and OperatingSystem='Windows*Server*'"

'[Functions] ****************************************
Function removeCN(inputString)
  Dim textValue
     textValue = inputString
 Dim textLength
     textLength = Len(textValue)
    textValue = Right(textValue, textLength-3)
     removeCN = textValue
End Function
'****************************************************

Set objAdRootDSE = GetObject("LDAP://RootDSE")
Set objRS = CreateObject("ADODB.RecordSet")
 objNamingContext = objAdRootDSE.Get(strNamingContext)
 strSQLQuery = "SELECT * FROM ‘LDAP://" & objNamingContext &_
        "' WHERE " &strFilter

objRS.Open strSQLQuery, strConnectionString

On Error Resume Next

Do until objRS.eof
 Set objServer = GetObject(objRS.Fields.Item(0))
 strComputer = removeCN(objServer.Name)
 Set objWMIService = GetObject("winmgmts:\" & strComputer &"\root\cimv2")
  If Err.Number <> 0 Then
   WScript.Echo strComputer &", ERROR Connecting to Object, " & Err.Description
  End if
 Set colItems = objWMIService.ExecQuery("Select * from Win32_Service")
 For Each objItem in colItems
  WScript.Echo strComputer &"," &objItem.Name &"," & objItem.StartName
 Next
objRS.MoveNext
Loop

Powered by WordPress with [GimpStyle]
Entries and comments feeds.