简体   繁体   中英

Foreach-Object items to an array, not a sub-array

So, I have this script I've been developing, I've changed around from pscustom objects, to hashtables, to splatted arrays, and to simple csv arrays, i've messed around with standard loops, do until, mixing standard loops with foreach-object, and honestly, there is an easy way to solve this problem, but i'd really prefer it not be the solution I use, lol.

So what's happening is, this variable will LOOK like it's outputting to to the correct format if I encapsulate the % loop in parenthesis... but in order to select the first or second string with the output there, I have to use 2 sets of brackets instead of just one. so for instance, $vm[0][0], $vm[0][1]

not entirely sure this method will work, but I've been trying some crazy stuff.

$vm = ( $d[1] , $l[1] | % { $_ + '\VM\VMWare' } ) , '\setup.exe' , '\setup64.exe' , '/s /v "/qn reboot=r"'

In order not to be jagged array, enclose the whole in @() . And use a semicolon as a separator.

$vm = @($d[1],$l[1] | % { $_ + '\VM\VMWare' }; '\setup.exe', '\setup64.exe', '/s /v "/qn reboot=r"')

or

$vm = @(
    $d[1],$l[1] | % { $_ + '\VM\VMWare' }
    '\setup.exe', '\setup64.exe', '/s /v "/qn reboot=r"'
)

Another way is to add one array to the other.

$vm = @($d[1],$l[1] | % { $_ + '\VM\VMWare' }) + '\setup.exe', '\setup64.exe', '/s /v "/qn reboot=r"'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM