关于powershell:如何将AD计算机名称传递给数组?

How to pass AD computer names to array?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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:无法绑定参数" Identity"。无法转换类型为" 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 = Get-ADComputer-身份$ server
+ ~~~~~~~~
+ CategoryInfo:InvalidArgument:(:) [Get-ADComputer],ParameterBindingException
+ FullyQualifiedErrorId:CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.GetADComputer

Set-ADComputer:无法验证参数" Identity"上的参数。参数为空。为参数提供有效值,然后尝试再次运行命令。

在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


要将服务器列表输出到文本文件,您需要做的是:

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


您的Get-ADComputer行是一个表达式问题,{}中缺少()。 解决该问题后,您的示例可以正常工作。

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

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

ft -HideTableHeaders弄乱了您的数组。 使用select -ExpandProperty代替:

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

如您所愿,这将为您提供一系列纯字符串。