Author Posts

January 1, 2012 at 12:00 am

by Nidhin CK at 2012-12-19 14:10:34

Could someone help me to modify the below script which i wrote for finding the empty OU's in AD. Im new to this powershell .. below code gives results but this will not work in all condition, for eg if an OU has more than one sub OU

cls
Import-Module activedirectory
$ou = Get-ADOrganizationalUnit -Filter {name -eq "che"} -Properties distinguishedname
$allou = Get-ADObject -Filter {objectclass -eq "organizationalUnit"} -SearchBase $ou
foreach ($singleou in $allou){
$objects = Get-ADObject -Filter * -SearchBase $singleou
$a = @($objects).length
if ($a -gt 2){
Write-Output "$objects is is not Empty" | Out-File c:\output.txt -Append
}
else{
Write-Output "$objects empty" | Out-File c:\output.txt -Append
}
}

by Infradeploy at 2012-12-20 03:44:35

$ou is an array.

So it's:

$ous = Get-ADOrganizationalUnit -Filter {name -eq "che"} -Properties distinguishedname
foreach$ou in $ous){
$allou = Get-ADObject -Filter {objectclass -eq "organizationalUnit"} -SearchBase $ou
foreach ($singleou in $allou){
$objects = Get-ADObject -Filter * -SearchBase $singleou
$a = @($objects).length
if ($a -gt 2){
Write-Output "$objects is is not Empty" | Out-File c:\output.txt -Append
}
else{
Write-Output "$objects empty" | Out-File c:\output.txt -Append
}
}
}