Follow @chaderoth

Sunday, October 14, 2018

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!

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.


What Happens When You Disconnect An ISO From A Linux VM While The Guest Operating System Still Has A Lock On The Media

If you have an operation you would like to perform and you do not inherently know how to perform said operation; what do you do? Well, I imagine like the majority of us you begin to scour the inter-webs for potential solutions.

In this case I wanted to find a way to easily and systematically find all of the virtual machines in my environment that had an ISO mounted to the virtual machine and then disconnect the ISO without any manual intervention on my part, other than perhaps running the script.

My goto for this task of course was PowerCLI. I have found that the more I play with and get to know PowerCLI the more excited I become about the possibilities it offers.

If you do a quick internet search for tasks like, "Use PowerCLI to get all VMs with a mounted ISO and disconnect ISO" you will find loads of blog posts on how to perform this task and some with different code styles and syntax, but all fairly basic as PowerCLI makes this a pretty simple operation.

The problem I discovered with this...well, actually I discovered a few problems.

Friday, December 29, 2017

How To Deploy Multiple VMs Using PowerCLI (Part 2) - Dynamic Parameters

In my previous post, "How To Deploy Multiple VMs Using PowerCLI" I showed how to script creating multiple VMs using PowerCLI and a Hash Table to track the build status. In that post I mentioned it was my first attempt at such an approach. Though this script had the potential to save someone quite a bit of time, it is actually a pretty tedious task to answer all of the questions the script prompts the user for and requires the user manually input the values needed for things like which ESXi host, Datastore, etc. to use for the VM build process.

This would likely result in an added step of the user needing to look up this information and could result in user error while mistyping information into the prompt.

While contemplating how to solve this problem I was recently introduced to parameters and using them in PowerShell to collect information from the command line and store their values into variables as part of another project I'm working on.

Saturday, November 11, 2017

How To Deploy Multiple VMs Using PowerCLI

As I have previously mentioned, working for an SMB and making up an Infrastructure Team of one can pose some unique challenges.

I was recently asked if I could spin up a number of VMs for a new Dev/Test environment for our Development Team.

This environment would require a number of groups of VMs running Microsoft Application Request Routing (ARR), Microsoft Internet Information Services (IIS), or Microsoft SQL Server.

I had been playing with VMware vSphere PowerCLI for things like moving all the VMs from one host to another for host maintenance as I don't have access to DRS or SDRS, but I hadn't done much more with it than that up to this point. Since I also do not have access to VMware vRealize Automation, I thought I would try creating a PowerCLI script to accomplish this task and to give myself a baseline for future automated deployments of multiple VMs.

Here is my first go at the script.

Sunday, October 1, 2017

IT Takes A Village...

"IT Takes a Village", taken from the African proverb, "It takes a village to raise a child." might seem like an odd sentiment and title for a blog about Information Technology and Virtualization.  As I mentioned in my introductory post I am also very passionate about the IT community and this growing passion is what ultimately led to my decision to finally step outside of my comfort zone and start this blog.

I recently attended VMworld 2017 and the great conversations I had the opportunity to engage in while there were absolutely priceless. I wouldn't have asked my current employer to send me to VMworld in the first place if I hadn't been encouraged to by a number of community members. These conversations and the advice I received gave me that final push I needed to start this blog, to step out of my comfort zone, and to be more active in the IT community.

Monday, September 11, 2017

Welcome to The Virtual Village

Welcome to The Virtual Village. My name is Chad Roth and I am an IT Systems Administrator currently working for an SMB and pursuing my VCP6-DCV (my first VMware certification). Being the sole Systems Administrator for a SMB means you need get to wear a lot of hats, usually in the same day. Among all of these skills, virtualization (VMware to be exact) is by far the one I am most passionate about. I mean, who wasn't blown away after their firsthand experience with virtualization technology?