16

I've been looking all over, and i cant seem to find the way to make this work the way i want it to.

I'm new to PS Parameters and have something i think should be fairly simple...I want to be able to run a script and detect whether or not the switch is present. Such as:

myscript.ps1 -Deploy

-or-

myscript.ps1

Code, that i've been playing with:

function start-script {
    param (
        [parameter()]
        [switch]$Deploy
    )
    if ($Deploy.IsPresent) {
        Write-Host "True"
    }
    else {
    Write-Host "False"
    }
}
    
start-script

Running, this doesnt return any errors, but does not output the results of the write-host either. It basically does nothing. What am i doing wrong, and how would i get the correct outputs?

Thanks. M

EDIT: Adding Screenshot of what i am experiancing, since a few have said it should work or work with removing the .isPresent

enter image description here

6
  • 1
    Your function works just fine, not sure exactly what's the problem. Are you calling your ps1 file from CLI? like .\myscript.ps1 -Deploy ? Commented Jun 20, 2021 at 17:03
  • I am. I started calling it from Powershell rather than the PS ISE. And it seems to be returning False no matter what now. .\testParam.ps1 -Deploy False .\testParam.ps1 False Commented Jun 20, 2021 at 17:12
  • 1
    If you want to determine whether the parameter value was specified in the function call you can inspect the keys in the $PSBoundParameters variable - e.g. $PSBoundParameters.ContainsKey(“Deploy”). If you just want to know if the caller wants the switch to be enabled or not you can simply use $Deploy - Powershell will assign the default value if the user doesn’t specify it in the upstream call. Commented Jun 20, 2021 at 17:15
  • 1
    To assign a value to a switch parameter when calling a function or script you use a slightly different syntax to regular parameters - if you want it to be false you can just omit it, and if you want it to be true you just say -Deploy. You can also do -Deploy:$true or -Deploy:$false or even -Deploy:$myBoolVariable Commented Jun 20, 2021 at 17:19
  • 1
    Remove the ispresent. A switch parameter is to return a Boolean if specified so nothing else should be needed: if ($Deploy){.... See this answer: stackoverflow.com/a/57784149/14903754 Commented Jun 20, 2021 at 17:24

2 Answers 2

19

Makes sense, this is because in your function call (the last line of your script) you're never giving the function any argument. If you want to pass an argument to your .ps1 script from an outside source you should add a param(...) block at the top of your script. I'm following with the code you already have but, in this case, I don't think a function is needed at all.

Code could be simplified to this:

param (
    [switch] $Deploy
)

if ($Deploy) {
    Write-Host "True"
    return
}

Write-Host "False"

But if you want to use a function, you can leverage $PSBoundParameters as long as your function param block matches your script's one:

param (
    [switch] $Deploy
)

function Start-Script {
    param (
        [switch] $Deploy
    )
     
    if ($Deploy) {
        Write-Host "True"
        return
    }

    Write-Host "False"
}

Start-Script @PSBoundParameters
  • From PowerShell:
PS /> .\script.ps1
False

PS /> .\script.ps1 -Deploy
True
  • From CMD:
D:\> powershell -File script.ps1
False

D:\> powershell -File script.ps1 -Deploy
True

Edit

To add a bit more context to the answer:

  • [switch] $Deploy only exist in the scope of your function this is why we add the param(...) block at the top of the script too, so that the function can be invoked with the same arguments as you have invoked the .ps1 script.

  • Splatting with $PSBoundParameters can work with multiple functions.

Example:

param (
    [switch] $Deploy,
    [string] $Name
)

function Start-Script {
    param (
        [switch] $Deploy
    )

    "Deploy switch is: {0}" -f $Deploy.IsPresent
}

function SayHello {
    param(
        [string] $Name
    )

    "Hello $Name!"
}

# This is what mclayton was referring to in their comment.
# If the Argument $Name is called:
if ($PSBoundParameters.ContainsKey('Name')) {
    # Call the function
    SayHello @PSBoundParameters
}

Start-Script @PSBoundParameters
  • From PowerShell:
PS /> .\script.ps1 -Name world -Deploy
Hello world!
Deploy switch is: True
Sign up to request clarification or add additional context in comments.

Comments

3

Even simpler:

function Test {
param (
    [switch]$Deploy
)
$Deploy.IsPresent
}

enter image description here

Comments

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.