ProcessOU
From Terminal23wiki
Call ProcessOU with an OU string that looks like a location such as "Security Groups/Resource Groups/File Server." ProcessOU will then use TestIfOUExists, and if it doesn't, will use CreateADOU. In this way, if you have an AD user or object you need created in a particular location, before creating it, just call ProcessOU with the target location. This will either verify or create it.
function CreateADOU ($strOULocation,$strNewOU)
{
$strLDAP = "LDAP://" + $strOULocation + "dc=SUBDOMAIN,dc=DOMAIN,dc=com"
$objOU = [ADSI]$strLDAP
$objNewOU = $objOU.Create("organizationalUnit", "ou=$strNewOU")
$objNewOU.SetInfo()
}
function TestIfOUExists ($strOUstring)
{
$strLDAP = "LDAP://" + $strOUstring + "dc=SUBDOMAIN,dc=DOMAIN,dc=com"
$objTest = [ADSI]$strLDAP
if ($objTest.Name -ne $null){ return $true } else { return $false }
}
function ProcessOU ($strOUInput)
{
$strLocation = $strOUInput.split("/")
$strOUPreTest = ""
$strOUPostTest = ""
foreach ($i in $strLocation)
{
$strOUPreTest = "ou=" + $i + "," + $strOUPreTest
if (TestIfOUExists $strOUPreTest -eq $true)
{ }
else
{ CreateADOU $strOUPostTest $i }
$strOUPostTest = $strOUPreTest
}
}
