VMware Cloud Community
tdubb123
Expert
Expert
Jump to solution

param with array

is it possible to specify an array in param

script.ps1 $vmhosts ?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Same reason the following is not listed in a column.

$vmhost = 'test1','test2','test3','test4'

Write-Host $vmhost

That is the way the PS output engine formats an array of strings.

If you want them on separate lines, you could do

$vmhost = 'test1','test2','test3','test4'

Write-Host ($vmhost -join "`n")


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Of course, see this snippet for example.
Same goes for a script in a .ps1 file, define the param as an array ([PSObject[]]$VMHost for example)

function Do-Something

{

   param(

   [int[]]$Array

   )

 

   0..$Array.Count | % {

   $Array[$_]

   }

}


Do-Something -Array 1, 2, 3


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

thanks luc

any idea why write-host doesnt list them in column?

function Do-Something

{

param(

[PSObject[]]$VMHost

)

write-host $vmhost

}

$vmhost contents is

test1

test2

test3

test4

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Same reason the following is not listed in a column.

$vmhost = 'test1','test2','test3','test4'

Write-Host $vmhost

That is the way the PS output engine formats an array of strings.

If you want them on separate lines, you could do

$vmhost = 'test1','test2','test3','test4'

Write-Host ($vmhost -join "`n")


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
tdubb123
Expert
Expert
Jump to solution

thanks

0 Kudos