1

I'm doing something that seems pretty basic to me but is not working as expected.

If the script is run with the -WhatIf switch then $liveTest should be "Test". If the script is run with the -Live switch then $liveTest should be "Live".

However both switches are causing $liveTest to be "Test"

param (
    [CmdletBinding()]
    [Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
    [Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
    [Switch]
    $users,

    [Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
    [Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
    [Switch]
    $computers,

    [Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
    [Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
    [Switch]
    $all,

    [Parameter(Mandatory=$true)]
    [string]
    $days,

    [switch]
    $console,

    [Parameter(Mandatory = $true, ParameterSetName = 'AllTest')]
    [Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyTest')]
    [Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyTest')]
    [switch]
    $WhatIf,

    [Parameter(Mandatory = $true, ParameterSetName = 'AllLive')]
    [Parameter(Mandatory = $true, ParameterSetName = 'UsersOnlyLive')]
    [Parameter(Mandatory = $true, ParameterSetName = 'ComputersOnlyLive')]
    [switch]
    $live
)
Process {

    # If -WhatIf or -Live switch is passed, creates a hashtable for the -WhatIf parameter.
    If($WhatIf) { 
        $whatIf = @{ WhatIf = $true }
        $liveTest = "Test"
    }
    ElseIf($live) { 
        $whatIf = @{ WhatIf = $false }
        $liveTest = "Live"
    }
    If($liveTest = "Test"){Write-Output $liveTest}
    elseif($liveTest = "Live"){Write-Output $liveTest}
}

enter image description here

3
  • 1
    You conditions are using assignment rather than comparison operators. If($liveTest = "Test") needs to become If($liveTest -eq "Test") Commented Feb 26, 2020 at 17:31
  • 1
    You didn't get even an error message while using wrong assignment operator! Commented Feb 26, 2020 at 17:58
  • I did not, I don't know if that's normal or not. Commented Feb 26, 2020 at 18:07

1 Answer 1

1

Your if and elseif conditions are using the assignment operator = rather than comparison operator -eq. As a result, $liveTest is getting set to Test on each run. Update your code to the following:

if ($liveTest -eq "Test") {
    Write-Output $liveTest
}
elseif ($liveTest -eq "Live") {
    Write-Output $liveTest
}

Since you are using if and elseif conditions to do variable assignment, $liveTest = "Test" always happens and $liveTest = "Live" never happens.

See About_Comparison_Operators for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

As always, thank you for taking the time to help me learn. I'll read the link you posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.