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)" & _ 

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) 

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) 
If Err.Number <> 0 Then 
    fileHandle.WriteLine (strComputer & ",Error: " & Err.Number & ",Error (Hex): " & Hex(Err.Number) & ",Source: " &  Err.Source & ",Description: " &  Err.Description) 
End If 

set strComputer = Nothing 
set colServices = Nothing 
set objWMIService = Nothing 


Set fileHandle = Nothing 
Set fileSysObj = Nothing 

Wscript.Echo "Completed"
4 Years
Discussion Span
Last Post by chriswelborn

This is really old, and I'm no expert on WMI, but I can provide at least a little insight.

It grabs the domain name for the current machine from Active Directory.

Creates a comma-separated value (csv) file like: 'domain_services_info.csv'

It uses WMI (Windows Management Instrumentation) to query a list of services available on the machine.

It then loops over those Service objects writing a new line to the csv file for each one. "computer, service, username"

If it errors, the error will be written to the end of the file.

The file is then closed, "Completed" is written to the terminal, and the script exits.

I have never personally had to write such a script, but from what I can tell it is harmless. It creates a csv file (text file), but other than that all it is doing is querying things. So I don't think any harm can be done.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.