Search code examples
powershellpowercli

PowerCLI- get VM cd-mount in 1 table


I try to get all the vm which have a mounted cd in one table, but the output I get is a line-by line like:

vm1 \vm1_isopath\ vm2 \vm2_isopath\

can it possible to get all the information in 1 table with 2 columns, of VM name, and ISOPath ?

my code is:

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

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

        }
    }

thank you.


Solution

  • I extended your code to:

    $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

    Hope that helps