powershell - 如何解决循环中的错误以从 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
解决方案
问题在于你的
$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
推荐阅读
- javascript - 自动设置输入值
- google-chrome - 用户使用 chrome.identity.launchWebAuthFlow 登录后,如何在过期前获得新的访问令牌?
- android - 尝试获取“BottomNavigationView”的布局参数时应用程序崩溃
- matlab - 三次样条 MATLAB 错误结果
- python - Python requests.post 代码将结果限制为 200。如何调整代码以获得所有 1000+ 的结果?
- html - 为什么我的视频没有显示?
- c++ - 为什么我能够更改作为常量引用传递的对象的成员?
- scala - 如何从 spark DataFrame 中获取所有行?
- algorithm - 如何查找目的地和源之间的距离是否小于5?
- c++ - 一个参数构造函数如何工作?