MuttsNutts Posted August 18, 2016 Posted August 18, 2016 Hi I'm migrating both our staff and students to new file servers for there home drives, now I've done the data migration via robocopy and seen as all our students logon names match the home folder name I simply did the change on mass in AD by selecting all students - properties - profile - ticking home folder box - selecting the required drive letter and typed in \\newservername\student$\%username% bang job done works a treat. Now the issue I have is our staff, as most of the usersnames did match home folder names at one point - but some get married and change names and then theres the old old staff that have been here donkeys years have completely different usernames to home folder name ratio - so I can't on mass change in AD as I would have a lot of staff not being able to connect or find their data. so searching around I came across the very thing that will do the job for me via powershell: Import-Module ActiveDirectory $oldServerName = "twenty" $newServerName = "thirty" [array]$AllUsers = Get-ADUser -Filter * -Properties HomeDirectory foreach($user in $AllUsers){ if(($user.HomeDirectory.ToString()).contains($oldServerName)){ $homeDirectory = ($user.HomeDirectory.ToString()) -replace $oldServerName, $newServerName Set-ADUser $user.DistinguishedName -HomeDirectory $homeDirectory } } Taken from https://community.spiceworks.com/topic/289126-how-can-i-change-only-the-servername-in-the-home-folder-path-in-ad now editing the server names and running this does in fact just change the server name but it rips through all users in the domain yes even students (ok yea I could run this and then once its done manually on mass change students again using the method at the top) but that's messy and not a way I'd like to do it. what I'm asking is I'm looking to target OU's in AD mainly the Staff OU or even sub OU's (so departments ) so far I have got it to this to with some searching and help from another tech in my dept Import-Module ActiveDirectory $oldServerName = "-OLDSERVER" $newServerName = "NEWSERVER" [array]$AllUsers = Get-ADUser -LDAPFilter '(name=*)' -SearchBase "OU=StaffTest,DC=MYDOMAIN,DC=ac,DC=uk" -Properties HomeDirectory foreach($user in $AllUsers){ if(($user.HomeDirectory.ToString()).contains($oldServerName)){ $homeDirectory = ($user.HomeDirectory.ToString()) -replace $oldServerName, $newServerName Set-ADUser $user.DistinguishedName -HomeDirectory $homeDirectory } } Obviously it doesn't work as is Now if I manually run Get-ADUser -LDAPFilter '(name=*)' -SearchBase "OU=StaffTest,DC=MYDOMAIN,DC=ac,DC=uk" -Properties HomeDirectory it does give me the details for just the users in that OU StaffTest I think the issue is with the Array? so this: [array]$AllUsers = and this: foreach($user in $AllUsers){ I'm not very good at powershell its a learning curve for me so anyone that can master this I'd really appreciate this or if not find me another method I can just change the server name on mass via OU selection as I have around 500 staff users to change this for. Thanks
Sephiroth Posted August 18, 2016 Posted August 18, 2016 I'm assuming that the space in "$oldS erverName" is not intentional? If you take that out it should run, I believe.
MuttsNutts Posted August 18, 2016 Author Posted August 18, 2016 (edited) Oh well time is a great problem solver I've only gone and done it myself - can't believe I didn't spot it earlier. Me using LDAPfilter (well actually suggested by my colleague) going to punch them now Anyway this is the working one that does target OU's (Note it also does sub OU's so I can now just aim this at my OU called Staff and it will do all sub OU's under it) !!!!!!! BIG NOTE HERE: It is however case sensitive on the server names so if you have some users with mixed upper and lower case letters set as the current server name it wont touch those so it's probably worth checking maybe an AD export to CSV just to check or make multiple scripts with all the varied cases LOL Import-Module ActiveDirectory $oldServerName = "OLDSERVER" $newServerName = "NEWSERVER" [array]$AllUsers = Get-ADUser -Filter * -SearchBase "OU=Staff,DC=MYDOMAIN,DC=ac,DC=uk" -Properties HomeDirectory foreach($user in $AllUsers){ if(($user.HomeDirectory.ToString()).contains($oldServerName)){ $homeDirectory = ($user.HomeDirectory.ToString()) -replace $oldServerName, $newServerName Set-ADUser $user.DistinguishedName -HomeDirectory $homeDirectory } } Thanks for looking Sorry Sephiroth I'm assuming that the space in "$oldS erverName" is not intentional? If you take that out it should run, I believe. Yes the space is a mistake - no idea how I managed to get a space in there I only C & P Edited August 18, 2016 by MuttsNutts
MuttsNutts Posted August 18, 2016 Author Posted August 18, 2016 (edited) It did it again it put a space in ???? must be when I quote the script here it is in plain text Import-Module ActiveDirectory $oldServerName = "-OLDSERVER" $newServerName = "NEWSERVER" [array]$AllUsers = Get-ADUser -LDAPFilter '(name=*)' -SearchBase "OU=StaffTest,DC=MYDOMAIN,DC=ac,DC=uk" -Properties HomeDirectory foreach($user in $AllUsers){ if(($user.HomeDirectory.ToString()).contains($oldServerName)){ $homeDirectory = ($user.HomeDirectory.ToString()) -replace $oldServerName, $newServerName Set-ADUser $user.DistinguishedName -HomeDirectory $homeDirectory } } EDIT: it still did it in plain text HA HA can't edit an image can you lol Edited August 18, 2016 by MuttsNutts
Arthur Posted August 18, 2016 Posted August 18, 2016 (edited) It did it again it put a space in ???? must be when I quote the script here it is in plain text Edited August 18, 2016 by Arthur Smaller picture :)
MuttsNutts Posted August 18, 2016 Author Posted August 18, 2016 Oh yea thanks, Doh me not used a forum for a while lol
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