在foreach循环中将get-aduser输出管道设置为set-aduser

时间:2015-09-30 15:47:49

标签: powershell

我正在尝试获取电话attrib为空的用户列表,并使用电话号码更新atrrib,到目前为止我的内容是:

$allen=gc "C:\0NIX\03SCRIPTS\TMP\jkirb\allen.txt"
$phonenumber = "972-xxx-xxx"

FOREACH ($user in $allen)
{
$nophone = get-aduser $user -pr *| where {$_.telephonenumber -eq $null} | select samaccountname |ft -HideTableHeaders
Set-ADuser -identity "$nophone" -replace @{telephonenumber="$phonenumber"}

}

这是错误的: Set-ADuser:找不到具有标识的对象:'Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData'在: 'DC = BHCS,DC = PVT'。 在行:7 char:1 + Set-ADuser -identity“$ nophone”-replace @ {telephonenumber =“$ phonenumber”} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo:ObjectNotFound:(Microsoft.Power ... t.FormatEndData:ADUser)[Set-ADUser],ADIdentityNotFoundException     + FullyQualifiedErrorId:找不到具有标识的对象:'Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerShell.Commands.Internal.Format.GroupStartData Mic    rosoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.PowerShell.Commands.Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData'在:'DC =
  BHCS,DC = PVT”。,Microsoft.ActiveDirectory.Management.Commands.SetADUser

1 个答案:

答案 0 :(得分:2)

当您使用任何Format- * cmdlet(在您的情况下为Format-Table)时,您正在向对象添加一些自定义格式,这会破坏对象以供将来管道使用。

请改为尝试:

$allen=gc "C:\0NIX\03SCRIPTS\TMP\jkirb\allen.txt"
$phonenumber = "972-xxx-xxx"

FOREACH ($user in $allen)
{
$nophone = get-aduser $user -pr *| where {$_.telephonenumber -eq $null} 
Set-ADuser -identity "$nophone" -replace @{telephonenumber="$phonenumber"}
}