PowerCLI

PowerCLI Commands to Manage ISOs before ESXi Maintenance

Sharing is caring, so hopefully you can learn from my experience and not make the same mistakes in your environment. If nothing else, take these PowerCLI snippets and grow. Caution. In my old environment, I would run this up to where I saw this

Issue:

Couldn’t put an ESXi host into maintenance mode because some VMs had an ISO attached to them.

Result:

  1. Maintenance delay.
  2. Lost a little trust from the business.
    • Trust is easy to lose, and tough to gain.

Resolution:

Get-Datacenter -name "Target Datacenter" | Get-VM "Target VM" | Get-CDDrive | Set-CDDrive -Connected $false -confirm:$false | select Parent, IsoPath, ConnectionState | Format-List

Steps to the Resolution

Install PowerCLI

Install-Module VMware.PowerCLI -Scope CurrentUser

Connect to vCenter/vSphere

  • You will be prompted for credentials after running the command below
connect-viserver "vC IP or FQDN"

Get your Target Datacenter Name/Host Cluster/VM Name

  • I’m using the Datacenter command because the last time I used this was for a ROBO cluster where all ESXi hosts in the cluster were going to be patched.
Get-Datacenter 

Now that we have our Datacenter name, let’s grab the VMs.

Get-Datacenter -name "Target Name" | Get-VM

I’m going to grab just 1 VM and see the options for the CD Drive.

Get-Datacenter -name "Target Name" | Get-VM "VM Name" | Get-CDDrive | Select *

In vSphere, we can see that the VM has a CD/ISO attached and set to reconnect on boot.

Verbiage check from GUI and PowerCLI

  • Connected = Connected
  • Connect At Power On = StartConnected
  • ? = GuestControl

Let’s clean this up so we only get relevant information before we proceed with making changes.

  • “Select” = We are selecting only the following properties to display (in green above)
  • “Format-List” = Please format this nicely
Get-Datacenter -name "Target DC" | Get-VM "Target VM" | Get-CDDrive | select Parent, IsoPath, ConnectionState | Format-List

This is where I would stop to export my results, and work with the application owners to see if the ISOs can be removed. This is 1 VM, so it may not be worth doing it, but here is the command to export the results for a whole datacenter.

Get-Datacenter "Target DC" | Get-VM | get-cddrive | select Parent, IsoPath, ConnectionState | Export-Csv AttachedISOReport.csv -NoTypeInformation

Caution – We’re about to make changes

Let’s disconnect the ISO from the VM, so the OS doesn’t recognize it, and we can proceed with maintenance. We’re keeping all our code, and adding this line:

  • Set-CDDrive -Connected $false
Get-Datacenter -name "Target Datacenter" | Get-VM "Target VM" | Get-CDDrive | Set-CDDrive -Connected $false | select Parent, IsoPath, ConnectionState | Format-List

Let’s add a line to not prompt us to confirm anymore

Get-Datacenter -name "Target Datacenter" | Get-VM "Target VM" | Get-CDDrive | Set-CDDrive -Connected $false -confirm:$false | select Parent, IsoPath, ConnectionState | Format-List

If you want to make sure the ISO doesn’t reconnect to the VM after reboot, add the “-StartConnected $false” parameter to “Set-CDDrive”

Get-Datacenter -name "Target DC" | Get-VM "Target VM" | Get-CDDrive | Set-CDDrive -Connected $false -StartConnected $false -confirm:$false | select Parent, IsoPath, ConnectionState | Format-List

Community Links

PowerCLI/Automation Broadcom Community

Broadcom Developer – PowerCLI

Additional #vCommunity Post

Tagged , , , , , , ,
Avatar photo

About Franky Barragan

Franky Barragan is currently the VMware {code} Community Manager. In his day job, Franky gets to work with the Communities Team at Broadcom. If money was no object, Franky would be a part-time chef and part-time instructor.
View all posts by Franky Barragan →