Follow @chaderoth

Tuesday, January 8, 2019

Use PowerCLI To Find VMs With Connected ISOs And Disconnect While Handling Linux Locked Media

In this post I will be building off of my last two posts which stemmed from a problem I ran into where I attempted to find and disconnect all ISOs connected to VMs in my environment. Please go back and read my previous posts, but as a quick recap; if you attempted to disconnect an ISO from a Linux VM before ejecting the media in the guest your Linux VM will become unresponsive until you answer a dialog that displays in vCenter.

In my previous post I showed you how to find all Linux VMs, in this case CentOS Linux VMs, properly eject the media inside of the guest using the Invoke-VMScript, and disconnect the ISO from the VM. That post; however, only covered Linux VMs.

I am sure a lot of us are managing mixed environments. So in this post I will be showing you how to find all VMs with connected ISOs like before, but this time we will be adding logic on how to handle both Windows and Linux VMs.

To do this I've modified the code structure a bit and I'm actually happier with how I'm deciding to prompt for the credentials to be used with the Linux guest OS needed to run the Invoke-VMScript cmdlet.

Similar to the original script, here I am getting all VMs with a configured ISO Path since this is how we know a VM has an ISO connected to it. I am storing the returned data in the variable, $VMs.

I am then searching through the data stored in $VMs, filtering out the Windows VMs, and storing data for those VMs in the variable, $WinVMs.

Here I am doing the same step as the previous step, except this time for the CentOS Linux VMs and storing that data in the variable, $CentVMs.

Now that we have our two variables populated with the appropriate VM data sorted by guest OS we can operate on them accordingly.

We are using an IF Statement to determine if we need to process the following block of code.
If ($WinVMs) or in otherwords, if the $WinVMs variable contains data which means we have returned Windows VMs that have an ISO connected to them. Then process the For-each Loop to disconnect the ISO from the Windows VMs. Since these VMs are running a Windows guest OS we do not need to eject the media from inside of the guest.

Here we are using the same IF Statement structure as above, but this time we are checking to see if there are CentOS Linux VMs with connected ISOs. I also want to point out what I eluded to earlier about getting credentials to be passed to the guest OS for ejecting the media.

If you read my previous post you will notice that I'm prompting for credentials even before we've determined if we truly have any VMs with a connected ISO. Looking back at this now it seems like a wasted step. That is why in the revised script we aren't prompted for credentials until we have validated that we truly have Linux VMs with a connected ISO in our environment.

We then process the For-each Loop to eject the media from inside of the guest OS before disconnecting the ISO from the VM because these are Linux VMs. Due to the For-each Loop this operation is performed on every VM object stored in the $CentVMs variable.

Now when we put it all together, here is the script in it's entirety.

Now instead of running two separate scripts to properly find and disconnect ISOs from both Windows and Linux VMs we have a way to easily perform these tasks together.
I'm really happy with how this came together and I hope that I've been able to share something with the community that makes a day to day task just a little bit easier.

Thanks for reading!

1 comment:

  1. A creative method improves the production process and helps you improve the accuracy and the general end of your merchandise. Make full use of your simulation software program to confirm any process earlier than sending it from the CAD system to the machine to keep away from collisions. When working on a 5-axis machine, it is hard to visualize collision factors; with the software program, you can to|you possibly can} closely monitor the size of the software, the interference of the software, and the size of the half. Bringing up the idea of trying CNC with a partner can really feel scary, but Balestrieri recommends “yes, no, and maybe” lists that folks can fill out to speak about what they’re open to and what they’re not. Porn shouldn’t be a primary port of call, however, Cashmere Sweater because of|as a outcome of} “it doesn’t all the time highlight setting up the method in a method that is ethical and protected for people”.