Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: How do I pass variables to switch parameters when invoking powershell at the command line?

Normally, if you want to defer the specification of a switch parameter to some variable, you can pass an expression to the switch parameter, as seen with the WhatIf parameter.

test.ps1  param ( [string] $source, [string] $dest, [switch] $test ) Copy-Item -Path $source -Destination $dest -WhatIf:$test 

This allows you great flexibility when working with switches. However, when you call powershell with cmd.exe or something, you wind up with something like this:

D:\test>powershell -file test.ps1 -source test.ps1 -dest test.copy.ps1 -test:$true  D:\test\test.ps1 : Cannot process argument transformation on parameter 'test'. Cannot convert value "System.String" to type "System.Manageme nt.Automation.SwitchParameter", parameters of this type only accept booleans or  numbers, use $true, $false, 1 or 0 instead. At line:0 char:1 +  <<<<     + CategoryInfo          : InvalidData: (:) [test.ps1], ParentContainsError    RecordException     + FullyQualifiedErrorId : ParameterArgumentTransformationError,test.ps1 

However, the same result appears when passing -test:true, and -test:1. Why doesn't this work? Shouldn't Powershell's type conversion system automatically recognize these strings as being convertible to bool or switch, and convert them?

Does this mean that when calling powershell scripts from some other system (such as a build system) it's necessary to construct complex flow control structures to determine whether or not to include a switch in the command string, or omit it? This seems tedious and error prone, which leads me to believe it's not the case.

like image 685
bwerks Avatar asked Jul 06 '12 18:07

bwerks


People also ask

How do you pass arguments to invoke in PowerShell?

Invoke-Command uses the ScriptBlock parameter that defines two variables, $param1 and $param2 . Get-ChildItem uses the named parameters, Name and Include with the variable names. The ArgumentList passes the values to the variables.

How do I create a switch parameter in PowerShell?

To create a switch parameter in a function, specify the switch type in the parameter definition. Switch parameters are easy to use and are preferred over Boolean parameters, which have a less natural syntax for PowerShell. For example, to use a switch parameter, the user types the parameter in the command.

How do you pass parameters to invoke expressions?

The only parameter Invoke-Expression has is Command . There is no native way to pass parameters with Invoke-Expression . However, instead, you can include them in the string you pass to the Command parameter.

How do I pass multiple command line arguments in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.


1 Answers

This behaviour has been filed as a bug on connect. This is a workaround:

powershell ./test.ps1 -source test.ps1 -dest test.copy.ps1 -test:$true 
like image 175
jon Z Avatar answered Sep 27 '22 23:09

jon Z