Querying AD users and updating its attributes


Today a good friend and great to DBA, Fabricio França (@fabriciodba),  asked me if I could help him with a PowerShell script to save  a few hours of his work.

He needed it for a CSV file with user information from AD, and searching for the user to update some attributes.
I confess that my familiarity with AD is minimal, but after some research the Script had life.

His problem : Need to search the AD user by postOfficeBox (POBOX) and physicaldeliveryofficename (Office) and update the attributes title(Job Title)postalCode (Zip/Postal Code) and department .

His CSV is looking like : (of course it is not his data)

postOfficeBox,physicaldeliveryofficename,title,postalCode,department

00000001,AAAAAA,Técnico de Suporte,00,Infraestrutura

00000002,BBBBBB,Analista de Sistemas,01,Desenvolvimento

00000003,CCCC,,Analista de Telecom,02,Telecom

the code is :

 

$VerbosePreference= ‘Continue’

$Count = 0

Write-Verbose “Loading CSV “

Import-Csv c:\temp\Sample.csv |

Foreach {

       $UserInformation = $_

       Write-Verbose “`n”

       Write-Verbose “Loading information from postOfficeBox = $($UserInformation.postOfficeBox) and physicaldeliveryofficename = $($UserInformation.physicaldeliveryofficename) “

       $root = [ADSI]””           

       $search = [adsisearcher]$root           

       $search.Filter = “(&(objectclass=user)(objectcategory=user)(postOfficeBox=$($_.postOfficeBox))(physicaldeliveryofficename=$($_.physicaldeliveryofficename)))”            

       $search.FindOne() | foreach {

             Write-Verbose “Updating Information from User Name = $(($_ | select -ExpandProperty properties).displayname) postOfficeBox = $($UserInformation.postOfficeBox) and physicaldeliveryofficename = $($UserInformation.physicaldeliveryofficename)”

             Try {

                    $objUser = [ADSI]”$($_.path)”

                    $objUser.put(“title”,$($UserInformation.title)

                      $objUser.put(“postalCode”,$($UserInformation.postalCode))                              $objUser.put(“department”,$($UserInformation.department))

                    $objUser.setInfo()

                    $Count++

                    Write-Verbose “Information from User Name = $(($_ | select -ExpandProperty properties).displayname) postOfficeBox = $($_.postOfficeBox) and physicaldeliveryofficename=$($_.physicaldeliveryofficename) UPDATED SUCCESSFULLY !!!”

             } catch {

                    Write-Error $_.exception

             }

       }

 }

Write-Verbose “`n”

Write-Verbose “`n”

Write-Verbose “`n”

Write-Verbose ” Process Finished !!! Updated $($count) Users”

$VerbosePreference= ‘SilentlyContinue’

 

 

Fast, Clean and Simple..Classic PowerShell !!!

My sources:

http://www.powershellpro.com/powershell-tutorial-introduction/powershell-tutorial-active-directory/
http://richardspowershellblog.wordpress.com/2012/03/22/updating-ad-users-in-bulk/

 

#PowerShellLifeStyle

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL, Virtual Pass BR. Bookmark the permalink.

3 Responses to Querying AD users and updating its attributes

  1. Laerte, muito obrigado mesmo!!!

  2. Laerte, Muito obrigado Brother!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s