0

I have the following PowerShell script:

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)}}

The problem is, that some users don't have a mobile phone number or facsimile Number. For these users I receive the following error message:

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

I cannot see any error at the script. does anyone has an idea?

1 Answer 1

0

The Select * in your command is obsolete. Also you don't have to use the ForEach-Object cmdlet, just pipe the result of Get-ADUser to the Set-ADObject cmdlet and omit the -Identity parameter:

Get-ADUser -Identity rzhrzzim -Properties DistinguishedName, telephoneNumber, otherTelephone, facsimileTelephoneNumber, otherFacsimileTelephoneNumber, mobile, otherMobile | 
    Set-ADObject -Replace @{otherTelephone=$_.telephoneNumber;otherFacsimileTelephoneNumber=$_.facsimileTelephoneNumber;otherMobile=$_.mobile}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes right. But i want to run the script later for all users. Then is have to use the Select and the the ForEach-Object: Get-ADUser -filter * -Properties telephoneNumber, otherTelephone, facsimileTelephoneNumber, otherFacsimileTelephoneNumber, mobile, otherMobile|
I doubt you need that.
I think I found the reason for the error messages: The problem is, that some users don't have a mobile phone number or facsimile Number. For these users I receive the error messages. But I don't know how to handle this. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.