tech-man Posted January 19, 2016 Posted January 19, 2016 Hello Could someone help me with a Powershell script? I need to update/replace job titles in AD from a CSV. The format of the CSV is: username, jobtitle. Thanks
Davit2005 Posted January 19, 2016 Posted January 19, 2016 Hello Could someone help me with a Powershell script? I need to update/replace job titles in AD from a CSV. The format of the CSV is: username, jobtitle. Thanks Bit rough and ready but here it is, should work. # Start of Script Import-Module ActiveDirectory Set-ExecutionPolicy -ExecutionPolicy Bypass $rows = import-csv "IMPORT_FILE_PATH.csv" # Uses Active Directory module for Windows Power Shell ForEach ($row in $rows) { $SamAccountName = $row.username $title = $row.jobtitle Set-ADUser -Identity $SamAccountName -Title $title } # End of Script 1
tech-man Posted January 19, 2016 Author Posted January 19, 2016 Not having much luck getting this to work? Powershell frustrates me!
Davit2005 Posted January 19, 2016 Posted January 19, 2016 Open a powershell prompt and run the script from within there. Post any error messages. Also try and set Powershell Execution policy to RemoteSigned https://technet.microsoft.com/en-gb/library/hh849812.aspx
tech-man Posted January 19, 2016 Author Posted January 19, 2016 No errors - just doesn't replace what is already in the job title field. Thanks
Davit2005 Posted January 19, 2016 Posted January 19, 2016 Are you refreshing the page? have you more than 1 DC?
tech-man Posted January 19, 2016 Author Posted January 19, 2016 Yes - refreshing page & yes more than one DC.
cheesits Posted January 19, 2016 Posted January 19, 2016 hey techman, davit's script worked fine for me in a test enviro so should work fine. Not wanting to state the obvious but have you changed the $rows = import-csv "IMPORT_FILE_PATH.csv" to point to the location of the csv? eg $rows = import-csv "c:\temp\staff.csv" 1
dapaulio Posted January 19, 2016 Posted January 19, 2016 (edited) As good as powershell is sometimes it can be like hitting a nail with a sledgehammer. I have a dsmod command script that does similar but changes the password but i have included the switch for the "office" field in AD (i assume this is the field you mean to change) ChangeUserDetails.cmd for /f "eol=; Tokens=1,2,3,4,5,6,7,8 Delims=," %%a in (%1) do ( dsmod user "CN=%%a,OU=removed,OU=removed,OU=removed,OU=removed,OU=removed,DC=removed,DC=removed,DC=removed,DC=removed,DC=removed" -upn %%a -display %%b -samid %%a -desc %%c -fn %%d -ln %%e -office %%g -pwd %%f -mustchpwd no ) [/Code] ChangeUserDetails.CSV format [Code] User,Display name,Description,First Name, Last Name, Password, Office [/Code] Adapt your csv and script to suit in elevated cmd prompt [Code] ChangeUserDetails.cmd ChangeUserDetails.csv [/Code] EDIT: Subsititute the -office switch with -Title. didnt see the job title field first time i looked Edited January 19, 2016 by dapaulio
Davit2005 Posted January 19, 2016 Posted January 19, 2016 Are you running this on the AD server or local machine. We run ours on an Exchange server but it works OK. We only do this as our create account script runs on the Exchange server and wanted to keep them together. Just strange how you are not getting an error message TBH. Are you running RM or Vanilla network??
tech-man Posted January 19, 2016 Author Posted January 19, 2016 Thanks - got it sorted. Didn't have headers in the CSV. 1
HPlum78 Posted January 26, 2016 Posted January 26, 2016 Did you know you can use short hand ForEach in ps so instead of:- ForEach ($row in $rows) { you can use the following:- $rows | %{ Hope this helps (or at least saves you guys some key presses!)
Davit2005 Posted January 26, 2016 Posted January 26, 2016 A few key presses shorter :-) . However I like been descriptive in code and using the ForEach it is a bit obvious what the coding does.
HPlum78 Posted January 26, 2016 Posted January 26, 2016 I suppose if you don't code much and don't comment your code foreach would be easier. A bit like where-object and ? if you like typing you use the first if you code for a living you use the latter! :-P 1
halbaradkenafin Posted January 26, 2016 Posted January 26, 2016 Did you know you can use short hand ForEach in ps so instead of:- ForEach ($row in $rows) { you can use the following:- $rows | %{ Hope this helps (or at least saves you guys some key presses!) Important note: best practice is to never use an alias in a script. It might save a few key presses but makes it less readable and assumes that anyone else who reads it will know what it means. Save the alias use for the command line where you are just wanting to get the results quickly.
HPlum78 Posted January 27, 2016 Posted January 27, 2016 (edited) Indeed you are correct best practice does say that code being shared in the code repository should not use the alias name to make them readable. But for internal use stuff that is well commented there is no issue with their use. I automate using PS for a living and churn out scripts and functions for various tasks daily just trying to share some PS Love. oh this little tool can help Edited January 27, 2016 by HPlum78 link 1
Davit2005 Posted January 27, 2016 Posted January 27, 2016 Indeed you are correct best practice does say that code being shared in the code repository should not use the alias name to make them readable. But for internal use stuff that is well commented there is no issue with their use. I automate using PS for a living and churn out scripts and functions for various tasks daily just trying to share some PS Love. oh this little tool can help Agree, there are certainly a lot of code available but when it is un-commented and using aliases as a new comer it is quite difficult to learn what is doing what especially when some of the coding does not even work. At the end of the day probably shouldn't be running any old random untested code that you picked off the internet straight on wide scale on production servers anyway.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now