[PowerCLI] Find and disconnect CD Drives on your VMs
One of the things that regularly pops up in our health checks in VMware environments are connected CD Drives that prevent DRS from functioning properly or prevents a host from entering maintenance mode when doing thing like patching.
In large environments it can get very tedious to disconnect all those drives manually. With PowerCLI it is just one line.
If you want to free up a host for maintenance mode you can do it with two lines:
Get-VMHost <your host> |Get-VM | Where-Object {$_.PowerState –eq “PoweredOn”} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False
Get-VMHost <your host>| Set-VMHost -VMHost Host -State "Maintenance"
If you want to disconnect all of your CD-ROMs:
Get-VM | Where-Object {$_.PowerState –eq “PoweredOn”} | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$False
If you just want to list all the VMs that have connected CD Drives:
Get-VM | Where-Object {$_.PowerState –eq “PoweredOn”} | Get-CDDrive | FT Parent, IsoPath
Comments (2)
Hi Dario
Das funktioniert so lange gut wie die cd unlocked ist. Wenn die Frage beantwortet werden muß wirds leider bunt. Hast du da einen guten Tip zu? Alles was ich bisher dazu fand hab ich bisher nicht zum Laufen bekommen.
Liebe Grüße, Stefan
Hallo Stefan
Danke fürs Nachfragen. Dieser Blogpost ist mittlerweile neun Jahre alt, aber schön dass er noch gelesen wird.
Du hast recht, sobald die VM das Laufwerk gesperrt hat, wirft ESXi die VM Question msg.cdromdisconnect.locked und Set-CDDrive bleibt einfach stehen, bis jemand antwortet.
In den meisten Fällen ist ein gemountetes VMware Tools ISO schuld. Das bekommst du ganz ohne Rückfrage weg:
Get-VM | Where-Object { $_.ExtensionData.Runtime.ToolsInstallerMounted } | Dismount-ToolsWenn wirklich das Guest-OS sperrt, startest du den Reconfigure asynchron und beantwortest die Frage separat:
$task = Get-VM MyVM | Get-CDDrive | Set-CDDrive -NoMedia -Confirm:$false -RunAsyncStart-Sleep 3
Get-VMQuestion -VM MyVM | Set-VMQuestion -Option "Yes" -Confirm:$false
Wait-Task $task
Das -RunAsync ist der Teil, an dem es sonst scheitert.
Gruss
Dario