2017-04-13 70 views
0

我有以下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 Parameter "Replace" kann nicht an das Ziel gebunden werden. Ausnahme beim Festlegen von "Replace": " Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt." Bei C:\Scripts\Set_AD_Phone.ps1:8 Zeichen:9 + -Replace <<<< @{otherTelephone=$($.telephoneNumber);otherFacsimileTelephoneNumber=$($.facsimileTelephoneNumber);ot herMobile=$($_.mobile)}} + CategoryInfo : WriteError: (:) [Set-ADObject], ParameterBindingException + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADObject

我看不到任何錯誤的腳本沒有任何人有一個想法

回答

0

Select *在您的命令已過時,你也不必使用。cmdlet的,只是管Get-ADUserSet-ADObject cmdlet的結果,並省略-Identity參數:

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

是正確的。但是我想稍後爲所有用戶運行腳本。然後必須使用選擇和ForEach對象: Get-ADUser -filter * - 屬性telephoneNumber,otherTelephone,傳真電話號碼,otherFacsimileTelephoneNumber,mobile,otherMobile | – Michael

+0

我懷疑你需要那個。 –

+0

我想我找到了錯誤信息的原因:問題是,有些用戶沒有手機號碼或傳真號碼。對於這些用戶,我收到錯誤消息。但我不知道如何處理這個問題。 :( – Michael