Tracking down those connected CD-ROMS with PowerCLI
August 13th, 2010
Here are some handy script lines for working out what CDs you have connected which can cause Vmotion to fail.
You don’t always want to disconnect all CDs. If you have .ISOs on shared storage Vmotion will be fine as long as the hosts can both see the .ISO files.
Get the names of VMs with connected CD drives:
get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name
Get the names of VMs with connected .ISOs
get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" -and $_.ISOPath -like "*.ISO*"} } | select Name, @{Name=".ISO Path";Expression={(Get-CDDrive $_).isopath }}
Disconnect all VMs where the CD Drive is connected and it is not an .ISO
$VMs = Get-VM $CDConnected = Get-CDDrive $VMs | where { ($_.ConnectionState.Connected -eq "true") -and ($_.ISOPath -notlike "*.ISO*")} If ($CDConnected -ne $null) {Set-CDDrive -connected 0 -StartConnected 0 $CDConnected -Confirm:$false }
or another slightly quicker and dirtier way thanks to commenter José de Alonso
Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false
Julian, I am surprised no one else has commented on this great bit of PowerCLI. I have 1800 VMs and we had an NFS share that we put ISO files on go bad. It hung ESXi hosts and caused VMs to become unmanageable until the ESXi hosts services were restarted. Pain in the butt finding all the connected shares. This was a great help.
Have a great day an keep slapping out that nice PowerCLI magic.
Thanks Julian!!
Very usseful script. thank you
thank you very much!
Thx!
Dude! This script is so usefull. Just what I needed.
Great!
Here is other PowerCLI onliner to disconnect all mounted VM ISOs:
Get-VM | Get-CDDrive | Where {$_.ISOPath -ne $null} | Set-CDDrive -NoMedia -Confirm:$false
Extracted from:
http://www.sysadmit.com/2017/12/vmware-desconectar-cd-dvd-iso-PowerCLI.html