Switch Parameter

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
jasonv
Posts: 49
Last visit: Thu Sep 01, 2022 5:40 am

Switch Parameter

Post by jasonv »

Hi;

I created a script that uses [switch] within the Parameters statement and it does not work when built as an .EXE file. So I tested this using a script directly from your blog and verified that there appears to be an issue.

Here is the blog link ...

https://www.sapien.com/blog/2015/11/30/ ... able-file/

Here is the script ...

Param
(
[Parameter(Mandatory = $true)]
[String]$Word,

[int]$Number = 1,

[Switch]$AddSpace
)

$space = ''
if ($AddSpace){ $space = ' ' }

1..$Number | foreach { "$Word$space" * $_}


Here are results ...

PS H:\Scripts\PSScripts\Testing> .\Parameters.exe -Word PowerShell -Number 3 -AddSpace
PowerShell
PowerShellPowerShell
PowerShellPowerShellPowerShell

PS H:\Scripts\PSScripts\Testing> .\Parameters.ps1 -Word PowerShell -Number 3 -AddSpace
PowerShell
PowerShell PowerShell
PowerShell PowerShell PowerShell

Any assistance would be appreciated.

Thanks. - Jason
User avatar
jasonv
Posts: 49
Last visit: Thu Sep 01, 2022 5:40 am

Re: Switch Parameter

Post by jasonv »

My apologies ... never mind it appears this this is known behavior. I need to replace switch value with string values. I wasn't reading carefully. The original editor I was using required a -Arguments parameter to pass all parameters.

Thanks - Jason
User avatar
dan.potter
Posts: 709
Last visit: Wed Nov 14, 2018 11:39 am

Re: Switch Parameter

Post by dan.potter »

does this work?

(1..3 | foreach {'powershell'}) -join ' '
This topic is 7 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked