Author Posts

December 20, 2017 at 8:34 pm

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
    #New HomeDirectory Server
    #ProfilePath Server
    #New ProfilePath Server

        $UserList | ForEach-Object {
            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.

December 21, 2017 at 4:25 pm

I'm not following – I think your use of "input" may be confusing me.

Right now, you've got two If blocks; you're saying you want to condense that to one If block? Maybe if you describe the logic of what you're after, versus the code...?

December 22, 2017 at 8:55 am

Like, DonJ, I am a bit taken aback by the goal of the request.

Based on what you defined here. What you show is as concise as you are going to get. IMHO

Even if you tried to accomplish the final block you are looking for, you are just going to engender other If-Then, Case-Select or Try/Catch blocks prior to getting to that block. Thus defeating your end goal of 'condense' the amount of code.

I say this, as I did look at this and tried a few things which all resulted in adding code not condensing it, but hey, there are far smarter folks on this forum than I.

December 22, 2017 at 12:36 pm

Don – Yes I want to condense the 2 if statements into 1. So I want to only use 1 if block and somehow use the string HomeDirectory if the variables $HDServer and $New$HDServer were use. Then use the string ProfilePath if the variables $PPServer and $NewPPServer are used.

postanote – Yea I figured. I tried for about a week and couldn't figure out it would ever work. I was thinking I might be able to use ParameterSets, but couldn't figure it out. If it can't be done, I'm fine with that. Just wanted some expert input to see if it was possible.