Re: WMI & ADSI

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