【问题标题】:trying to get all the GPO's related to the OU with invoke command尝试使用调用命令获取与 OU 相关的所有 GPO
【发布时间】:2020-05-26 10:41:43
【问题描述】:

我有一套代码:

#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> 调用命令 -session $s -scriptblock {get-gpo -guid $test} 无法验证参数“Guid”上的参数。参数为 null 或空。提供一个不为 null 或空的参数,然后重试该命令。 + CategoryInfo : InvalidData: (:) [Get-GPO], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.GroupPolicy.Commands.GetGpoCommand + PSComputerName : 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} 

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

【问题讨论】:

  • {get-gpo -guid $test} -> {get-gpo -guid $using:test}
  • 那是我的一种。如果我这样做 {get-gpo -guis $using:test} 我会得到一个错误,因为查看 $test,我需要以某种方式对其进行子串化并通过它,我需要剪切第一部分“cn =”和“之前” },cn=poli...
  • $test | Select-Object -ExpandProperty cn

标签: windows powershell invoke gpo


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多