Option Explicit
Dim objNetwork
Dim computerName
Dim ou
' Get the computerName of PC
Set objNetwork = CreateObject("Wscript.Network")
computerName = objNetwork.ComputerName
' Call function to find OU from computer name
ou = getOUByComputerName(computerName)
WScript.echo ou
Function getOUByComputerName(byval computerName)
' *** Function to find ou/container of computer object from computer name ***
Dim namingContext, ldapFilter, ou
Dim cn, cmd, rs
Dim objRootDSE
' Bind to the RootDSE to get the default naming context for
' the domain. e.g. dc=wisesoft,dc=co,dc=uk
Set objRootDSE = GetObject("LDAP://RootDSE")
namingContext = objRootDSE.Get("defaultNamingContext")
Set objRootDSE = Nothing
' Construct an ldap filter to search for a computer object
' anywhere in the domain with a name of the value specified.
ldapFilter = "<LDAP://" & namingContext & _
">;(&(objectCategory=Computer)(name=" & computerName & "))" & _
";distinguishedName;subtree"
' Standard ADO code to query database
Set cn = CreateObject("ADODB.Connection")
Set cmd = CreateObject("ADODB.Command")
cn.open "Provider=ADsDSOObject;"
cmd.activeconnection = cn
cmd.commandtext = ldapFilter
Set rs = cmd.execute
If rs.eof <> True And rs.bof <> True Then
ou = rs(0)
' Convert distinguished name into OU.
' e.g. cn=CLIENT01,OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
' to: OU=WiseSoft_Computers,dc=wisesoft,dc=co,dc=uk
ou = Mid(ou, InStr(ou, ",") + 1, Len(ou) - InStr(ou, ","))
getOUByComputerName = ou
End If
rs.close
cn.close
End Function