priara
priara

Reputation: 25

"Cannot validate argument on parameter 'Identity'" while changing the description for multiple users

I have a simple script to change part of the description for a user in AD.

Get-ADUser testuser -Properties description | ForEach-Object { 
    Set-ADUser $_.SamAccountName -Description "ChgDescript,$($_.Description.Split(',')[1])" 
}

However, I now have 700 users, where I need to change part of the description, using the command above. I'm unable to figure out the script correctly to import the .csv file and run the script against it.

$csvFile = "path_is_here"

Import-Module ActiveDirectory

Import-Csv $csvFile | Get-ADUser -Properties description | ForEach-Object { 
    Set-ADUser $_.SamAccountName -Description "ChgDescript,$($_.Description.Split(',')[1])" 
}

When I run the script above, I receive the following error:

Cannot validate argument on parameter 'Identity'.

Upvotes: 1

Views: 40285

Answers (3)

Tell PowerShell which delimiter to use, when it is not comma: import-csv -Path "c:\test.csv" -Delimiter ";"

And it will work fine.

Upvotes: 0

Jeff
Jeff

Reputation: 546

The error is coming from the Get-ADuser cmdlet and informing you that its -Identity parameter is null. I don't know the structure of your input file; but lets assume that its just a list of account names (so I added a header value of AccountName for readability), pass that property of your custom object created by your Import-Csv cmdlet to the Get-ADUser's -Identity parameter and see if it helps.

Unit Test Example:

Import-Csv -Path "C:\Temp\Users.txt" -Header "AccountName" | ForEach-Object {
    Get-ADUser -Identity $_.AccountName
}

Checkout method 3 in the help examples for Set-ADUser

http://go.microsoft.com/fwlink/?LinkID=144991

Modify the Manager property for the "saraDavis" user by using the Windows PowerShell command line to modify a local instance of the "saraDavis" user. Then set the Instance parameter to the local instance.

See if this works:

$Users = Import-Csv -Path "C:\test_users.txt" -Header "AccountName"
     foreach($User in $Users){ 
     $ADUser = Get-ADUser -Identity $User.AccountName -Properties Description 
     $ADUser.Description = "PUT YOUR MODIFIED DESCRIPTION HERE" 
     Set-ADUser -Instance $ADUser 
}

Upvotes: 3

Chilloutfaktor
Chilloutfaktor

Reputation: 16

May you have to store the description in a variable before, like:

Get-Content users.txt | Get-ADUser  -Prop Description | Foreach {
$desc = $_.description + " Disabled 21122012 123456"
     Set-ADUser  $_.sAMAccountName -Description $desc
 }

(just copied from technet)

Upvotes: 0

Related Questions