Discovering VMs with ISOs Attached

One of my customers this week ran into an issue with a VM that locked up and was having issues at an OS / application level. They were ready to reboot the VM and noticed an error in vCenter mentioning that an attached ISO had been locked by the guest OS and could not be automatically unmounted. We were able to change the VM settings to disconnect the ISO image and reboot the VM to get everything working again. It seemed that the ISO image had been moved or renamed on the NFS datastore that the ISO had been located on which caused this error for the VM.

The general recommendation for VMs and ISO images is to not leave them permanently connected to the VM CD Drive unless there is a specific need for the VM to keep the ISO attached. I recommended auditing their VMs to see which VMs had ISOs attached and proceed with unmounting ISOs that could be disconnected.

Automating the Process

Using PowerCLI, we are able to detect all of the VMs that have an attached ISO image with a single line of code and output a list of the VMs as needed. For my example, I get all of the VMs in the cluster named Cluster1 with ISOs attached.

Connect-VIServer -Server vcenter.dcgold.local -Credential (Get-Credential)

$AttachedISOs = Get-Cluster Cluster1 | Get-VM | Get-CDDrive | Where-Object IsoPath -ne $null | Select @{Name='VMName'; Expression={$_.Parent}}, IsoPath
$AttachedISOs

VMName             IsoPath
------             -------
lab-zfeingold-vm01 [Datastore1] VMware/Image1.iso
lab-zfeingold-vm02 [Datastore1] VMware/Image2.iso    
lab-zfeingold-vm03 [Datastore1] VMware/Image3.iso    

NOTE: If the IsoPath shows as empty with [ ], this is due to the CD Drive being set to use a Datastore ISO File for the VM settings, but no ISO has been selected.

For a count of the number of VMs reflected in this list, use Measure-Object as seen below:

$ISOCount = ($AttachedISOs | Measure-Object).Count
$ISOCount

25

Take this information a step further and export the list to a CSV files with the following:

$AttachedISOs | Export-CSV -Path 'C:\temp\AttachedISOs.csv'

To proceed with disconnecting all of the ISO images, take the result of the first command that gets a list of all of the VMs with ISOs connected, then set the CD Drive settings to have no media attached to effectively disconnect the ISO image.

$AttachedISOs | Set-CDDrive -NoMedia -Confirm:$false | Out-Null

Validate that the ISOs have been detached by running the first command again and confirm that there is no output returned.

Get-Cluster Cluster1 | Get-VM | Get-CDDrive | Where-Object IsoPath -ne $null | Select @{Name='VMName'; Expression={$_.Parent}}, IsoPath


Hopefully that helps and saves you the trouble of manually clicking through to find all of the VMs in your environment with ISO images connected and automates the task of disconnecting the media from the CD Drive as needed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: