Lanefs
Lanefs

Reputation: 3

Powershell not passing Parameter to If Statement

I have this

Param (
    [Parameter(Mandatory=$true)]
    [ValidateSet('Yes','No')]
    [string]$ContinueSetup,
    [Parameter(Mandatory=$true)]
    [Validateset('yes', 'no')]
    [string]$InstallDropbox = 'Yes',
    [Parameter(Mandatory=$true)]
    [Validateset('yes', 'no')]
    [string]$InstallSlack,
    [Parameter(Mandatory=$true)]
    [Validateset('yes', 'no')]
    [string]$InstallOffice    
    )

if ($ContinueSetup -eq 'yes'){
    if ($InstallDropbox = 'yes'){
         write-host 'install dropbox'
             }
                else
                {write-host 'dropbox not selected'}    
    if ($InstallSlack = 'yes'){
        write-host 'install slack'
            }
                else
                {write-host 'slack not selected'}
    if ($InstallOffice = 'yes'){
        write-host 'install office'
            }
                else
                {write-host 'Office not selected'}
}
if ($continuesetup -eq 'no') {write-host 'no setup'; break}

It asks for my parameters as I want, but doesn't pass the parameters on, instead it just sets them all to 'yes'. Are parameters inherent? How should I set this so that it stops at each if statements, checks the parameter and does one of two actions, yes/no, and then moves onto the next if statement?

Upvotes: 0

Views: 382

Answers (4)

Glenn
Glenn

Reputation: 1855

I'd also recommend using [switch] instead of [bool] for your parameter types and not specifying $true as the default. SwitchParameter parameters make invocation simplier and more straightforward.

Param (
    [switch]$Setup,
    [switch]$NoDropboxInstall,
    [switch]$NoSlackInstall,
    [switch]$NoOfficeInstall
)

if ($Setup) {
    "NoDropboxInstall: $NoDropboxInstall"
    "NoSlackInstall: $NoSlackInstall"
    "NoOfficeInstall: $NoOfficeInstall"
}
if (!$Setup) { 
    write-host 'no setup'
    return
}

Invoking the script is simpler:

.\parameters.ps1 -Setup -NoDropboxInstall 

Output:

NoDropboxInstall: True
NoSlackInstall: False
NoOfficeInstall: False

Upvotes: 1

David Mayo
David Mayo

Reputation: 53

As others have said, the bool type is designed for this sort of thing. Since they're only true or false, you don't have to include equality statements, which makes the code much more readable. Instead of

if( $InstallSlack -eq 'yes' )  # Note the -eq comparison is used in Powershell, not '=' or '=='

with a bool, you can just write

if( $InstallSlack )

which I think is much clearer.

To do the opposite, you put a -not in front of the bool:

if( -not $InstallSlack )

Here's your code rewritten with bool's (and with me changing the formatting a bit by adding some whitespace and aligning the code block indentation).

Param(
    [Parameter(Mandatory=$true)]
    [bool]$ContinueSetup,

    [Parameter(Mandatory=$true)]
    [bool]$InstallDropbox = 'Yes',

    [Parameter(Mandatory=$true)]
    [bool]$InstallSlack,

    [Parameter(Mandatory=$true)]
    [bool]$InstallOffice    
)

if ($ContinueSetup){
    if ($InstallDropbox){
         write-host 'install dropbox'
    }
    else{
        write-host 'dropbox not selected'
    }  

    if ($InstallSlack){
        write-host 'install slack'
    }
    else {
        write-host 'slack not selected'
    }

    if ($InstallOffice){
        write-host 'install office'
    }
    else {
        write-host 'Office not selected'
    }
}
else{
    write-host 'no setup'
    break
}

Bools are a very powerful programming concept. In Powershell, they can be combined/modified with -not, -or, -and, and a few others. This link has the details.

(Just for completeness, I'll note that some people (including me) don't like seeing [bool] parameters in Powershell. There's something called a [switch] parameter that can be used instead, but I wouldn't worry about it too much. Here's a link.)

Upvotes: 1

js2010
js2010

Reputation: 27606

Yes, an assignment statement can be an expression. Occasionally useful, but confusing. Any non-null result will be true.

if ($a = 'yes') {'yes'}
yes

if ($a = 'no') {'yes'} 
yes

if ($a = '') {'yes'}  
# nothing

Upvotes: -1

Daniel Mann
Daniel Mann

Reputation: 59055

Comparison in PowerShell is done using -eq, not =.

Also, do not make parameters yes and no. There is already a built-in type that's immune to typos: bool. $true and $false are what you're after

Upvotes: 1

Related Questions