首页 > 解决方案 > 如何解决循环中的错误以从 OU 获取 GPO

问题描述

我创建了一个 PS 脚本来查找在 OU 和 subOU 中链接的 GPO,我做错了什么?我收到此错误并对消息感到困惑:

Get-ADOrganizationalUnit:无法绑定参数“身份”。无法将“Selected.Microsoft.ActiveDirectory.Management.ADOrganizationalUnit”类型的“@{DistinguishedName=OU=Windows 7,DC=domain,DC=com}”值转换为“Microsoft.ActiveDirectory.Management.ADOrganization alUnit”类型。在 F:\Untitled4.ps1:11 char:39 + $LinkedGPOs = Get-ADOrganizationalUnit <<<< $OU | 选择对象-ExpandProperty LinkedGroupPolicyObjects + CategoryInfo : InvalidArgument: (:) [Get-ADOrganizationalUnit], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADOrganizationalUnit

get-module activedirectory,grouppolicy

$exportFilePath = "c:\temp\AllGPOsWin7-report date $(get-date -f dd-MM-yyyy).csv"
$OU = "ou=Windows 7,dc=domain,dc=com"

$AllSubOU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * | 
     Select-Object DistinguishedName

foreach ($OU in $AllSubOU){
$OU
$LinkedGPOs = Get-ADOrganizationalUnit $OU | select-object -ExpandProperty LinkedGroupPolicyObjects

$LinkedGPOGUID = $LinkedGPOs | foreach-object{$_.substring(4,36)}

$GPOName = $LinkedGPOGUID | foreach-object{get-gpo -guid $_ | select-object displayname}

$OU,$ListGPO -join "," | Out-File -FilePath $exportFilePath -Append -Width 200

标签: powershell

解决方案


问题在于你的

$AllSubOU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * | 
 Select-Object DistinguishedName

命令,这应该更改为

$AllSubOU = Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter * | 
 Select-Object -ExpandProperty DistinguishedName

由于您没有指定展开部分,PowerShell 将其返回到容器中。因此 OU 在错误代码中的大括号 {} 中

更新- 根据对此答案的评论,请参见下文:

#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

推荐阅读