VMware Cloud Community
Bacon_Phat
Contributor
Contributor
Jump to solution

Mounted ISOs NOT Using Get-VM

I am trying to find a way to get information on what VMs have ISOs mounted, but given I have 4000 VMs, if I use the generic

Get-VM | Get-CDDrive | where { $_.ISOPath -like "*.iso*" } | Select-Object Parent,IsoPath

It takes forevery, and kicks the snott out of my vCenters.

I have spent a few days rolling through the idea of Get-View, but lack the syntax know-how.  Any suggestions?

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Due to the foreach statement, you have to use this little trick to capture the output in an array.

This updated version only reports mounted ISO files

$report = &{foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device)){
    $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]})
    if($cds){
      foreach($cd in $cds){
        if($cd.Backing -is [VMware.Vim.VirtualCdromIsoBackingInfo]){
          New-Object PSObject -Property @{
            VM = $vm.Name
            CD = $cd.DeviceInfo.Label
           
Info = $_.FileName
          }         }       }     }   } }
$report | ConvertTo-Html Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

    foreach($vm in (Get-View -ViewType VirtualMachine  -Property Name,Config.Hardware.Device)){
      $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]})
      if($cds){
        foreach($cd in $cds){
          switch($cd.Backing){
            {$_ -is [VMware.Vim.VirtualCdromIsoBackingInfo]}{
              $Type = "Datastore ISO File"
              $Info = $_.FileName
            }             {
$_ -is [VMware.Vim.VirtualCdromAtapiBackingInfo]}{               $Type = "Host Device"
             
$Info = $_.DeviceName
            }             {$_ -is [VMware.Vim.VirtualCdromRemoteAtapiBackingInfo]}{               $Type = "Client Device"
             
$Info = $null
            }           }          
New-Object PSObject -Property @{             VM = $vm.Name
            CD = $cd.DeviceInfo.Label
            Type = $Type
           
Info = $Info
          }         }       }     }

It returns all connected CD/DVD, but it should be rather straightforward to use a where-clause to filter out what you want.


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

0 Kudos
Bacon_Phat
Contributor
Contributor
Jump to solution

Fantastic work- the problem with this is that it obtains all VMs with a CDROM present, not just an ISO attached.  So, the report sends me a result of all 4000 VMs.

Here is a result:

Info : CD/DVD Drive 1
CD   : CD/DVD Drive 1
Type : Host Device
VM   : VM-Shelty64-02d 2011-12-16 00:20:13 717

Also, I am having trouble formatting output to ConvertTo-HTML.  I tried putting the results into an array, but no luck:

$ISOs = @()

    foreach($vm in (Get-View -ViewType VirtualMachine -SearchRoot $Script:vmwSearchRoot -Property Name,Config.Hardware.Device)){

      $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]})

      if($cds){

        foreach($cd in $cds){

          switch($cd.Backing){

            {$_ -is [VMware.Vim.VirtualCdromIsoBackingInfo]}{

              $Type = "Datastore ISO File"

              $Info = $_.FileName

            }

            {$_ -is [VMware.Vim.VirtualCdromAtapiBackingInfo]}{

              $Type = "Host Device"

              $Info = $_.DeviceName

            }

            {$_ -is [VMware.Vim.VirtualCdromRemoteAtapiBackingInfo]}{

              $Type = "Client Device"

              $Info = $null

            }

          }

          New-Object PSObject -Property @{

            VM = $vm.Name

            CD = $cd.DeviceInfo.Label

            Type = $Type

            Info = $Info

          }

        }

      }

    }

$ISOs | ConvertTo-Html –Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Due to the foreach statement, you have to use this little trick to capture the output in an array.

This updated version only reports mounted ISO files

$report = &{foreach($vm in (Get-View -ViewType VirtualMachine -Property Name,Config.Hardware.Device)){
    $cds = @($vm.Config.Hardware.Device | where {$_ -is [VMware.Vim.VirtualCdrom]})
    if($cds){
      foreach($cd in $cds){
        if($cd.Backing -is [VMware.Vim.VirtualCdromIsoBackingInfo]){
          New-Object PSObject -Property @{
            VM = $vm.Name
            CD = $cd.DeviceInfo.Label
           
Info = $_.FileName
          }         }       }     }   } }
$report | ConvertTo-Html Body "<div style='color:#089199'><H3>ISOs</H3></div>" | Set-Content C:\Temp\Output.htm


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

0 Kudos
Bacon_Phat
Contributor
Contributor
Jump to solution

That worked great!  Thanks!!

0 Kudos