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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI Active Directory Query

Status
Not open for further replies.

wotsit

MIS
Oct 18, 2002
47
0
0
GB
Hi,

I have a wmi query that won't work and i just don't know why. I'm completely stuck and i'm not sure what to do. I am very new to scripting unfortunately and i'm trying to learn.

I have a script which runs wmi queries to servers which works successfully. It reads from a text file containing ip addresses and then writes the collected information to a .csv file. An example of a query is below:

set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem In colItems
StrComputerName= objItem.Caption
StrManufacturer = objItem.Manufacturer
strModel = objItem.Model
strCaption2 = objItem.Caption
strSystemType = objItem.SystemType
strDomain = objItem.Domain
strOwner = objItem.PrimaryOwnerName
strName = objItem.Name
next

All the values eg, strName, strDomain have been declared in a Dim statement.

The tricky bit which has me confused is that for these servers, i need to get some of their information from Active Directory as well. The information i need is in the wmi namespace called root\directory\LDAP and the wmi class is called ds_computer.

I amended my original script with the appropriate name space and class, but nothing is written to the.csv file apart from the server's ip address. The amended bit is

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery("SELECT * FROM ds_computer",,48)
For Each objItem In colItems
strServerDescription = strDS_description
next


Can anybody see where i am going wrong and point me in the right direction please?

Thank you,
H
 
What is supposed to be strDS_description ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you for your responding so quickly :)

strDS_description is the description field of the computer object in active directory. For example, one severs details should be:

DS_description Network Test Server
DS_displayName NETTEST01$
DS_memberOf Domain Controllers

DS_displayName is the server name and DS_memberOf should detail the security groups the server belongs to.

Any ideas?
 
Have you tried this ?
strServerDescription = objItem.DS_description

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you. I've just tried i but i get the same result. Only this appears in the .csv file:

192.168.70.18;;;;

192.168.70.19;;;;

192.168.70.24;;;;

192.168.70.25;;;;

192.168.70.26;;;;

Is there anything else i can try?

Thanks.
 
add a msgbox colItems.count after setting colItems to see if anything is returned.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I don't know if this will help, but the code below returns ALL computer names and descriptions for all computers in my domain (note that the script is only executing on the local computer, but yet displays all domain computer information). At least this may help you understand how this query works, and if it will work for you.

Below is snipped from a script in the technet scriptomatic tool. Save as a .vbs and run cscript from a command prompt:
Code:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

arrComputers = Array(".")
For Each strComputer In arrComputers
   WScript.Echo
   WScript.Echo "=========================================="
   WScript.Echo "Computer: " & strComputer
   WScript.Echo "=========================================="

   Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
   Set colItems = objWMIService.ExecQuery("SELECT * FROM ds_computer", "WQL", _
                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)

   For Each objItem In colItems
      WScript.Echo "DS_name: " & objItem.DS_name
      strDS_description = Join(objItem.DS_description, ",")
         WScript.Echo "DS_description: " & strDS_description
      WScript.Echo
   Next
Next
 
OK, we've to deal with an array ...
strServerDescription = Join(objItem.DS_description, "")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thank you all. PHV - I've added the line that you suggested, but it hasn't made any difference. I'm obviously doing something wrong, but can't see what. I'll paste the whole code below, can you see anything obvious?

Thank you,
H


Option Explicit
'On Error Resume Next
Dim objFSO, objInFile, objOutFile, strFile, strOutData, strOutFile, strNoData
Dim strComputer, strDS_description, strServerDescription
Dim objLocalWMIService, objWMIService, colItems, objItem, objComputer, colComputers
Dim objPing, objStatus
Dim IsReachable

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objLocalWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\directory\LDAP")


' Input and Output files
strFile = "C:\audit\input.txt"
strOutFile = "C:\audit\output.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(strFile)
Set objOutFile = objFSO.CreateTextFile(strOutFile, True)

Do Until objInFile.AtEndOfStream

strComputer = objInFile.ReadLine


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery("SELECT * FROM ds_computer",,48)
For Each objItem In colItems
strServerDescription = Join(objItem.DS_description, ",")
strServerDescription = objItem.DS_description
next


' Write data to .csv file

strOutData = strComputer & ";" & strDS_description & ";" & vbCrLf & vbCrLf
objOutFile.Write strOutData

loop


' Close the .csv file
objOutFile.Close


' Display audit complete dialogue when finished
MsgBox "Audit Completed Successfully
 
not add, replace.

Code:
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\directory\LDAP")
Set colItems = objWMIService.ExecQuery("SELECT * FROM ds_computer",,48)
For Each objItem In colItems
strServerDescription = Join(objItem.DS_description, ",")
[s]strServerDescription = objItem.DS_description[/s]
next


-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Thank you Geates. I have removed that line, but unfortunately i get the same result. The only thing i get in the .csv file is,

192.168.70.12;;
192.168.70.14;;

etc.

Is there anything else i've missed?
 
add a msgbox colItems.count after setting colItems to see if anything is returned.

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
wotsit:

As I indicated in my post, when i execute the query on MY LOCAL MACHINE ONLY (strComputer = "."), it gives information about ALL computers on the domain. When I execute it with strComputer set to an IP address of another computer / server on the domain (like you are doing), I get no information (like you are getting).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top