Vbs Script To Get A Machines Parent Organizational Unit

 

This Vbs script will prompt you for a machine name and then return the parent Organizational Unit (OU) that it belongs to.

 

Note: Be sure to change Your_DC_Name to your DC name in the line that reads:

 

'LDAP://dc=Your_Dc_Name,dc=com' WHERE objectCategory = 'computer' " _

 

Vbs Script:

 

Const ADS_SCOPE_SUBTREE = 2

 

strComputer = InputBox("Enter Machine Name")

 

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

 

On Error Resume Next

objCommand.CommandText = "Select ADsPath From 'LDAP://dc=Your_Dc_Name,dc=com' WHERE objectCategory = 'computer' " _

& "AND name='" & strcomputer & "'"

Set objRecordSet = objCommand.Execute

 

objRecordSet.MoveFirst

 

Do Until objRecordSet.EOF

strResult = objRecordSet.Fields("ADsPath").Value

objRecordSet.MoveNext

Loop

 

If strResult <> "" Then

Wscript.Echo "The Parent OU For " & UCase(strComputer) & " is: " & strResult

Else

Wscript.Echo "The Machine Was Not Found"

End If

 

 

 

Published Sunday, November 16, 2008 7:46 PM by dhite
Filed under:

Comments

No Comments