Dynamic logging

Welcome Forums General PowerShell Q&A Dynamic logging

This topic contains 3 replies, has 3 voices, and was last updated by

11 months, 4 weeks ago.

  • Author
  • #89762

    Points: 1
    Rank: Member

    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.

  • #89833

    Points: 1,704
    Helping HandTeam Member
    Rank: Community Hero

    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...?

  • #89900

    Points: 320
    Helping Hand
    Rank: Contributor

    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.

  • #89915

    Points: 1
    Rank: Member

    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.

The topic ‘Dynamic logging’ is closed to new replies.