Hi everyone,
How to remove the CDROM from a snapshot?
I'm trying to move VMs off of datastores by putting the datastores into maintenance mode.
Some VMs will not migrate off because the VM has a snapshot with a mounted CDROM.
The error is:
Remove-CDDrive : 12/11/18 4:04:12 PM | Remove-CDDrive | The CDDrive 'CD/DVD drive 1' is not attached to a VM. The requested operation is only supported for devices attached to VM. |
At line:15 char:16
+ | $r2a = Remove-CDDrive -CD $r2 -Confirm:$false |
+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
+ CategoryInfo | : InvalidArgument: (CD/DVD drive 1:CDDriveImpl) [Remove-CDDrive], VimException |
+ FullyQualifiedErrorId : Client20_VirtualDeviceServiceImpl_TryValidateDeviceOwnerIsVm_IsDatastore,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveCDDrive
Below is my code. It successfully removes CDROMs from VMs but fails to remove the CDROM from a snapshot. Any assistance would be appreciated.
$vms | % {
$r1 = get-vm $_.Name | Get-CDDrive
$r2 = get-vm $_.Name | Get-Snapshot | Get-CDDrive
if (($r1.IsoPath) -or ($r2.IsoPath)) {write-host $_.Name -ForegroundColor green}
if ($r1.IsoPath) {
write-host (" Main has CD mounted: {0}" -f $r1.IsoPath)
write-host (" Removing CD")
$r1a = $r1 | Remove-CDDrive -Confirm:$false
}
if ($r2.IsoPath) {
write-host (" Snapshot has CD mounted: {0}" -f $r2.IsoPath)
write-host (" Parent VM: {0}" -f $r2.Parent)
write-host (" Removing CD")
$r2a = Remove-CDDrive -CD $r2 -Confirm:$false
}
}
You can do something like this
Get-VM | Get-CDDrive | where{$_.IsoPath} | Set-CDDrive -NoMedia -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Drat.
I get this error on trying to remove CDROM from some but not all of the VMs:
Remove-CDDrive : 12/11/18 4:09:47 PM | Remove-CDDrive | The VM must be in the following state: PoweredOff. |
At line:9 char:22
+ | $r1a = $r1 | Remove-CDDrive -Confirm:$false |
+ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
+ CategoryInfo | : InvalidOperation: (PoweredOn:PowerState) [Remove-CDDrive], ViError |
+ FullyQualifiedErrorId : Common_SharedParameterHelper_VerifyVmIsInState_VmNotInState,VMware.VimAutomation.ViCore.Cmdlets.Commands.RemoveCDDrive
You are trying to remove the CDROM drive itself, which is not possible while a VM is powered on.
In fact, you normally don't need to remove the CDROM drive itself, you only need to unmount the ISO file that was mounted on that CDROM drive.
What did you have mounted on the CDROM drive?
And if it was an ISO file, was it perhaps located on the datastore you are trying to place in maintenance?
And no, you can't afaik, remove a CDROM drive, or a mounted ISO, from a snapshot, without reverting to that snapshot.
Best solution is to remove all snapshots.
And since a snapshot is not a backup, that shouldn't be an issue :smileygrin:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
Thanks! You're right, each problem-VM has an ISO mounted in its CD-ROM and so the VM is not migrating properly.
How do I force a VM to eject a mounted CDROM ISO?
Thanks!
-Chris
You can do something like this
Get-VM | Get-CDDrive | where{$_.IsoPath} | Set-CDDrive -NoMedia -Confirm:$false
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference