PowerCLI: Reset CD-drives using PowerShell
As most of you know and probably experienced from time to time: when a Virtual Machine’s CD-drive is connected to an ISO-file on one of your Datastores or even connected to the physical drive of your ESX-host the migration due VMotion of a Virtual Machine will not work.
Normally this isn’t really a problem except if you put a ESX-host in Maintenance mode and Virtual Center will simply not tell you why the Maintenance mode process is hanging or even giving a time-out after 15 minutes for no obvious reason. Most of the times it’s a Virtual Machine which has a CD-drive connected to an ISO file. A waste of time if you ask me.
So to prevent this from happening I’ve written a simple PowerShell oneliner/script disconnect these CD-drives from the ISO-files or from the physical drives and set them to Client-drives which is ok for VMotion:
(Get-VM -LocationGet-VMHost "your.esx.host")) | ` ForEach ( $_ ) { Get-CDDrive $_ | ` Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | ` Set-CDDrive -NoMedia -Confirm:$False }
Instead of executing this just on one host you can also execute this for your entire cluster:
(Get-VM -LocationGet-Cluster "Your Cluster Name")) | ` ForEach ( $_ ) { Get-CDDrive $_ | ` Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | ` Set-CDDrive -NoMedia -Confirm:$False }
Or ofcourse, by Datacenter:
(Get-VM -LocationGet-Datacenter "Your Datacenter Name")) | ` ForEach ( $_ ) { Get-CDDrive $_ | ` Where { $_.IsoPath.Length -gt 0 -OR $_.HostDevice.Length -gt 0 } | ` Set-CDDrive -NoMedia -Confirm:$False }
It’s as easy as that, now there will be no Virtual Machine interrupting your VMotions anymore and you can put your ESX hosts in maintenance mode without any problems
Cheers!
