Great Technology Talk

Friday, May 22, 2009

Powershell: Return GPO OU links

Wrote a powershell function to return OU links for a GPO.

###########################################################################
# Function : GetGpoOULinks
# Description: Returns all OU links for GPOs in the current domain
# Parameters : $objGPO - GPO object
# Returns : Array of OU links for the GPO.
###########################################################################
function GetGpoOULinks(
$objGPO=$(throw '$objGPO is required'))
{
$gpmSearchCriteria = $gpm.CreateSearchCriteria()
$gpmSearchCriteria.Add($gpmConstants.SearchPropertySomLinks, $gpmConstants.SearchOpContains, $objGPO)
$gpmSomList = $gpmDomain.SearchSOMs($gpmSearchCriteria)

$srtResults=@()
#$srtResults=$($objGPO.displayname) +":"+ $($objGPO.id)+","
if ($gpmSomList.Count -ne 0)
{
foreach ($som in $gpmSomList)
{
$links=$som.GetGPOLinks()
foreach ($link in $links)
{
If ($link.gpoid -eq ($objGPO.id)){
If ($link.enabled){
$srtResults=$srtResults + "$($som.name) (T),"
}
Else
{
$srtResults=$srtResults + "$($som.name) (F),"
}
}
}
}
}
Else
{
$srtResults=$srtResults + "GPO Unlinked" # This GPO is unlinked.
}
return $srtResults
}



Used it in a script that searched sysvol for GPOs that matched certain criteria, when found I queried AD to return where those GPOs were linked.

Labels: ,