Share via


Cannot modify sAMAccountName attribute

Question

Tuesday, November 22, 2011 3:06 PM

I cannot update sAMAccountName attribute value. Here is very simple code. I tried update value but it failed. Here is also error message.

*******

cls

 

# Variables

$ou = "OU=BasicUsers,DC=JHU,DC=LOCAL"

 

# Get all users in the target OU

$Users = Get-ADUser -Filter * -SearchBase $ou

 

# Iterate the users and update the department and title attributes in AD

foreach($User in $Users) {

 

# Variables

$first = $last = $sam = $orgsam = $null

 

    # Originals

    $orgsam = $User.sAMAccountName

 

    # sAMAccountName

    $first = $User.givenName

$last = $User.surname

    $sam = $first + "." + $last

    $sam = $sam.ToLower()

 

    # write attributes

    $User.sAMAccountName = $sam

}

********* Error ***********

Exception setting "sAMAccountName": "The adapter cannot set the value of property "sAMAccountName"."

At line:25 char:11

+     $User. <<<< sAMAccountName = $sam

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

Exception setting "sAMAccountName": "The adapter cannot set the value of property "sAMAccountName"."

At line:25 char:11

+     $User. <<<< sAMAccountName = $sam

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

Exception setting "sAMAccountName": "The adapter cannot set the value of property "sAMAccountName"."

At line:25 char:11

+     $User. <<<< sAMAccountName = $sam

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : PropertyAssignmentException

 

 

________________________________________________________________________________________________________________________________________________________________________________________________________________

All replies (3)

Tuesday, November 22, 2011 3:22 PM ✅Answered

Get-ADUser -Filter * -SearchBase $ou  | Foreach {Set-ADUser $_ -Replace @{samaccountname = ($_.givenname+"."+$_.surname).ToLower()}}

Tuesday, November 22, 2011 4:14 PM ✅Answered

Thanks very much. If I want to update more than one attribute I need to do something more?

-Replace @{title="director";mail=[email protected]}

For more information about Set-AdUser - http://technet.microsoft.com/en-us/library/ee617215.aspx


Tuesday, November 22, 2011 4:09 PM

Thanks very much. If I want to update more than one attribute I need to do something more?