尝试使用 invoke 命令获取与 OU 相关的所有 GPO

trying to get all the GPO's related to the OU with invoke command

我有一套代码:

#Find the OU with the selected Canonical name and save it to this variable
$OUObject = Invoke-Command -Session $S -ScriptBlock {Get-ADOrganizationalUnit -filter *  -Property CanonicalName | Where-Object {$_.CanonicalName -eq $using:listBox2.SelectedItem}}

所以在这段代码之后,我得到了一个存储在变量 $OUObject 中的 OU。 我现在想让所有 gpo 都链接到这个 ou。 所以我的下一步是:

$test = $OUObject.LinkedGroupPolicyObjects

现在 $test 保存所有链接到它的 ou 的 gpos。现在的问题是我想通过名字获取它们。所以我可以这样做:

invoke-command -session $s -scriptblock {get-gpo -guid $test} 

但我会得到这个错误: PS C:\WINDOWS\system32> invoke-command -session $s -scriptblock {get-gpo -guid $test} 无法验证参数 'Guid' 的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。 + CategoryInfo : InvalidData: (:) [Get-GPO], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.GroupPolicy.Commands.GetGpoCommand + PS计算机名:DC01

所以我查看了 $test,这就是它的内容:

PS C:\WINDOWS\system32> $test
cn={5873971D-F689-4E83-8AFA-389FDD7F34CD},cn=policies,cn=system,DC=bla,DC=local
cn={2B7F8931-038E-46BC-B1DB-FBFA86097C08},cn=policies,cn=system,DC=bla,DC=local
cn={C74CADA1-B609-44A3-8D3C-F733CF3112E2},cn=policies,cn=system,DC=bla,DC=local

所以我真正需要的是仅将 cn{..}

中的部分传递给 get-gpo 命令

如果我硬编码并执行此操作:

invoke-command -session $s -scriptblock {get-gpo -guid 5873971D-F689-4E83-8AFA-389FDD7F34CD} 

我得到了正确的结果。 谁能帮我实现这个目标?

使用正则表达式 -replace 运算符从 DN 中提取 GUID,然后使用 $using: 修饰符将值传递给 Invoke-Command

$GUIDs = $test -replace '^cn=(\{[0-9a-f-]+\}).*$'
Invoke-Command -Session $s { $using:GUIDs |ForEach-Object { Get-GPO -Guid $_ } }