2017-05-11 31 views
0

我試圖讓所有這些都在一個表中的安裝光盤中的虛擬機,但輸出我得到的是一條線,通過類似線:PowerCLI-獲得1臺虛擬機的CD-安裝

VM1 \ vm1_isopath \ vm2 \ vm2_isopath \

是否可以通過2列,VM名稱和ISOPath獲取1個表中的所有信息?

我的代碼是:

$VMs=Get-VM 
ForEach ($vm in $VMs) 

    { 
     $VMmount=Get-CDDrive -VM $vm 
     if ($VMmount.IsoPath) 
     { 
      $vm | select Name 
      $VMmount.IsoPath 

     } 
    } 

謝謝。

回答

0

我伸出你的代碼:

$VMs=Get-VM 
 

 
$vmInfos = @() 
 

 
ForEach ($vm in $VMs) 
 
    { 
 
     $VMmount=Get-CDDrive -VM $vm 
 
     if ($VMmount.IsoPath) 
 
     { 
 
      # Store needed info in hashtable 
 
      $info = @{} 
 
      $info.name = ($vm | select -ExpandProperty Name) 
 
      $info.IsoPath = $VMmount.IsoPath 
 
      
 
      # Convert hashtable to custom object 
 
      $object = New-Object –TypeName PSObject –Prop $info 
 
      
 
      # Add to array 
 
      $vmInfos += $object 
 
     } 
 
    } 
 

 
# Dump collected infos 
 
$vmInfos | format-table -autosize

希望幫助