【问题标题】:How to pass AD computer names to array?如何将 AD 计算机名称传递给数组?
【发布时间】:2018-10-07 16:52:48
【问题描述】:

我正在尝试在与添加到 $servers 数组的过滤器匹配的所有计算机上设置 ADComputer。但它不起作用。我想这与将对象传递给字符串有关,但我无法理解它。有人有黄金提示吗?

#Get gateway
$gateway = "MGMT01"
$gatewayObject = Get-ADComputer -Identity $gateway

#Get servers
$servers=@(Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"}   -Properties Name | select name | ft -HideTableHeaders)

#Create list of servers
Out-File -FilePath c:\adcomputers.txt -InputObject $servers

#Set WAC delegation
ForEach ($server in $servers)
{
$nodeObject = Get-ADComputer -Identity $server
Set-ADComputer -Identity $nodeObject -PrincipalsAllowedToDelegateToAccount $gatewayObject
}

错误:

Get-ADComputer:无法绑定参数“身份”。无法转换类型为“Microsoft.PowerShell.C”的“Microsoft.PowerShell.Commands.Internal.Format.FormatEndData”值 ommands.Internal.Format.FormatEndData”键入“Microsoft.ActiveDirectory.Management.ADComputer”。

在 C:\Users\SA.****\Desktop\inventorize-honolulu-incl-sso.ps1:7 char:40 + $nodeObject = 获取 ADComputer -Identity $server + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ADComputer], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Set-ADComputer:无法验证参数“身份”上的参数。参数为空。为参数提供一个有效值,然后再次尝试运行该命令。

在 C:\Users\SA.****\Desktop\inventorize-honolulu-incl-sso.ps1:8 char:26 + 设置 ADComputer -Identity $nodeObject -PrincipalsAllowedToDelegateToAc ... + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Set-ADComputer], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADComputer

【问题讨论】:

    标签: arrays powershell object foreach active-directory


    【解决方案1】:

    要将您的服务器列表输出到文本文件,您只需要以下内容:

    Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"} |
      Select-Object -ExpandProperty Name |
      Out-File "c:\adcomputers.txt"
    

    【讨论】:

    • 感谢您的回复,虽然我在生成 .txt 文件时没有任何问题。该错误与 ForEach 循环有关。查询到的计算机名在哪里,不要传给-identity参数。
    • 实际上您现有的生成文本文件的代码不正确:我们不使用ftFormat-Table 的别名)输出到文本文件(因此我的答案)。跨度>
    【解决方案2】:

    Bill_Stewart 的想法是对的,只是与您的工作方式不完全相符。

    ft -HideTableHeaders 弄乱了您的阵列。请改用select -ExpandProperty

    $servers=@(Get-ADComputer -Filter {OperatingSystem -like "Windows Server*"}   -Properties Name | select -ExpandProperty name)
    

    这会给你一个纯字符串数组,就像你想要的那样。

    【讨论】:

    • 感谢您提出这些话。事实上,它与那个编辑一起运行。谢谢加布里埃尔和@Bill_Stewart。我将研究 -ExpandProperty 的使用。
    【解决方案3】:

    您的 Get-ADComputer 行是表达式问题,您在 {} 中缺少 ()。修复该问题后,您的示例运行良好。

    $servers=@(Get-ADComputer -Filter {(OperatingSystem -like "Windows Server*")} -Properties Name | select name | ft -HideTableHeaders)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-30
      相关资源
      最近更新 更多