The figure is determined and held in the $freemem variable. After that we simply output two lines to show the amount in MB and GB. We use a simple function to divide the figure by 1024 and round it off, displaying the result with two decimal places. The figure needs to be divided by 1024 as the variable holds the amount in Kilobytes (KB), therefore to determine Megabytes (MB), we divide by 1024. The second figure for GB requires one more division.

On my quest to learn more about PowerCLI, I have been playing around with some more cmdlets in my lab. As a simple task, I have figured out how to tell ESX or ESXi hosts to enter and exit maintenance mode. Here’s how we do this. First of all ensure you are connected to your vCenter server instance using Connect-VIServer ServerName.

Enter maintenance mode:

Set-VMHost ESXi-01.noobs.local -State "Maintenance"

Exit maintenance mode:

Set-VMHost ESXi-01.noobs.local -State "Connected"

Set host to “disconnected” state:

Set-VMHost ESXi-01.noobs.local -State "Disconnected"

So now that we know the basics of setting the state of a VMware host, how about we get slightly more technical and perform one of the above operations on a bunch of hosts in one go? Powershell / PowerCLI is all about automation after all! Note that in the following script, I have also include a simple “if / else” statement to prompt the user running the script manually as we are about to send all ESX(i) hosts into maintenance mode! Use this at your own risk of course, it is just for demonstration purposes. You may want to modify to select hosts to enter maintenance mode on certain criteria. For example, all hosts in a particular cluster, or all hosts with a certain property. Here is the script I would use to perform the operation on all the ESX(i) hosts found in vCenter:

Once the script is executed, you should get a progress indicator whilst hosts are being dealt with. Afterwards you’ll get some output from each host listing its relevant Connection Status and statistics. Like so:

In the above example, we set some variables, and use some basic logic checking with an IF ELSE statement and an equal to (-eq) operator. We also see how to perform a few operations on ESX or ESXi hosts. I hope this helps anyone starting out with PowerCLI. Please do leave any comments, suggestions or improvements in the comments section!

Checking for any snapshots running on VMs in various clusters can be quite repetitive if done manually, looking through vCenter at each of your VMs. In the clusters I work with there are a LOT of VMs to check, and naturally I wanted to automate this process. Sure, I could rely on the vCenter alarms for snapshot size warning, but these are not completely reliable, as they only alert me when snapshots start growing large in size. I wanted something that would alert me to the presence of a snapshot regardless of its size. I therefore set about learning the basics of PowerCLI (as you can see in my last post) and searched around for some sample cmdlets that would help me retrieve a list of VMs with snapshots on them.

So here is the end result of running this snapshot checking script. It uses powershell cmdlets to generate an HTML email and sends it across to the address you specify. You will of course need to ensure you can connect out on port 25 for mail and have authentication on your mail server (or being sending from and to a domain hosted on your mail server (i.e. connecting to relay mail internally). Enter your mail server, to, and from details in the script to customise it. You’ll also need to authenticate with your vCenter server before running the script of course – you could use a cmdlet in the script to do this automatically. I have just been manually authenticating for now as I have not yet deployed this in production and have just been testing.

Another point worth mentioning – you should change the path that the report is saved to on disk – in my script it is set to F:\, so just modify this to suit your environment. Kudos to Andrew at winception for his Snapshot checking code – I have used a lot of it above, but modified it somewhat to include additional information, and style the HTML table so that it is much easier on the eyes. I also added the e-mail functionality to the script. The following is a screenshot after I executed the script in PowerCLI manually. You would of course look to automate the process by scheduling this script in on your machine.

Enjoy, and please drop any comments, improvements or feedback in the comments section!

So today is the first time I am trying out PowerCLI for vSphere. Yes, I know I am late to the game in the VMware scene, but I hope to start learning more about PowerCLI and start automating some tasks that are currently being done manually where I work.

to connect and get a list of VMs. I guess this is my “Hello World” start out with PowerCLI then. If anyone has any tips or quick and easy cmdlets to run in PowerCLI to get information back, please drop a comment with them below. I would also love to know how to iterate through a list of VMs and check whether they have snapshots or not! That would be a great start to what I am looking to achieve.