Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to: List members of the "Local Administrator Group

Status
Not open for further replies.

Jaws25

IS-IT--Management
Joined
Sep 4, 2003
Messages
11
Location
US
I have a script that ping the wks in a particular OU and put them into a .txt file if successful. I have called up the successful.txt file and put it into an array. I then want to use that array of wks names to list all members of the local admin group.

If someone has a suggestion it would be greatly appreciated.

thanks,
Jaws
 
Hello Jaws25,

Which part poses a difficulty? or just want to prettier presentation writing to excel or whatever?

Run the loop with each element of the array within which stores the computer's netbios name. With the computer name, bind to the (local) administrators group "WinNT://computername/administrators,group" and then use the .members collection to retrieve all members' objects. Their .name will give you their login samaccountname.

regards - tsuji
 
Here is the script that I have been working on below. It pings systems based off of the container.txt file OU list(I chose one with only a few systems to save time). Successful pings are logged to pingsuccessful.txt and then loaded into an array. When loading the systems names into the array each system is check for members of the local administrators group. For some reason it errors out when trying to get the members off the second system. Error message is:

C:\Share\AD\admins.vbs(86, 1) (null): The network path was not found.

Any help would be greatly appreciated. Thanks!!!!

'========
'= Main =
'========
Dim Computer, elem
PingSystems
MakeArray
MsgBox "Listing Local Admins is now complete"

'================
'= Ping Systems =
'================
Sub PingSystems()
Dim Outputfile, oFSO, OFile, Outputfile2, oFSO2, oFile2
Dim iFSO, iFile, objContainer, StdOut, objShell

OutputFile="C:\share\ad\pingSuccessful.txt"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSo.CreateTextFile(OutputFile, True)
OutputFile2="C:\share\ad\pingFailed.txt"
Set oFSO2 = CreateObject("Scripting.FileSystemObject")
Set oFile2 = oFSO.CreateTextFile(OutputFile2, True)

Set iFSO = CreateObject("Scripting.FilesyStemObject")
InputFile="c:\share\ad\container.txt" 'contains OU structure

Set ifile = iFSO.OpenTextFile(inputfile)

WScript.Echo "Pinging Systems Now..."
Do until ifile.AtEndOfLine
strComputerContainer = ifile.ReadLine

Set objContainer = GetObject("LDAP://" & strComputerContainer)
objContainer.Filter = Array("Computer")

Set StdOut = WScript.StdOut
Set objShell = CreateObject("WScript.Shell")

On Error Resume Next

For Each objComputer In objContainer
Computer = Split(objComputer.Name, "=")(1)
Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & Computer)
strPingResults = LCase(objScriptExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
oFile.WriteLine(Computer)
Else
oFile2.WriteLine(Computer)
End If
Next
Loop
WScript.Echo "Pinging Systems Complete!"
End Sub

'========================
'= Opens TXT into array =
'========================
Sub MakeArray
Const ForReading = 1
Const WKS = "c:\share\ad\pingsuccessful.txt"
Dim fso, f
Dim arrText()
Dim text

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(WKS, ForReading)

iUpperBound = 0
While Not f.AtEndOfStream
ReDim Preserve arrText(iUpperBound)
arrText(UBound(arrText)) = f.ReadLine
iUpperBound = iUpperBound + 1
Computer = arrText(UBound(arrText))
WScript.Echo Computer
GetLocalAdmins 'calls sub to get local admin info
Wend
f.Close
End Sub

'=====================
'= Gets Local Admins =
'=====================
Sub GetLocalAdmins
Dim objComp
strComputer = computer
Set objComp = GetObject("WinNT://" & strComputer) 'seems to have issues here.
objComp.GetInfo 'or here....
If objComp.PropertyCount > 0 Then
Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
If objGroup.PropertyCount > 0 Then
WScript.Echo "The members of the local Administrators group on " & strComputer & " are:"
For Each mem In objGroup.Members
WScript.echo vbTab & Right(mem.adsPath,Len(mem.adsPath) - 8)
Next
Else
WScript.echo "** Connecting to the local Administrators group on " & strComputer & " failed."
WScript.Quit 1
End If
Else
WScript.Echo "** Connecting to " & strComputer & " failed."
WScript.Quit 1
End If
End Sub
 
Jaw25,

First a revision of pingSystems.
Code:
'================
'= Ping Systems =
'================
Sub PingSystems()
Dim Outputfile, FSO, OFile, Outputfile2, oFile2
Dim iFile, objContainer, StdOut, objShell    
    
    OutputFile="C:\share\ad\pingSuccessful.txt" 
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set oFile = FSO.CreateTextFile(OutputFile, True)
    OutputFile2="C:\share\ad\pingFailed.txt"
    Set oFile2 = FSO.CreateTextFile(OutputFile2, True)
    
    InputFile="c:\share\ad\container.txt" 'contains OU structure
    
    Set iFile = FSO.OpenTextFile(InputFile, 1, true)  'assume existing else create an empty file
    
    WScript.Echo "Pinging Systems Now..."
    Do until iFile.AtEndOf[red]Stream[/red]
    strComputerContainer = iFile.ReadLine
    
    Set objContainer = GetObject("LDAP://" & strComputerContainer)
    objContainer.Filter = Array("Computer")
     
    Set StdOut = WScript.StdOut
    Set objShell = CreateObject("WScript.Shell")
    
    On Error Resume Next
     
    For Each objComputer In objContainer
      Computer = Split(objComputer.Name, "=")(1)
      Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & Computer)
      do while objScriptExec.status=0
          wscript.sleep 100
      loop
      strPingResults = LCase(objScriptExec.StdOut.ReadAll)
      If InStr(strPingResults, "reply from")[red]<>0[/red] Then
        oFile.WriteLine(Computer)
      Else
       oFile2.WriteLine(Computer)
      End If
    Next
    Loop
    [red]iFile.close
    oFile2.close
    oFile.close[/red]
    WScript.Echo "Pinging Systems Complete!"
End Sub
Your instr() is doing the opposite you are intended to do.

Try the revision first and feedback.

- tsuji
 
Correction:

Maybe not the opposition! So I take back that line.

- tsuji
 
Jaw25,

Here a revision for the rest.
Code:
'========================
'= Opens TXT into array =
'========================
Sub MakeArray
Const ForReading = 1
Const WKS = "c:\share\ad\pingsuccessful.txt"
Dim fso, f
Dim arrText()
Dim text

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(WKS, ForReading)

iUpperBound = 0
While Not f.AtEndOfStream
   ReDim Preserve arrText(iUpperBound)
   arrText(UBound(arrText)) = f.ReadLine
   iUpperBound = iUpperBound + 1
   Computer = arrText(UBound(arrText))
   WScript.Echo Computer
   if not isempty(Computer) then
      GetLocalAdmins [blue]Computer[/blue] 'calls sub to get local admin info - pass computer as variable explicitly
   end if
Wend 
f.Close
End Sub

'=====================
'= Gets Local Admins =
'=====================
Sub GetLocalAdmins [blue](Computer)[/blue]
Dim objComp
strComputer = Computer 
Set objComp = GetObject("WinNT://" & strComputer) 'seems to have issues here.
objComp.GetInfo  'or here....
If objComp.PropertyCount > 0 Then
    Set objGroup = GetObject("WinNT://" & strComputer & "/Administrators,group")
    If objGroup.PropertyCount > 0 Then
        WScript.Echo  "The members of the local Administrators group on " & strComputer & " are:"
        For Each mem In objGroup.Members
            WScript.echo vbTab & Right(mem.adsPath,Len(mem.adsPath) - 8)
        Next
    Else
        WScript.echo "** Connecting to the local Administrators group on " & strComputer & " failed."
        WScript.Quit 1
    End If
Else
    WScript.Echo  "** Connecting to " & strComputer & " failed."
    WScript.Quit 1
End If
End Sub
I would prefer to pass computer as variable explicitly to avoid problem with such a common name and enforce its limited scope. Also, check computer not being empty before passing just in case of empty line.

- tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top