Script to Get Computer OU

Just some quick code to get the OU Name of the computer we run the script on.


Function GetComputerOU
	Dim objSysInfo: Set objSysInfo = CreateObject("ADSystemInfo")
	Dim objComputer: Set objComputer = GetObject("LDAP://" & objSysInfo.ComputerName)
	Dim objOU : Set objOU = GetObject(objComputer.Parent)
	GetComputerOU = objOU.OU
End Function

Wscript.Echo GetComputerOU


function GetComputerOU
	$SysInfo = New-Object -ComObject "ADSystemInfo"
	$Computer = [ADSI]("LDAP://{0}" -f $SysInfo.GetType().InvokeMember("ComputerName", [System.Reflection.BindingFlags]::GetProperty, $null, $SysInfo, $null))
	return ([ADSI]$Computer.Parent).OU


Was once an enthusiastic PepperByte employee but is now working elsewhere. His blogs are still valuable to us and we hope to you too.