Why do I need to use 'parametersetname' with a PowerShell switch? -


my question parameter field 'parametersetname' , mysterious need included.

i have powershell (v3) script called test.ps1:

param([parameter(parametersetname="anything")][switch]$foo=$false) $foo.ispresent 

if run in powershell ide cmd line this:

.\test.ps1 

i get

> false 

whereas calling

.\test.ps1 -foo 

i

> true 

both good. has taken me while (ages) realise switch work, must add descriptor "parametersetname".

because if repeat test, omit "parametersetname" this:

param([parameter][switch]$foo=$false) $foo.ispresent 

when run this:

.\test.ps1 

i error:

cannot process argument transformation on parameter 'foo'. cannot convert "false" value of type "system.management.automation.switchparameter" type "system.management.automation.parameterattribute".

whereas calling

.\test.ps1 -foo 

i

missing argument parameter 'foo'. specify parameter of type 'system.management.automation.parameterattribute' , try again.

does know why?

what magic 'parametersetname' doing?

you don't need use parametersetname, need brackets after parameter.

if omit brackets seems powershell reading type information parameter , trying cast parameter type, instead of treating parameter attribute.

param([parameter()][switch]$foo=$false) 

works

param([switch]$foo=$false) 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -