Array parameter in a powershell function does not load the first element -
i have function takes 1 string parameter, , 1 array of string parameter
function w { param([string]$format, [string[]]$args) write-host $format write-host $args }
then execute function in following way:
w "formatparam" "args1" "args2" w "formatparam" "args1" "args2" "args3" w "formatparam" "args1" "args2" "args3" "args4" w "formatparam" "args1" "args2" "args3" "args4" "args5"
as can seen in output, string array not seem load first parameter, meant go array:
formatparam args2 formatparam args2 args3 formatparam args2 args3 args4 formatparam args2 args3 args4 args5
any ideas?
$args special
$args
special variable in powershell; shouldn't declaring parameter because you're overriding it. let's name else, $remaining
.
arrays use commas
the way you're passing arguments in (with spaces) makes them different parameters. pass array you'd have call this:
w "formatparam" "args1" "args2","args3","args4","args5"
with commas. since that's not want, need rewrite function.
remaining arguments
function w { param( [string]$format, [parameter( valuefromremainingarguments=$true ) [string[]] $remaining ) write-host $format write-host $remaining }
and should able call so:
w "formatparam" "args1" "args2" "args3" "args4" "args5"
Comments
Post a Comment