Re: WMI & ADSI

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Torgeir Bakken \(MVP\) (Torgeir.Bakken-spam_at_hydro.com)
Date: 08/04/04


Date: Thu, 05 Aug 2004 01:22:16 +0200

poul0004 wrote:
> I'm trying to query ADSI to get a listing of all computer objects then use
> WMI to query the domain role of each computer object.
>
> I'm using ADO to extract the computers using the following query:
>
> aCmd.CommandText =
> "<LDAP://DOMAINNAMEREMOVED>;(objectClass=computer);cn,name;subtree"
>
> I then loop through the recordset that receives the results of calling
> execute on the command and try the following:
>
> do while not arst.eof
> Set objSystemSet = GetObject("winmgmts:{impersonationLevel=impersonate}!\\"
> & aRst.Fields(0)).InstancesOf ("Win32_ComputerSystem")
> sServerName = System.Name
> sDomainRole = System.DomainRole
>
> However, objObjectSet is always empty. No computer names get returned and 0
> is returned for the domain role.
>
> I have modified the script a bit and gotten the results I need, but it the
> only way I have gotten it to work is to hard code the OU names such as:
>
> Set ADSObject = GetObject("LDAP://CN=" & strcomputer & ",OU=PATH_REMOVED")
>
> Set WMIObject = ADSObject.GetWMIObject
>
> sServerName = wmiobject.caption
> sDomainRole = wmiobject.domainrole
>
> However, this misses the point of having the script being able to find all
> the computer accounts automatically without intervention.
Hi

No need for hard coding in your latter method, the distinguishedName
attribute will give you what you want.

Change aCmd.CommandText to:

aCmd.CommandText =
  "<LDAP://DOMAINNAMEREMOVED>;(objectClass=computer);distinguishedName;subtree"

Then, in the loop, you can do like this:

do while not aRst.eof

    Set ADSObject = GetObject("LDAP://" & aRst.Fields("distinguishedName"))
    Set WMIObject = ADSObject.GetWMIObject
    sServerName = WMIObject.caption
    sDomainRole = WMIObject.domainrole

    'snip code

loop

-- 
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx


Relevant Pages

  • Re: security center
    ... but you can query the PC's via WMI (Windows Management Instrumentation) scripting. ... The below script is for the local computer, and would need to be modified to determine all PC's on your network/domain and then query each computer individually and report when there is a problem. ...
    (microsoft.public.windowsxp.security_admin)
  • Re: Sort results from ExchangeV2
    ... the mailbox. ... I can get the query to return only mailboxes larger than the set size, but if I request the collection sorted it becomes empty. ... SQL for WMI ... torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: http://www.microsoft.com/technet/scriptcenter/default.mspx. ...
    (microsoft.public.scripting.vbscript)
  • Re: Order By in Query
    ... of WMI for Exchange ... Exchange_Mailbox Order By MailboxDisplayName", ... I get an error for the above query ... torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.win32.programmer.wmi)
  • Re: WMI Problem
    ... So while I am banging my head on WMI, ... > there a way to query the domain for all machines currently logged on, ... > better yet all machines whose names start with 'WKS_' that arelogged on? ... -- torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway Administration scripting examples and an ONLINE version of the 1328 page Scripting Guide: ...
    (microsoft.public.scripting.wsh)
  • Re: ExecQuery hangs whole script but GetObject is fine
    ... problems/errors with XP SP2 if using WMI. ... How to troubleshoot WMI-related issues in Windows XP SP2 ... > executing the query and the query hangs and thus the whole script ... > of the DCs as a domain admin but in the scheduled task that the script ...
    (microsoft.public.win32.programmer.wmi)