簡體   English   中英

如何解決循環錯誤以從OU獲取GPO

[英]how to resolve Error in Loop to get GPO from OU

我創建了一個PS腳本來查找在OU和subOU中鏈接的GPO,我在做什么錯呢? 我收到此錯誤,並對消息感到困惑:

Get-ADOrganizationalUnit:無法綁定參數“ Identity”。 無法將類型“ 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

問題出在你的

$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

請注意, $ListGPO不存在,並且如果有多個鏈接,則必須擴展GPO。 此外, Get-ADOrganizationalUnit必須提取屬性LinkedGroupPolicyObjects才能使用它(使用-Properties LinkedGroupPolicyObjects ),我將-join中的分隔符從“,”更改為“ |” 因為專有名稱中包含逗號。 同樣, -width 200將在達到該限制時切斷GPO。 建立一個自定義對象並使用Export-Csv可能更好。

在下面找到一個暫定腳本。

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

$AllSubOU = (Get-ADOrganizationalUnit -SearchBase $OU -SearchScope Subtree -Filter *).DistinguishedName

foreach ($myOU in $AllSubOU){
    $myOU
    $LinkedGPOs = ( Get-ADOrganizationalUnit $myOU -properties LinkedGroupPolicyObjects).LinkedGroupPolicyObjects
    $LinkedGPOGUID = $LinkedGPOs | foreach-object{$_.substring(4,36)}
    $GPOName = ( $LinkedGPOGUID | foreach-object { (get-gpo -guid $_) | select-object -expandproperty displayname}) -join "|"
    $out = if ( $GPOname ) { $myOU,$GPOName -join "|"} else { $myOU } 
    $out | Out-File -FilePath $exportFilePath -Append -Width 200
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM