change aduser attributes

Welcome Forums General PowerShell Q&A change aduser attributes

Viewing 8 reply threads
  • Author
    Posts
    • #282715
      Participant
      Topics: 9
      Replies: 6
      Points: 129
      Rank: Participant

      Hello,

      I need to change existing values in multiple aduser, attribute fields.
      Only when I run this script Powershell does not return a error, unfortunately it also does not change anything.

      I have a imported csv file, with adusers and a to be replaced /filled Atrribute field and its new value
      Example:

      Samaccountname : adm-test-RevV
      Displayname          : Revel Vin
      Description            : Admin account Servicedesk CTASK0902707
      Emailaddress        : [email protected]
      Company                : test-Company

      When  I use a set-aduser -replace on a test account it works fine.
      In the foreach loop is looks fine only it does not change any attribute

      I am not sure what I am missing at this moment.

      Since the company attribute field is empty, I use the -add instead of – replace.

      thx

    • #282727
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      There are a couple of things wrong.

      • It appears you want to process each row for a user. When you use the dot notation (e.g ($Users).samaccountname), that is creating an array of just samAccountNames. The only data you have in your loop is that sam, so you do not have the rest of the row (DisplayName, Description, etc.) for the user.
      • The Set-ADUser should be called one time, with the Server set to the DC you want to write to, otherwise it will be updating all over the place and you can’t validate until replication is complete.
      • The Set-ADUser replace is a hashtable with the AD Attribute you want to set and the value you want to set. The code you posted has a variable setting a variable
      • Recommend collecting the results. You have a row with the user data, so you can just append a ‘Status’ to capture success\issues so you can review what happened for each user
    • #282883
      Participant
      Topics: 9
      Replies: 6
      Points: 129
      Rank: Participant

      @Rob Simmers,

      Thank you for the reply, I will “investigate” this solution, and compare it with that what I did sofar.
      Copy paste is one thing, to see the difference and knowing why  is more my goal. 🙂

    • #283108
      Participant
      Topics: 5
      Replies: 10
      Points: 93
      Rank: Member

       

      • #283114
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        obijuan, what does it mean “…#Keys of user email address”

      • #284071
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        Thanks Obijuan, this code worked for me

    • #283111
      Participant
      Topics: 141
      Replies: 298
      Points: 526
      Rank: Major Contributor

      Rob, I have this same question. In your answer code, can I still use the “REPLACE” block if my Users’ (extension) attributes are blank?

    • #283258
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Jeff, Have you tried testing it on a single user with a single attribute? Key or lookup attribute to find the user.

      • #283315
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        @Rob, yes I’m trying with this code:

        ..but it’s balking that: “Get-ADUser : Variable: ‘sam’ found in expression: $sam is not defined.”

        My $Users variable is properly formatted and import-csv is working with it (for one test user)

    • #284005
      Participant
      Topics: 25
      Replies: 208
      Points: 790
      Helping Hand
      Rank: Major Contributor

      $results = foreach ( $user in $users ){
      #try to find the user
      $adUser = Get-ADUser $user.samaccountname }

      you have your aduser query outside of your foreach loop.
      as well, you don’t need to utilize the filter on your get-aduser statement, get-aduser accepts samaccountname for the identity value

      • #284056
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        David, even with these tweaks:

        ..I get the error:

        I do have value(S) inside of $adUser including one for SamAccountName (I’m testing with one User)

    • #284062
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      The users.csv contains users with a samaccountname? If it’s a single user, try wrapping $users in @() to ensure it’s an array:

      Note that I normally use a -Filter to find a user rather than -Identity to connect to a user. Filter you are checking to see if a result returned (e.g. If ($aduser)…) whereas -Identity you have to use a try\catch to capture the error if the user doesn’t exist.

      • #284383
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        yep, have samAccoutnName in csv and per your first suggestion, testing with one user.

        Revised code:

        Error still on both Get_ADUser with -identity (and -filter)

        I got my work done with Obijuan’s code but still trying to do same thing with yours for the sake of the education. Thanks

        • This reply was modified 1 week, 6 days ago by Jeff Taylor.
    • #284488
      Participant
      Topics: 17
      Replies: 1951
      Points: 3,986
      Helping Hand
      Rank: Community Hero

      Let’s do some basic 101 troubleshooting. Both errors indicate you are passing NULL or nothing to the parameters. The csv content has not been shared.

      I always highlight the code that is importing the csv and check it:

      Before I start writing code for Active Directory or anything else, I verify my for loop is getting my data:

      • #284692
        Participant
        Topics: 141
        Replies: 298
        Points: 526
        Rank: Major Contributor

        Yes, all that checks out on my end. I do Return the samAccountName from the $csv > $user variable.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.