Eject all CD-ROMs in a VMware cluster

Every VMware administration knows the following issue – the cluster needs to patched and so the particular nodes need to be rebooted. Fortunately this doesn’t bother the users working on the farm becuase the virtual machines can be moved “online” between the cluster nodes.

There might be a little show stopper when ISO images (which aren’t saved on the cluster storage) are mounted – e.g. local CD drives. In this case the virtual machine can’t be moved and the cluster node won’t enter maintenance mode – it is necessary to fix this manually.

Depending on the amount of mounted ISOs, this can take some time – what a pity! I wrote the following PowerShell script to produce relief:

PowerCLI C:scripts> vim VMISOs.ps1

#Get all virtual machines with mounted ISOs and dismount (if wanted)
#2013 By Christian Stankowic 

##################################################################

#Get array of all Clusters
$myClusters = Get-Cluster

#Create VMs array
$VMs = @()

Write-Host "Okay - I'm going to check all VMs whether there are mounted ISO files."
Write-Host "This will take some time - get yourself some coffee.. ;-)"
Write-Host ""

#Get vms of cluster
foreach ($cluster in $myClusters) {
                #Get VMs
                #$thisVMs = Get-VM
                $thisVMs = Get-Cluster $cluster | Get-VM
                $counter=0;

                #Get VM information
                foreach ($vm in $thisVMs) {
                               #Get view
                               $vmView = $vm | Get-View

                               if( (($vm | Get-CDDrive).ISOPath) -or (($vm | Get-CDDrive).RemoteDevice) -or (($vm | Get-CDDrive).HostDevice) )
                               {
                                               #Setup output
                                               $VMInfo = "" | Select "VM","Host","ISO","RemoteDevice","HostDevice"

                                               #Write-Host "VM = $vm | Host = " ($vm | Get-VMHost).name " | ISO = " ($vm | Get-CDDrive).ISOPath " / Remote-Device = " $(vm | Get-CDDrive).RemoteDevice " / HostDevice = " $(vm | Get-CDDrive).HostDevice

                                               #Defining hostname, ESX host and ISO path
                                               $VMInfo."VM" = $vm.Name
                                               $VMInfo."Host" = ($vm | Get-VMHost).Name
                                               $VMInfo."ISO" = ($vm | Get-CDDrive).ISOPath
                                               $VMInfo."RemoteDevice" = ($vm | Get-CDDrive).RemoteDevice
                                               $VMInfo."HostDevice" = ($vm | Get-CDDrive).HostDevice

                                               #Add to array
                                               $VMs += $VMInfo
                               }

                               $counter++;
                               if( $counter % 10 -eq 0 ) {
                               Write-Host "Check $counter of " $thisVMs.length " in " $cluster " so far..."
                               }
                }
}

#sort array by Cluster
$VMs | Sort Cluster

#disconnect
$answer = Read-Host "Found " $VMs.length " mappings - force disconnect now? (y/n)"
if($answer -eq "y")
{
                foreach ($vm in $VMs)
                {
                               Write-Host "Disconnect on " $vm.VM "..."
                               Get-VM $vm.VM | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$false
                }
}
else { Write-Host "Disconnect aborted by user." }

The script requires that PowerShell and VMware PowerCLI are installed. After a connection to vCenter is established, the script asks all clusters and appropriate virtual machines for mounded CD-ROMs/ISOs. Afterwards a list with all affected virtual machines is displays – after that the media is ejected and the administration tasks can continue.

PowerCLI C:scripts> Connect-VIServer TVM-VCENTER

PowerCLI C:scripts> .VMISOs.ps1
Okay - I'm going to check all VMs whether there are mounted ISO files.
This will take some time - get yourself some coffee.. ;-)

Check 10 of  136  in  Demo-Cluster  so far...
....
VM           : TVM-WEB01
Host         : hypervisor01.localdomain.loc
ISO          :
RemoteDevice : /usr/lib/vmware/isoimages/windows.iso
HostDevice   :

Found  6  mappings - force disconnect now? (y/n): y
Disconnect on  TVM-WEB01 ...
...

This helps me a lot before starting administration tasks. 🙂

Sharing is caring

Leave a Reply