Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


WMI classes

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,3500
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> WMI classes
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 WMI classes - 6/23/2005 1:29:14 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
I'm using vbscript to get information about computers on my network using the Active Directory and the WMI classes. Is there a way that I can get what OU a computer is located in. It's the only piece of info that I have left to get.
 
 
Post #: 1
 
 Re: WMI classes - 6/23/2005 2:22:40 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Here is a short script I wrote some time ago to examine my domain... you should be able to rework it slightly to fit your needs. Note the caveat in the comment that the default Computers container will not be searched (since it isn't an OU) so you might want to deal with that... the point being that not all computers in AD need to be part of an OU.

' This script enumerates all the computers in a domain and
' lists the OU in which they're defined. Note that if a
' computer is not a member of an OU that it won't be reported
' by this script... that includes all computers which are
' in the container "Computers" (the default location when a
' computer is remotely added to the domain).

Dim iNextID
iNextID = 0

Set oRootDSE = GetObject("LDAP://RootDSE")
Set oDomain = GetObject("LDAP://" & oRootDSE.Get("DefaultNamingContext"))

Call EnumOUs(oDomain.ADsPath)

Sub EnumOUs(sADsPath)
Set oContainer = GetObject(sADsPath)
oContainer.Filter = Array("OrganizationalUnit")
For Each oOU in oContainer
'' WScript.Echo oOU.ADsPath
Call EnumComps(oOU.ADsPath, oOU)
Call EnumOUs(oOU.ADsPath)
Next
End Sub

Sub EnumComps(sADsPath, oOU)
Set oContainer = GetObject(sADsPath)
oContainer.Filter = Array("User")
For Each oADobject in oContainer
If oADobject.Class = "computer" Then
Wscript.Echo oADobject.Name & " in OU " & oOU.ADsPath
End If
Next
End Sub

(in reply to orangedude)
 
 
Post #: 2
 
 Re: WMI classes - 6/23/2005 2:47:04 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
Thanks,

I have a quick question tho. What do each of the sub routines do exactly? I just want to understand the code better. I guess I will call the routines and check the oADobject.Name each time and if its the right name that i'm looking for then report out and stop.

(in reply to orangedude)
 
 
Post #: 3
 
 Re: WMI classes - 6/23/2005 2:49:22 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
Also I do i just get the OU name out of the oOU.ADsPath? What are the DC?

(in reply to orangedude)
 
 
Post #: 4
 
 Re: WMI classes - 6/23/2005 3:11:18 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
EnumOUs finds and iterates through every OU in the domain. For each one it finds, it calls EnumComps which finds and iterates through each "User" object looking for a "computer" type of entry (users and computers are mixed together as "User" objects).

DC refers to "Domain Controller" and the "DC=xxxx" entries are the nodes of your domain name.

Yes, you need to "parse" the resulting strings returned from oADobject.Name and oADobject.ADsPath to pick out what you need. Alternatively, you can use oADobject.sAMAccountName instead of oADobject.Name to return the computer name but it will have a trailing "$" appended to it.

(in reply to orangedude)
 
 
Post #: 5
 
 Re: WMI classes - 6/23/2005 3:16:42 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
OK,

Thanks alot. Can a computer be in two OUs. Because it reports out two OU=xxx, OU=xxx. ?

(in reply to orangedude)
 
 
Post #: 6
 
 Re: WMI classes - 6/23/2005 3:43:48 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
Here is another example script that approaches your problem more directly... just be sure to change the DC values and the computer-name value you need (you have to append the "$" character to the end of your computer name). I didn't put any error checking in it so you might want to add that... if you use a computer name that can't be found you will receive an error message.

' Herẻs the revised script that searches Active Directory and reports back
' the OU where the computer account resides.

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = _
"SELECT distinguishedName FROM 'LDAP://dc=YourDomain,dc=com' " & _
"WHERE objectCategory = 'computer' " & _
"AND sAMAccountName = 'galahad$'"
Set objRecordSet = objCommand.Execute

objRecordSet.MoveFirst
Do Until objRecordSet.EOF
strDN = objRecordSet.Fields("distinguishedName").Value
arrPath = Split(strDN, ",")
intLength = Len(arrPath(1))
intNameLength = intLength - 3
Wscript.Echo Right(arrPath(1), intNameLength)
objRecordSet.MoveNext
Loop

(in reply to orangedude)
 
 
Post #: 7
 
 Re: WMI classes - 6/23/2005 5:23:17 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
I got the first code example to work. Thanks alot.

(in reply to orangedude)
 
 
Post #: 8
 
 Re: WMI classes - 6/23/2005 6:52:59 AM   
  Xandros

 

Posts: 100
Score: 0
Joined: 6/23/2005
From:
Status: offline
When you see two objects of the same type in the path that means there is a hierarchy of one inside the other... in other words your example shows one OU as a child of another OU (i.e. a "sub-OU", if you will).

(in reply to orangedude)
 
 
Post #: 9
 
 Re: WMI classes - 6/28/2005 2:21:41 AM   
  orangedude

 

Posts: 29
Score: 0
Joined: 6/23/2005
From:
Status: offline
Oh. Ok thanks alot.

(in reply to orangedude)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> WMI classes Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts