Hi.
I have here a script which I had acquired from the net. I have tested it in a lab environment and it seems to accomplish my task perfectly. That being- to list each server in the domain, its services and the account the service runs under.
Problem is- I don't want to run this on our production network without understanding the code. Could somebody walk through this code to help me understand? I would really appreciate it.
strActiveDirectoryDomainDNSName = "example.domain.com"
strBase = "<LDAP://" & strActiveDirectoryDomainDNSName & ">;"
strFilter = "(&(objectclass=computer)(objectcategory=computer)" & _
"(operatingSystem=*Server*));"
strAttrs = "cn;"
strScope = "subtree"
set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
Wscript.Echo strBase & strFilter & strAttrs & strScope
Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
Set fileSysObj = CreateObject("Scripting.FileSystemObject")
Set fileHandle = fileSysObj.CreateTextFile(strActiveDirectoryDomainDNSName & "_services_info.csv", True)
objRS.MoveFirst
while Not objRS.EOF
strComputer=lcase (objRS.Fields(0).Value) & "." & strActiveDirectoryDomainDNSName
On Error Resume Next
Set objWMIService = GetObject _
("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery _
("Select * From Win32_Service")
For Each objService in colServices
fileHandle.WriteLine(strComputer & "," & objService.Name & "," & objService.StartName)
Next
If Err.Number <> 0 Then
fileHandle.WriteLine (strComputer & ",Error: " & Err.Number & ",Error (Hex): " & Hex(Err.Number) & ",Source: " & Err.Source & ",Description: " & Err.Description)
Err.Clear
End If
objRS.MoveNext
set strComputer = Nothing
set colServices = Nothing
set objWMIService = Nothing
wend
fileHandle.Close
Set fileHandle = Nothing
Set fileSysObj = Nothing
Wscript.Echo "Completed"