Hi Team
I have found a script from a poshcode user called Themoblin and as he has posted here I thought this was a good place to ask for some advice. Being new to Powershell but ultimately keen to learn I grabbed a script to change a users profile and home drive server details inside AD when migrating servers.
I added a section to search only in one OU (we have 20,000 user objects and the AD OU structure is very flat…) but I would now like to find out how to get it to only act on the users that need the old server name changed Here is the script as it stands now
Import-Module Activedirectory
#Lower Case!!
$oldserver = “oldserver”
#Lower Case!!
$newserver = “newserver”
$users = get-aduser -filter * -SearchBase “OU=xx,OU=xx,DC=xx,DC=xx,DC=xx,DC=xx,DC=xx” -properties homedirectory,profilepath
foreach ($user in $users) {
$name = $user.name
$DN = $user.distinguishedname
$profilepath = $user.profilepath
$homedirectory = $user.homedirectory
if ($profilepath -like "*$oldserver*")
{
#Add -whatif to next line before running the script to see what would happen
Set-Aduser $DN -profilepath $profilepath.ToLower().replace($oldserver,$newserver)
}
else {
Write-Host -foregroundcolor RED “User $name does not have roaming profile on $oldserver”
}
if ($homedirectory -like "*$oldserver*")
{
#Add -whatif to next line before running the script to see what would happen
Set-Aduser $DN -homedirectory $homedirectory.ToLower().replace($oldserver,$newserver)
}
else {
Write-Host -foregroundcolor RED “User $name does not have a Home directory on $oldserver”
}
}
What I am thinking is one of 2 things, both I am struggling with what to actually search for to help me get the desired result.
1st Thought: Filter the first array ($users) further to only pass through users whose homedirectory or profilepath contain $oldserver
2nd Thought: Pre-export all users with this detail and break up into smaller files, and feed in a CSV/text file as the input to allow for a ‘batch processing’ mode
We have some sites with 10 users on low bandwith, to larger hospital sites with a few thousand users who will need to be migrated. Option 2 may be the better method of getting this done, but as I said, quite new to powershell and it been quite silly of me to have taken this long to get a real interest in it.
Any advice on where to start, or if someone can explain how to get what I’m after, so that I can try to learn how to do it myself? If this is simple then you can guess what sort of a newbie I am to all of this