PowerCLI: Listing VMs with ISOs mounted

For almost a year ago, I posted a simple one-liner to list all VMs who has ISOs mounted. You can view that post here: http://cloud.kemta.net/2013/10/powershell-vmware-list-all-vms-with-iso-mounted-and-dismount-them/

That post was written before I truly discovered the major advantages of using Get-View instead of Get-VM, Get-VMHost and so on. If used correctly, there’s a major difference in speed when using Get-View over Get-VM.
When writing this post I checked the differences in speed when using the old way that I linked to above and my new function (which I’ll get to in a second or two..), the result was as follows:


As you can see, the difference is pretty clear. 5 seconds vs. 1.6 minutes…

So, without further ado, I present to you the code for Get-ISOMounts:

function Get-ISOMounts
        Param (
        $VMs = Get-View -ViewType virtualmachine -Property name,Config.Hardware.Device
        $VMsWithISO = @()
        $progress = 1
        foreach ($VM in $VMs) {
            Write-Progress -Activity "Checking if VMs have ISOs mounted" -Status "Working on $($VM.name)" -PercentComplete ($progress/$VMs.count*100) -Id 1 -ErrorAction SilentlyContinue
            if (($VM | select -ExpandProperty config | select -ExpandProperty hardware | select -ExpandProperty device | select -ExpandProperty deviceinfo | where {$_.Summary -like "ISO*"}) -ne $NULL) {
                $object = New-Object PSObject
                Add-Member -InputObject $object NoteProperty VM $VM.Name
                Add-Member -InputObject $object NoteProperty "ISO mounted" (($VM | select -ExpandProperty config | select -ExpandProperty hardware | select -ExpandProperty device | select -ExpandProperty deviceinfo | where {$_.Summary -like "ISO*"}).Summary).Substring(4)
                $VMsWithISO += $object
        Write-Progress -Activity "Checking if VMs have ISOs mounted" -Status "All done" -Completed -Id 1 -ErrorAction SilentlyContinue
        if ($Dismount)
            Write-Verbose "Starting to dismount ISOs"
            $progress = 1
            foreach ($VM in $VMsWithISO) {
                Write-Progress -Activity "Dismounting ISOs" -Status "Working on $($VM.name)" -PercentComplete ($progress/$VMsWithISO.count*100) -Id 1 -ErrorAction SilentlyContinue
                Get-CDDrive -VM $VM.Name | Set-CDDrive -NoMedia -Confirm:$False
            Write-Progress -Activity "Dismounting ISOs" -Status "All done" -Completed -Id 1 -ErrorAction SilentlyContinue
  Lists all VMs with ISOs mounted, can also dismount them
  Lists all VMs with ISOs mounted. If the switch -Dismount is present all mounted ISOs will be dismounted
  Lists all mounted ISOs in the vCenter
  Get-Snapshots -Dismount
  Lists all mounted ISOs on VMs in the vCenter and then dismounts them

I feel the help section should speak for itself, but I’ll provide you a screenshot none the less. Just running Get-ISOMounts will provide you with an output looking like this:


Category(s): PowerCLI, Powershell, VMware
Tags: , , , , , ,

One Response to PowerCLI: Listing VMs with ISOs mounted

  1. Thanks for your script!

    However, when trying to unmount, I get an error saying

    Get-CDDrive : Cannot validate argument on parameter ‘VM’. The argument is
    null. Supply a non-null argument and try the command again.
    At C:UsersxxxGet-ISOMounts.ps1:29 char:33
    + Get-CDDrive -VM $VM.Name | Set-CDDrive -NoMedia
    + ~~~~~~~~
    + CategoryInfo : InvalidData: (Smilie: :) [Get-CDDrive], ParameterBinding
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

Leave a Reply

Your email address will not be published. Required fields are marked *