首页 > 解决方案 > 如何将 AD 计算机名称传递给数组?

问题描述

我正在尝试在与添加到 $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 ommands.Internal.Format.FormatEndData”的“Microsoft.PowerShell.Commands.Internal.Format.FormatEndData”值转换为类型“Microsoft.ActiveDirectory.Management.ADComputer”。

在 C:\Users\SA.****\Desktop\inventorize-honolulu-incl-sso.ps1:7 char:40 + $nodeObject = Get-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 + Set-ADComputer -Identity $nodeObject -PrincipalsAllowedToDelegateToAc ... + ~~~~~~~ ~~~~ + CategoryInfo : InvalidData: (:) [Set-ADComputer], ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADComputer

标签: arrayspowershellobjectforeachactive-directory

解决方案


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

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

推荐阅读