How to see which session has the Remote Console opened in VMware! (GUI and PowerCLI!)

So you ever been in that situation where you either see a message like,

Number of active connections has changed. There are now 2 active connections to this console

Or…

Unable to connect to the MKS: Console access to the virtual machine cannot be granted since the connection limit of ‘x’ has been reached.”  (This second one is most likely to come up when you have either a STIG or security defined limitation to the number of remote connections to use, thus by default you’re more likely to see message #1!)

Well, the question is… What do you do in these circumstances? You might ask yourself, “Damnit! Who is the other person connected!”   When looking for this answer you may come across responses in the forums such as:

  • Just use tsadmin to login to the server to see who else is logged in
    • Great if you’re on windows, but that doesn’t tell you show OWNS the SESSION
  • ssh into the box to see who else is logged in
    • Wow it’s like you’re not getting it. What if they’re not logged in, but simply sitting at the login prompt? I hear fail…
  • Use arbitrary application to arbitrarily see who is logged in!
    • Okay, seriously? (Yes I’ve seen NUMEROUS responses like this, which refer to lots of applications, and don’t solve the problem…)

Well, guess what, I don’t like these answers, which prompted me to write this blog post, thus I’ll show you how to do it via the GUI (Very Easy…) and also via PowerCLI because we love PowerCLI! Disclaimer: I do not show how to do this using the VMware Web Client, because I personally hate the web client, so if you’d like to document that part, go for it. :)

Checking for Remote console connected in the GUI!

image

So within the handy GUI from within a Virtual Machine (In this context I use the UI VM for vCOPS…) Just click on the “Tasks & Events” tab and you’ll notice the entry below, “Remote console connected” looking at the timestamp you’ll be able to note which user it was which connected and at what time.  Thus if this user doesn’t need to be logged into the console you can go kill their session as needed. Easy Peasy, right?! :)

Checking for Remote console connected using PowerCLI!!!

This one is just as easy if not easier, especially if you want to modify this script ultimately to query lots of objects, or you can even modify it to see which particular sessions a particular user has opened, that is outside the scope of THIS blog post, but nonetheless, here is the syntax!

Get-VM %VMNAME% | Get-VIEvent | Where {$_.Gettype().Name -eq "VMRemoteConsoleConnectedEvent"} | Select UserName, CreatedTime, FullFormattedMessage

What you have here is a Get-VM looking at a specific Virtual Machine (You supply) then it will pull up from that VM or VMs the Remote Console Connected event, After which point PowerCLI will dump the results of the Username connected, what time they did, and the FullFormattedMessage (Which I mainly have in there for errorchecking, you can omit if you’d like)

But lo and behold, BOOM you’re done! Hopefully this was easy for you, if you’d like there are a lot of VERY long and VERY complex scripts which do the EXACT same thing, but you know how I feel about One-Liners. :)

Good luck and stay Physical, VIRTUAL I MEAN VIRTUAL, OMG!

How to suppress annoying shell warnings on your ESXi hosts in vCenter!

So there you are logging into one of your vCenter’s and then WHOA WHAT IS GOING ON, WHY IS THERE A YELLOW TRIANGLE ON MY SERVER

Annoying Shell Warning on ESXi for ESXi Shell and SSH enabled

Then you click on your server, go to the “Summary” tab and notice it has some ERRORS! Oh my! But you might say to yourself… “Yea, I enabled remote ssh and local shell, so why is it warning me about this?!”

Well, now you don’t need to be annoyed by non-essential ‘warning’ messages which detract from REAL and ACTUAL error messages!

image

This is easily enough fixed by simply going to EACH of your servers, clicking “Configuration” then going to the “Advanced Settings” under Software, drilling down to “UserVars” finding “UserVars.SuppressShellWarning” and changing the value from “0” to “1”

But I don’t wanna do it manually!

Yea. Me neither. I’d rather execute this VERY simply using a single PowerCLI script.  I’ve found some really useful scripts out there which do this in 5-7 lines! That’s awesome…. I hate mult-line scripts unless I SPECIFICALLY need to do multi-line.  So here it is all in one-line!

Disclaimer: The assumption is that you’re using Connect-VIServer in order to connect to your one or multiple vCenters.

Set-VMHostAdvancedConfiguration –VMHost * –Name UserVars.SuppressShellWarning –Value 1

Poof! That is it, and it’ll go through one by one talking to each of your vCenters and enumerating out each of the underlying hosts and change this value!  It literally cannot get any easier than that! <3

How to stop VMware Tools Install en masse with PowerCLI!

The other day I was looking at one of my SRM sites and noticed that a CRAP load of VMs were complaining about a CD-Rom mounted, and I was all like, “deh, what is going on!” Well, there was a mass-installation of the VMware tools in place, or at least the tools were mounted.

This is awesome and all, but I didn’t want these VMs to have the tools installer active any more and instead wanted to CANCEL ALL OF THEM!  Sure I could right click on each VM and deactivate the tools installer, but this was for hundreds of VMs, and that sounds annoying.

So, for your benefit and very simply…

Get-VM * | Dismount-Tools

Yes, it is literally that easy. :)  Poof and you’re done! You can call out specific VMs if you’d like, but I think you have that under control by now!

Using PowerCLI to Shutdown a VM resize vCPU and Memory and Power back on!

You ever been in that situation of “Oh no, I need to modify a VM to increase/decrease the number of vCPUs and Memory, but I can’t do it during normal business hours” Or whatever the situation may be?  Well, fellow #vExpert @BoBolander and I were discussing this recently, and worked together to create this!  This also will apply if you’re doing this to multiple virtual machines!

Well, look no further. :) You can script this, as in to execute it as a script to happen while you’re at home asleep during your maintenance window, but also even if you’re modifying a simple VM and want the LEAST amount of time and effort possible, minimizing the downtime of the VM this will work also!

Shutting down your Virtual Machine via PowerCLI

This will seem simple, that is because it frankly is.  For the purpose of these descriptions I’m going to use a long VM name so you know this works if a VM has a name with quotes or spaces in it.  So for these purposes, the VM in question will be named, “VMware vCenter Log Insight 1.0.4”

Shutdown-VMGuest –VM “VMware vCenter Log Insight 1.0.4” –Confirm:$False

Wow, that was simple! So I run that and my virtual machine is shut down? Yes. Yes it is.

Changing your Virtual Machines vCPU and Memory

There you are, your VM is currently rocking out at 4GB of Memory and 1vCPU and you’re all “I really want this to be 8GB of memory and 2vCPUs! You know, because I did the analysis and notice that it’ll perform better, no I’m not just blindly following some vendors ‘best practices’” Ok, if you’re serious about this, here you go!

Set-VM “VMware vCenter Log Insight 1.0.4” –MemoryGB 8 –NumCPU 2 –Confirm:$False

Whoa, that was it? My VM now has its resources resized just like that? Yes. Just like that. You’re done!

Please Power my VM Back on! I have work to do!

Alright alright, you want to power your VM back on, you’ve made your change and you’re set! Well, here you go just as simply…

Start-VM –VM “VMware vCenter Log Insight 1.0.4”

And poof your VM will start back up and you’ll be set!

You can combine The Set-VM and Start-VM ya know!

Yea, that’s correct! You can instead run the last two components as a single one-liner command like;

Get-VM “VMware vCenter Log Insight 1.0.4” | Set-VM –MemoryGB 8 –NumCPU 2 –Confirm:$False | Start-VM

Unfortunately I’ve yet to find a way to combine the VM Shutdown component and the Set-VM Component without it breaking, thus…

Hey but wait, I thought you said this was scriptable

You’re all like, “I can’t just copy and paste these in sequence because my VM hasn’t shut down in time and I get an error trying to use CPU hot plug, and I can’t power the VM back on because it’s still shutting down!!!”    Yep, you got it, you’re absolutely correct! The initial steps were more to provide the steps of how you can go about modifying all of this from the PowerCLI command line, now if we’re really serious about this, here’s a multi-liner which will perform all of these tasks in sequence! Will this work if you don’t have VMware Tools installed? No. No it will not. Get your tools installed, ya’here! :)

  • $vm_name = Get-VM “VMware vCenter Log Insight 1.0.4”
  • ForEach ($vm in $vm_name){
  • $vm_name | Shutdown-VMGuest –Confirm:$False
  • Sleep 60
  • $vm_name | Set-VM –MemoryGB 8 –NumCpu 2 –Confirm:$False
  • $vm_name | Start-VM
  • }

A few things… I had to do this in a bulleted list because otherwise the spacing between it was ridiculous, and secondly, am I proud of the utter lack of elegance of this code? No. Not in the least. I think this is the ugliest thing I’ve ever seen. Does it work? Yes. It works QUITE well.  I can equally execute this as an Import-CSV to perform this against a whole series of Virtual Machines but that is outside the scope of this particular blog post. :)

If you happen to have a more elegant want to execute a VMGuest Shutdown while ALSO executing a Set-VM within the same command-set, preferably within one line, feel free to include it in the comments, otherwise this will work and can be scripted whether you’re performing against one virtual machine or more.   I personally will use this to script/execute easy modification of resources when I need to grow or shrink a virtual machines allocations without having to go through and ‘right click – wait’ over and over again, and especially so when I’m performing this against more than one virtual machine.

Good luck, I look forward to your comments, thoughts, and any improvements you may have! Hopefully this helps improve your own management!

Using PowerCLI to dump your permission structure in vCenter

So there I was pondering the question, “How can I pull the details of the Permission tab in vCenter without logging into all of my damn vCenters!”

I tossed around a few ideas of commands which didn’t give me what I wanted. I searched and found some really awesome scripts by @LucD22 and others which were awesome if I wanted to view the rights assigned to roles, assign rights to those roles, and import them respectively.  But my objective was far more simpler than that.   I wanted a very easy way to dump what Role is tied to which User accounts across my hundreds of vCenters in a simple fashion.

Lo and behold, here is the base result!

Get-VIPermission | Select Role, Principal, Entity, UID

Wow that was easy! but wait! WTF WHY DOES UID KEEP TRUNCATING! Hey, calm down, calm down… I have a fix for that. ;)   Also in that same fix, I really cared specifically who was granted the Administrative type roles, less so caring about Virtual Machine User, or Power User.  Thus the following two modifications took care of that.

Get-VIPermission | Where {$_.Role –eq “Admin”} | Select Role, Principal, Entity, UID | Export-CSV “C:\Temp\Rights.csv”

And there it went! Dumping all of the data I wanted into a CSV file which I could sort and manipulate unflustered and unfettered by the results!

What was really useful about this, is unlike just looking at the top level permissions tab, this will also drill down into sub-objects whereby I was able to find accts which were granted permission on a sub-part!   Hopefully this helps you as much as it helped me in discovering and respectively writing this :)

I’ll probably come back and revisit this script on a quarterly basis making sure no-one went rogue in the meantime. You should too. :)