ps1Liner :: Eject CDROM drive for all VMware VM

ps1liner
What is one-liner? This is just one line of code (PowerShell/VMware PowerCLi or whatever) intended to perform particular task. It may be very or sometimes very-very long, but is still one line only!

This one-liner unmounts Datastore ISO file from all CD/DVD drives for all pipelined VM.

1Liner

Get-VM | Get-CDDrive | ? {$_.IsoPath} | Set-CDDrive -NoMedia:$true -Confirm:$false -ea SilentlyContinue

WhatIf

  • You can review the state before execution.
Get-VM | Get-CDDrive | select @{N='VM'; E={$_.Parent}}, @{N='CD Device'; E={$_.Name}} ,IsoPath | ? {$_.IsoPath} | sort Parent | ft -au

Customizations

  • Get VM that reside in particular Datacenter.
Get-Datacenter North | Get-VM | Get-CDDrive | ? {$_.IsoPath} | Set-CDDrive -NoMedia:$true -Confirm:$false -ea SilentlyContinue
  • Get VM that run in particular Cluster(s).
Get-Cluster PROD, DEV | Get-VM | Get-CDDrive | ? {$_.IsoPath} | Set-CDDrive -NoMedia:$true -Confirm:$false -ea SilentlyContinue

How to use

  • Connect to your VCenter(s)/ESXi Host(s).
Connect-VIServer VC1 -wa SilentlyContinue
  • Copy-Paste the code to the PowerShell console and hit Enter. That’s all.

Used cmdlets

  • The core cmdlets.
Get-VM
Get-CDDrive
Set-CDDrive
  • Auxiliary cmdlets
Get-Datacenter
Get-Cluster
Where-Object
Sort-Object
Select-Object
Format-Table

More 1Liners

Add PowerCLi version to the $PSVersionTable variable

One thought on “ps1Liner :: Eject CDROM drive for all VMware VM

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s