A very frequent requirement for every administrator to list members of Administrators group of all computers (servers and desktops) in the domain. The script below can be used to run to get a csv file with a list of all computers in the domain and the members of Administrators local group. The type of member (User, Computer or Group) will be provided.
The output of the script is saved in file "AdminMembers.csv" file at the same path of script. The script also uses a temporary file "c:\RunResult.tmp" to store ping results to verify if the computer is online or not. Both can be modified in the script to values of your choice.
'---------Script Start-------------
on error resume next
Const ForReading = 1
const ForWriting = 2
const ForAppending = 8
Set objShell = CreateObject("Wscript.Shell")
strTempFile = "c:\RunResult.tmp"
Set oFS = CreateObject("Scripting.FileSystemObject")Set ws = CreateObject ("Scripting.FileSystemObject")
Set w = ws.OpenTextFile (".\AdminMembers.csv", ForAppending, True)
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strQuery = "<LDAP://" & strDNSDomain & ">;(&(sAMAccountType=805306369)(objectCategory=computer(!useraccountcontrol:1.2.840.113556.1.4.803:=2));cn;subtree"
objCommand.CommandText = strQuery
Set RS = objCommand.Execute
Wscript.echo RS.RecordCount & " Computers found"
w.WriteLine "Information Collection started at " & now
x = 1
While not RS.EOF
sComputer = RS.Fields("CN")
If IsConnectible(sComputer, 1, 750) Then
set Group = GetObject("WinNT://" + sComputer + "/Administrators,group")
For Each Member in Group.Members
mem = Member.adspath
w.writeline sComputer & "," & right(mem,len(Member.adspath)-8) & "," & Member.class
Next
WScript.Echo x & " " & sComputer & " Completed"
Else
WScript.Echo x & " " & sComputer & " not accessible"
End If
RS.MoveNext
x = x+1
Wend
wscript.echo "Done!"
Function IsConnectible(strHost, intPings, intTO)
' Returns True if strHost can be pinged.
Dim objFile, strResults
If intPings = "" Then intPings = 2
If intTO = "" Then intTO = 750
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True
Set objFile = oFS.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close
Select Case InStr(strResults, "TTL=")
Case 0
IsConnectible = False
Case Else
IsConnectible = True
End Select
End Function
'---------Script Start-------------
The output of the script is saved in file "AdminMembers.csv" file at the same path of script. The script also uses a temporary file "c:\RunResult.tmp" to store ping results to verify if the computer is online or not. Both can be modified in the script to values of your choice.
'---------Script Start-------------
on error resume next
Const ForReading = 1
const ForWriting = 2
const ForAppending = 8
Set objShell = CreateObject("Wscript.Shell")
strTempFile = "c:\RunResult.tmp"
Set oFS = CreateObject("Scripting.FileSystemObject")Set ws = CreateObject ("Scripting.FileSystemObject")
Set w = ws.OpenTextFile (".\AdminMembers.csv", ForAppending, True)
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strQuery = "<LDAP://" & strDNSDomain & ">;(&(sAMAccountType=805306369)(objectCategory=computer(!useraccountcontrol:1.2.840.113556.1.4.803:=2));cn;subtree"
objCommand.CommandText = strQuery
Set RS = objCommand.Execute
Wscript.echo RS.RecordCount & " Computers found"
w.WriteLine "Information Collection started at " & now
x = 1
While not RS.EOF
sComputer = RS.Fields("CN")
If IsConnectible(sComputer, 1, 750) Then
set Group = GetObject("WinNT://" + sComputer + "/Administrators,group")
For Each Member in Group.Members
mem = Member.adspath
w.writeline sComputer & "," & right(mem,len(Member.adspath)-8) & "," & Member.class
Next
WScript.Echo x & " " & sComputer & " Completed"
Else
WScript.Echo x & " " & sComputer & " not accessible"
End If
RS.MoveNext
x = x+1
Wend
wscript.echo "Done!"
Function IsConnectible(strHost, intPings, intTO)
' Returns True if strHost can be pinged.
Dim objFile, strResults
If intPings = "" Then intPings = 2
If intTO = "" Then intTO = 750
Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1
objShell.Run "%comspec% /c ping -n " & intPings & " -w " & intTO _
& " " & strHost & ">" & strTempFile, 0, True
Set objFile = oFS.OpenTextFile(strTempFile, ForReading, _
FailIfNotExist, OpenAsDefault)
strResults = objFile.ReadAll
objFile.Close
Select Case InStr(strResults, "TTL=")
Case 0
IsConnectible = False
Case Else
IsConnectible = True
End Select
End Function
'---------Script Start-------------
No comments:
Post a Comment