Follow @chaderoth

Saturday, October 6, 2018

Use PowerCLI To Find Linux VMs With Connected ISO And Disconnect ISO Without Causing VM To Become Unresponsive

In my previous post I discussed what can happen if you attempt to disconnect an ISO from a Linux virtual machine before first ejecting or unmounting the media inside the guest operating system.

This can have dire consequences if you bring all of your Linux VMs to a screeching halt. It is easy enough to answer the prompt displayed in vCenter for a handful of VMs, but if you have a large Linux environment this could be cumbersome. The VM also returns to a running state as if it were paused, but this severs any connected sessions. Any outside operations running that use the affected VM as a resource will also be unsuccessful during this time.

Like I stated in my previous post the majority of the PowerCLI examples I found in my research simply returned all VMs where the ISO path was populated, regardless of guest OS and then for each one of the VMs returned would disconnect the ISO. We now know what happens when you attempt this operation on a Linux VM before ejecting or unmounting the media inside of the guest OS.

My first task was to find a way to talk to the guest OS inside of my PowerCLI script without needing to sign in to the guest OS from a console or through an SSH session.

I knew the functionality to do this was available by way of the Invoke-VMScript cmdlet, but I never attempted to use it with Linux before. I had tried using this cmdlet a few times with a Windows VM, but I ran into a number of security loopholes to jump through that were understandable but painful.

I then needed to find a way to pass credentials to the guest OS in a way I was comfortable with. In otherwords, I didn't want to store these credentials in the script itself.

For this I used the familiar Get-Credential cmdlet which will prompt the user with an authentication dialog box which is familiar in Windows. The gathered credentials are then stored in an object to be passed to the Invoke-VMScript cmdlet and then to the guest OS by way of VMware Tools.

I basically took the original code I found mentioned in my previous post and altered it slightly because I wanted to gather more properties of each VM returned so I could use these properties in my script for things like displaying which VMs would be affected, etc.

First I'm simply using the Get-Credential cmdlet and storing the gathered credentials in the variable, $Creds.

Here I am getting all the VMs whose OSFullName property contains "CentOS" and for each VM return those whose IsoPath property is populated which means there is an ISO connected to the VM. I'm then selecting all properties for those VMs.

This section of the script I am using the Write-Host cmdlet to display the VM where the media is being ejected inside of the guest OS and the Invoke-VMScript to pass the eject command to the guest OS.

Here again I am displaying the operation and this is where we are disconnecting the ISO from the VM using the Set-CDDrive cmdlet.

When we put it all together I am using a For-each Loop to iterate through all VMs returned that meet the criteria we filtered by earlier, ejecting the media inside of the guest OS, and disconnecting the ISO from the VM.

In my next post I hope to show you how we will be able to run one script that will return all VMs with an ISO connected to the VM and for those VMs returned that are running a Linux OS use the code above to eject the media in the guest and also process Windows guests as well.

No comments:

Post a Comment