Foreach里面的哈希表与powershell

时间:2018-01-18 14:45:51

标签: powershell foreach hashtable

有没有办法在哈希表中插入foreach循环。

这样的东西?

$vms = get-vm
foreach ($vm in $vms) {
   $disks=Get-Vhd  $vm.id
   $hash = [ordered]@{
        'VM<br>Name' =  $vm.vmname 
         'State' = $vm.state
         'Disk' = foreach ($disk in $disks) {$disks.size -join '.' }

    }
New-Object -TypeName PSObject -Property $hash

1 个答案:

答案 0 :(得分:1)

$data = foreach ($disk in $disks) {$disks.size -join '.' }
$hash = [ordered]@{
     'VM<br>Name' = $vm.vmname 
     'State'      = $vm.state
     'Disk'       = $data
}