locked
Set-Aduser update Country/region attribute field in ADUC RRS feed

  • Question

  • Hi,

    I have a script that checks a DB with user details and updates AD with those details.

    I can update all required fields except the country/region field.

    I know the attribute is co and i got it working in the past, but it is driving me nuts...

    This is the script:

    import-module  activedirectory 
    
    $result | foreach-object { Set-ADUser $_.username -title $_.title}
    $result | foreach-object { Set-ADUser $_.username -OfficePhone $_.work_phone}
    $result | foreach-object { Set-ADUser $_.username -City $_.city}
    $result | foreach-object { Set-ADUser $_.username -StreetAddress $_.address}
    $result | foreach-object { Set-ADUser $_.username -PostalCode $_.postcode}
    $result | foreach-object { Set-ADUser $_.username  -replace  @{co=$_.country}} 

    It runs fine but doesn't update the country/region field.

    I also tried like this:

    $result | foreach-object { Set-ADUser $_.username  -co $_.country}

    But i get the error:

    Set-ADUser : A positional parameter cannot be found that accepts argument 'France'.

    Any ideas?

    Thanks

    • Edited by slowscripter Tuesday, September 11, 2012 12:22 PM
    • Changed type slowscripter Tuesday, September 11, 2012 12:38 PM
    Tuesday, September 11, 2012 12:18 PM

Answers

  • There are three related AD attributes:

      • c is the two character country abbreviation, per ISO-3166.
      • co is the country name spelled out.
      • countryCode is an integer designating the language, per ISO-3166.

    My recollection is that ADUC assigns values to all three when you select a country in the GUI, but in code or script you must assign valid values to all three yourself. Also, the Country parameter of the Set-ADUser cmdlet assigns a value to the c attribute, while the CountryCode parameter assigns a value to the countryCode attribute. When I use the following:

    Set-ADUser -Identity jsmith -Country "US"

    -----

    The value "US" is assigned to the c attribute and "United States" shows in ADUC on the Address tab, but no values are assigned to the co or countryCode attributes.


    Richard Mueller - MVP Directory Services


    • Edited by Richard MuellerMVP, Banned Tuesday, September 11, 2012 4:04 PM typos
    • Proposed as answer by Yan Li_ Thursday, September 13, 2012 7:48 AM
    • Marked as answer by Yan Li_ Monday, September 17, 2012 1:42 AM
    Tuesday, September 11, 2012 4:02 PM

All replies

  • $result |

    foreach-object { Set-ADUser $_.username -replace @{country=$_.country}}

    The attribute name in AD is 'country'.

    You should change the type on this thread to "question".  A discussion does not involve a specific question, nor will it have a definitive answer.  This is not one of those.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, September 11, 2012 12:25 PM
  • 1) First - "The country/region in the address of the user. The country/region is represented as a 2-character code based on ISO-3166."

    2) Second

    $result | foreach-object { Set-ADUser $_.username  -Country $_.country}

    Tuesday, September 11, 2012 12:27 PM
  • If the 'country' attribute is the 2-character ISO code, then what is 'countrycode'?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, September 11, 2012 12:34 PM
  • Hi i already tried the -country but get the error:

    Set-ADUser : A value for the attribute was not in the acceptable range of values

    That is because the attribute for country/region  is co and can't be set by set-aduser

    http://technet.microsoft.com/en-us/library/ee617215.aspx

    That's why i tried to do the replace co=$_.Countrycode

    Thanks

    Tuesday, September 11, 2012 12:38 PM
  • If the 'country' attribute is the 2-character ISO code, then what is 'countrycode'?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Attribute c = Company for set-aduser, attribute co - Name

    c - FR

    co - France

    Tuesday, September 11, 2012 12:40 PM
  • Hi, i now changed it to 'question' sorry about that.


    Tuesday, September 11, 2012 12:40 PM
  • Hi, there are 3 attributes for country c, co, country

    none of them work with set-aduser to update the country/region field in aduc

    Tuesday, September 11, 2012 12:43 PM
  • To set Co(France):

    $result | foreach-object { Set-ADUser $_.username  -replace  @{co="$($_.country)"}}


    • Edited by Kazun Tuesday, September 11, 2012 12:44 PM
    Tuesday, September 11, 2012 12:44 PM
  • Hi i just tried it, the script doesn't show any errors but it doesn't update the attribute.

    Regards

    Tuesday, September 11, 2012 12:51 PM
  • Hi i just tried it, the script doesn't show any errors but it doesn't update the attribute.

    Regards


    Do you have only one DC?
    Tuesday, September 11, 2012 12:55 PM
  • Hi, i have multiple DCs but i check in the DC that the script server is connected to

    I also delete the City attribute, so when i run the script and it updates this attribute but not the country/region i know it has failed.

    Regards

    • Edited by slowscripter Tuesday, September 11, 2012 12:59 PM
    Tuesday, September 11, 2012 12:56 PM
  • Hi, i have multiple DCs but i check in the DC that the script server is connected to

    Show output:

    $result[0] | foreach-object { Set-ADUser $_.username  -replace  @{co=$_.country} -server  server1.contoso.com

    Get-ADUser $_.username -Prop co -server  server1.contoso.com | Ft name,co

    }


    Tuesday, September 11, 2012 12:59 PM
  • My mistake.  I made the assumption they'd have chosen a more intuitive naming convention for those attributes.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, September 11, 2012 1:06 PM
  • My mistake.  I made the assumption they'd have chosen a more intuitive naming convention for those attributes.


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "


    Is Kazun ever wrong?

    Grant Ward, a.k.a. Bigteddy

    Tuesday, September 11, 2012 1:35 PM
  • Hi Kazun,

    Output:

    Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting "Replace": "Object reference not set to an instance of an object."
    At E:\Scripts\adscript.ps1:74 char:63
    + $result[0] | foreach-object { Set-ADUser $_.username  -replace <<<<   @{co=$_.country} -server  myserver1}
        + CategoryInfo          : WriteError: (:) [Set-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADUser
     
    
    name                                                                                                               co                                                                                                                
    ----                                                                                                               --                                                                                                                
    surname , user1                                                                                                     France                                                                                                            

    So it seems that the co propery is updated with the value 'France' but it doesn't show in the ADUC

    Regards

    Tuesday, September 11, 2012 1:44 PM
  • Is Kazun ever wrong?

    Grant Ward, a.k.a. Bigteddy

    Possibly not.  But at this point it should be apparent that I am, which would seem to make me an odd choice to be asking for the answer to the question ;).

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

    Tuesday, September 11, 2012 1:45 PM
  • Hi Kazun,

    Output:

    Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting "Replace": "Object reference not set to an instance of an object."
    At E:\Scripts\adscript.ps1:74 char:63
    + $result[0] | foreach-object { Set-ADUser $_.username  -replace <<<<   @{co=$_.country} -server  myserver1}
        + CategoryInfo          : WriteError: (:) [Set-ADUser], ParameterBindingException
        + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADUser
     
    
    name                                                                                                               co                                                                                                                
    ----                                                                                                               --                                                                                                                
    surname , user1                                                                                                     France                                                                                                            

    So it seems that the co propery is updated with the value 'France' but it doesn't show in the ADUC

    Regards


    Wait replication or change server to myserver1(ADUC-Change Domain Controller). If you update only co attribute,but not attribute c(In tab Address - Country/Region would be empty)..
    Tuesday, September 11, 2012 1:55 PM
  • Hi, this is not a replication issue.

    If i update the c value to fr, then the country/region gets updated to france.

    But i think there is a way to just change the co value, but i am not sure how.. :(

    Tuesday, September 11, 2012 2:11 PM
  • The most annoying thing is that it can be done if i import the microsoft exchange module and use the command

    set-user $_.username  -countryOrRegion $_.Country

    But i rather not load more modules

    Tuesday, September 11, 2012 2:29 PM
  • He

    It's possible, but you need to update them all 3 at the same time, with one and the same command:

    set-aduser testuser-Replace @{c="BE";co="belgium";countrycode=59}

    Regards.

    Peter


    Peter Van Keymeulen, IT Infrastructure Solution Architect, www.edeconsulting.be

    Wednesday, June 17, 2015 2:38 PM
  • I'd just like to say that Peter's solution of using the one command works 100%.

    | +-- JDMils |

    Tuesday, December 15, 2015 10:05 PM
  • a small format issue. just used this command to full satisfaction:

    set-aduser -identity testaccount -streetaddress "Laanstraat 1" -City "Amsterdam" -postalcode "1000 AB" -State "NH" -Replace @{c="NL";co="Netherlands";countrycode=528}

    The address has been changed to protect the innocent

    Wednesday, January 10, 2018 10:57 AM