Проблема связана с вашей командой
$AllSubOU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * |
Select-Object DistinguishedName
, ее следует изменить на
$AllSubOU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * |
Select-Object -ExpandProperty DistinguishedName
Поскольку вы не указали часть раскрытия, PowerShell возвращал ее внутри контейнера,Следовательно, подразделение в фигурных скобках {} в коде ошибки
ОБНОВЛЕНИЕ - Следуя комментариям к этому ответу, см. Ниже:
#create a new hashtable
$hashTable = [hashtable]::new{}
#define the root OU
$rootOU = "OU=computers,DC=domain,DC=local"
#get all the sub OU in the root OU
$allSubOU = Get-ADOrganizationalUnit -SearchBase $rootOU -SearchScope Subtree -Filter * | Select-Object -ExpandProperty DistinguishedName
#loop through the sub OUs
foreach ($subOU in $allSubOU){
#get the linked GPO objects on the OU
$linkedGPO = Get-ADOrganizationalUnit $subOU | Select-Object -ExpandProperty LinkedGroupPolicyObjects
#if the OU has a GPO then run through them
if ($linkedGPO){
#adding name to hashtable for current OU
$hashTable.Add($subOU, @())
#running through each GPO in sub OU
foreach($GPO in $linkedGPO){
#getting GUID from GPO
$GPOGuid = $GPO.SubString(4,36)
#using GUID to get displayname of GPO
$GPOName = Get-GPO -Guid $GPOGuid | Select-Object -ExpandProperty DisplayName
#add the GPO to the hashttable for the current OU
$hashTable.$subOU += $GPOName
}
}else{
#ou has no GPOs
$hashTable.Add($subOU, "No GPO")
}
}
#enumerate through the hashtable
$hashTable.GetEnumerator() |
#add OU to OU column in csv
Select-Object -Property @{N='OU';E={$_.Name}},
#add GPO to GPO(s) column in CSV
@{N='GPO(s)';E={$_.Value} } |
#export to CSV
Export-Csv -NoTypeInformation -Path PATH