Re: Modifiy this script to read a nested "Computers" container

From: John (no email)
Date: 04/08/04


Date: Thu, 08 Apr 2004 22:20:18 +0100


Hi Richard.. I ran the below script and received the following error.
There are computer accounts in both the top level OU called Site1 and
the child OU called computers. Any suggestions on the null error?
Thanks Richard for your help.

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\get local admin.vbs(45, 1) (null): 0x80041010

Exit code: 0 , 0000h

Set Dom = GetObject("LDAP://ou=Computers,ou=site1,dc=xyz,dc=com")
Dom.Filter = Array("Computer")
strLogFile = "LocalAdmins.LOG"
strLocalAdminGroup = "Administrators"
Set objFSO = CreateObject("Scripting.fileSystemObject")

on error resume next
Set objLogFile = objFSO.CreateTextFile(strLogFile, NO_OVERWRITE)

If Err.Number <> 0 Then
WScript.Echo "Cannot create " & strLogFile & " -OR- " _
    & strLogFile & "already exists."
  WScript.Quit
End If
On Error GoTo 0

objLogFile.WriteLine("**************** Started at " & Now() _
  & "****************")
objLogFile.WriteLine()

For Each Member in Dom
  strComputer = Member.sAMAccountName
  Call GetLocalAdmins(strComputer)
Next

objLogFile.WriteLine()
objLogFile.WriteLine("**************** Finished at " & Now() _
  & "****************")
objLogFile.Close

'-=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=-
' SUB GetLocalAdmins
' purpose: Retrieve list of local administrators from online machines
' input: strComputer (a machine name)
' output: results are echoed on screen and saved in strLogFile
' notes: uses Win32_PingStatus class ("strPinger" must be XP or 2003)
'-=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=--=[]=-
Sub GetLocalAdmins(HostName)
strPinger = "."
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate}!\\" & strPinger & "\root\cimv2")
Set colPingedComputers = objWMIService.ExecQuery ("Select * from
Win32_PingStatus Where Address = '" & strComputer & "'")

objLogFile.WriteLine("-----------------------------------------------------")
For each objComputer in colPingedComputers
If objComputer.StatusCode = 0 Then
WScript.Echo "Processing " & strComputer
objLogFile.WriteLine(strComputer)
Set objLocalAdminGroup = GetObject("WinNT://" & strComputer & "/" &
strLocalAdminGroup)
For Each objLocalAdmin In objLocalAdminGroup.Members
objLogFile.WriteLine("Member of " & strLocalAdminGroup & " group: " &
objLocalAdmin.Name)
Next
Set objLocalAdminGroup = Nothing
Else
WScript.Echo strComputer & " Offline"
objLogFile.WriteLine(strComputer & " Offline")
End If
Next
End Sub
 

On Mon, 5 Apr 2004 23:47:15 -0500, "Richard Mueller [MVP]"
<rlmueller-NOSPAM@ameritech.NOSPAM.net> wrote:

>Yes, the Distinguished Name you gave should make the code search the
>"ou=site1" ou and all sub (child) containers and OU's. However, since it
>seems you only want to search one OU, you could set the base of the search
>to be "ou=Computers,ou=site1" and forget about a recursive subroutine. For
>example:
>
>Set Dom = GetObject("LDAP://ou=Computers,ou=Site1,dc=xyz,dc=com")
>Dom.Filter = Array("computer")
>
>For Each Member in Dom
> strComputer = Member.sAMAccountName
> Call GetLocalAdmins(strComputer)
>Next
>
>In other words, your original code should work if you just use LDAP,
>substitute the Distinguished Name of the ou=Computers,ou=Site1 OU, and
>substitute "Member.sAMAccountName" for "Member.Name". Also, you had
>Array("computers") where it should be Array("computer"). (I haven't checked
>Sub GetLocalAdmins.)
>
>Note, that in the Recursive sub I posted earlier, where I used
>"Member.Name", it should have been "Member.sAMAccountName". The attribute
>name is different when using the LDAP provider and I missed that
>modification. I hope this helps.
>
>--
>Richard
>Microsoft MVP Scripting and ADSI
>HilltopLab web site - http://www.rlmueller.net



Relevant Pages

  • Re: Modifiy this script to read a nested "Computers" container
    ... I ran the below script and received the following error. ... >There are computer accounts in both the top level OU called Site1 and ... >Thanks Richard for your help. ... >Sub GetLocalAdmins ...
    (microsoft.public.scripting.wsh)
  • Help listing all groups and users
    ... Hi I have a script that list all users and nested groups for the admin ... Dim objGroup, strComputer ... Sub EnumGroup ... Dim objMember ...
    (microsoft.public.scripting.vbscript)
  • Modifiy this script to read a nested "Computers" container
    ... I don’t remember where I got the script from so I can’t give the guy ... Array("Computers") <- array targets the default OU Computers at the ... ' input: strComputer ... Sub GetLocalAdmins ...
    (microsoft.public.scripting.wsh)
  • PowerStatus
    ... I have the below script, but sometimes it can take up ... to 4-5 minutes to try and connect to the remote computer if it's not ... IsComputerOn strComputer ... Sub IsComputerOn ...
    (microsoft.public.scripting.wsh)
  • Add DL group to local admins for each computer in OU
    ... I have been trying to modify this script: ... On Error Goto 0 ... Sub EnumComputers ...
    (microsoft.public.windows.server.scripting)

Loading