powershell - Positional parameter binding with values starting with a minus/hyphen -


here powershell test script:

param(     [parameter(mandatory=$true,               position=0)]    [int]$val ) write-output $val; 

now want call this:

powershell -executionpolicy bypass -file ".\testscript.ps1" "-1" 

the error is:

a parameter cannot found matches parameter name '1'.

note have no control on way application calls script (i.e. can not make application call script -val:-1). binding has positional. how can make script bind -1 $val param block?

if can't fix how application calls script other option see drop named parameters entirely , use $args collection instead:

c:\>type test.ps1 [int]$val = $args[0] write-output $val  c:\>powershell -executionpolicy bypass -file test.ps1 -1 -1

by casting argument [int] type safety still enforced:

c:\>powershell -executionpolicy bypass -file test.ps1 a cannot convert value "a" type "system.int32". error: "input string not in correct format." @ c:\users\cobalt\documents\test\test.ps1:1 char:1 + [int]$val = $args[0] + ~~~~~~~~~~~~~~~~~~~~     + categoryinfo          : metadataerror: (:) [], argumenttransforma...     + fullyqualifiederrorid : runtimeexception 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -