passing array of strings into function


im trying pass array of stings function function not seem able see elements in array. here function , how called:

function printstuff
{
    param($v1, $v2, [string[]]$arr)

    foreach($s in $arr)
    {
    echo $s
    }
}

$a = @("aa","bb","cc")
printstuff("a","b",$a)

 

i assumed foreach loop go through elements in array , print them out outputs blank. doing wrong here?

change function this

function printstuff
{
   param($v1, $v2, [string[]]$arr)
    write-host 'write: $arr'
   foreach($s in $arr)
   {
   write-host $s
   }
   write-host 'write: $v1'
   write-host $v1
   write-host 'write: $v2'
   write-host $v2
}

now check this

ps c:\> printstuff "a" "b" $a
write: $arr
aa
bb
cc
write: $v1
a
write: $v2
b
ps c:\> printstuff ("a","b",$a)
write: $arr

write: $v1
b aa bb cc
write: $v2

maybe see whats happening...
when call function toss stuff @ (not formal typical programming langs)

so when use () grouping data , getting sent first var in function list...



Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Group Policy Event ID 1058 Error Code 1326 (The user name or password is incorrect)

Suspicious event log Event ID: 4905

DCOM received error "2147746132" from...