无法将参数'Replace'绑定到目标。异常设置“替换”:“对象参考

时间:2017-04-13 07:34:29

标签: powershell active-directory

我有以下PowerShell脚本:

Import-Module ActiveDirectory

# Find all accounts with a Department
# Copy that value into Description
Get-ADUser -filter * -Properties telephoneNumber, otherTelephone, facsimileTelephoneNumber, otherFacsimileTelephoneNumber, mobile, otherMobile | 
Select-Object * | 
ForEach-Object {Set-ADObject -Identity $_.DistinguishedName `
-Replace @{otherTelephone=$($_.telephoneNumber);otherFacsimileTelephoneNumber=$($_.facsimileTelephoneNumber);otherMobile=$($_.mobile)}}

问题是,有些用户没有手机号码或传真号码。对于这些用户,我收到以下错误消息:

  

Set-ADObject:Der参数“替换”kann nicht和das Ziel gebunden   werden。 Ausnahme beim Festlegen von“替换”:“Der Objektverweis   wurde nicht auf eine Objektinstanz festgelegt。“Bei   C:\ Scripts \ Set_AD_Phone.ps1:8 Zeichen:9   + -Replace<<<< @ {otherTelephone = $($ .telephoneNumber); otherFacsimileTelephoneNumber = $($ .facsimileTelephoneNumber);催产素   herMobile = $($ _。移动)}}       + CategoryInfo:WriteError:(:) [Set-ADObject],ParameterBindingException       + FullyQualifiedErrorId:ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADObject

我在剧本中看不到任何错误。有没有人有想法?

1 个答案:

答案 0 :(得分:0)

命令中的Select *已过时。此外,您不必使用ForEach-Object cmdlet,只需将Get-ADUser的结果通过管道传输到Set-ADObject cmdlet,并省略-Identity参数:

Get-ADUser -Identity rzhrzzim -Properties DistinguishedName, telephoneNumber, otherTelephone, facsimileTelephoneNumber, otherFacsimileTelephoneNumber, mobile, otherMobile | 
    Set-ADObject -Replace @{otherTelephone=$_.telephoneNumber;otherFacsimileTelephoneNumber=$_.facsimileTelephoneNumber;otherMobile=$_.mobile}