Monday, June 30, 2008

Cool Script

I can't script worth a crap and usually scour the web for scripts that do great things. I recently needed to find out which users in our domain had local admin rights on their workstations.

While searching, I came across this script written by Brian Desmond. The script will read your from a text file (that you create..another script) all the pc's in your domain(c:\pclist.txt), test for connectivity and output the data to a log and csv file. It's pretty cool. If you don't want the script to bomb, simply put an "ON ERROR RESUME NEXT" below "Option Explicit". Just be sure to go back and check the devices it missed. You can also edit the file to enumerate Power Users or any other group. Thanks Brian.

'==========================================================================
' NAME: Dump Local Administrators Membership
'
' AUTHOR: Brian Desmond,
' DATE : 4/16/2007
'==========================================================================




Option Explicit

Const LogFile = "LocalAdmins.log"
Const resultFile = "LocalAdministratorsMembership.csv"
Const inputFile = "C:\PClist.txt"


Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim shl
Set shl = WScript.CreateObject("WScript.Shell")

Dim fil
Set fil = fso.OpenTextFile(inputFile)

Dim results
Set results = fso.CreateTextFile(resultFile, True)

WriteToLog "Beginning Pass of " & inputFile & " at " & Now()
WScript.Echo "Beginning Pass of " & inputFile & " at " & Now()
'On Error Resume Next

Dim grp
Dim line
Dim exec
Dim pingResults
Dim member

While Not fil.AtEndOfStream
line = fil.ReadLine

Set exec = shl.Exec("ping -n 2 -w 1000 " & line)
pingResults = LCase(exec.StdOut.ReadAll)

If InStr(pingResults, "reply from") Then
WriteToLog line & " responded to ping"


'On Error Resume Next

Set grp = GetObject("WinNT://" & line & "/Administrators")


results.WriteLine line & ",Administrators,"

For Each member In grp.Members

WriteToLog line & ": Administrators - " & member.Name
results.WriteLine ",," & member.Name
Next
Else
WriteToLog line & " did not respond to ping"

End If
Wend

results.Close

Sub WriteToLog(LogData)
On Error Resume Next

Dim fil
'8 = ForAppending
Set fil = fso.OpenTextFile(LogFile, 8, True)

fil.WriteLine(LogData)

fil.Close
Set fil = Nothing
End Sub

No comments: