I have a script that grabs all the users from an OU and then logs what the results. The output is like this:
User johnthomson's HomeDirectory is on Server Server1 johnthomson's HomeDirectory is \\Server1\johnthomson Changing johnthomson's HomeDirectory from \\Server1\johnthomson to \\Server2\johnthomson User johnthomson's ProfilePath is on Server Server3 johnthomson's ProfilePath is \\Server3\johnthomson Changing johnthomson's ProfilePath from \\Server3\johnthomson to \\Server4\johnthomson
Here’s the code (there’s more to this, but only this part I am concerned about):
$UserList=Get-ADUser -SearchBase -Filter * -SearchScope OneLevel -Properties HomeDirectory,ProfilePath #HomeDirectory Server $HDServer="Server1" #New HomeDirectory Server $NewHDServer="Server2" #ProfilePath Server $PPServer="Server3" #New ProfilePath Server $NewPPServer="Server4" $UserList | ForEach-Object { $Name=$_.Name $HomeDirectory=$_.HomeDirectory $ProfilePath=$_.ProfilePath If ($HomeDirectory -match $HDServer) { Write-Host "User $($Name)'s HomeDirectory is on Server $($HomeDirectory.Split('\\')[2])" Write-Host "$($Name)'s HomeDirectory is $($HomeDirectory)" Write-Host "New HomeDirectory Server is $NewHDServer" -ForegroundColor Green } If ($ProfilePath -match $PPServer) { Write-Host "User $($Name)'s ProfilePath is on Server $($ProfilePath.Split('\\')[2])" Write-Host "$($Name)'s ProfilePath is $($ProfilePath)" Write-Host "New ProfilePath Server is $NewPPServer" -ForegroundColor Green } }
Is there a way to make a template and dynamically input the text “HomeDirectory” when using the variables $HDServer and $NewHDServer. The same when using the variables $PPServer and $NewPPServer to input the text “ProfilePath”. I just want to condense the script if possible to something like this:
If ($variable -match $variable) { Write-Host "User $($Name)'s is on Server $($variable)" Write-Host "$($Name)'s is $($variable)" Write-Host "New $variable Server is $variable" -ForegroundColor Green }
I did try to put in a hashtable, but I couldn’t get it to work. I’m just not sure how to tackle this. Thanks.