$=SetOption('Explicit','On')
Dim $strComputerName, $strOU
; Get the computerName of PC
$strComputerName = @WKSTA
; Call function to find OU from computer name
$strOU = GetOUByComputerName($strComputerName)
? $strOU
Function GetOUByComputerName($strComputerName)
; *** Function to find ou/container of computer object from computer name ***
Dim $strNamingContext, $strFilter
Dim $objConnection, $objCommand, $objRecordSet, $objRootDSE
Dim $aR, $R, $C, $x
; Bind to the RootDSE to get the default naming context for
; the domain. e.g. dc=wisesoft,dc=co,dc=uk
$objRootDSE = GetObject("LDAP://RootDSE")
$strNamingContext = $objRootDSE.Get("defaultNamingContext")
$objRootDSE = ""
; Construct an ldap filter to search for a computer object
; anywhere in the domain with a name of the value specified.
$strFilter = "<LDAP://"+$strNamingContext+">;(&(objectClass=Computer)(name="+$strComputerName+"));distinguishedName;subtree"
; Standard ADO code to query database
$objConnection = CreateObject("ADODB.Connection")
$objCommand = CreateObject("ADODB.Command")
$objConnection.Provider = "ADsDSOObject"
$objConnection.Open("Active Directory Provider")
$objCommand.activeconnection = $objConnection
$objCommand.commandtext = $strFilter
$objRecordSet = $objCommand.Execute
$aR = $objRecordSet.GetRows()
Dim $aFR[Ubound($aR,2),Ubound($aR,1)]
For $R=0 to Ubound($aR,2)
$x=0
For $C=0 to Ubound($aR,1)
$aFR[$R,$C]=$aR[$C,$R]
If $x=0
$getOUByComputerName = $aFR[$R,$C]
$x=1
EndIf
Next
Next
$objRecordSet.Close
$objConnection.Close
EndFunction