Get a list of VMs in a cluster and find their HA Restart Priority with PowerCLI

 

This is just a quick post today using one of the most common PowerCLI cmdlets, Get-VM.

 

I needed to find a list of VMs in a specific cluster, and grab their respective HA Restart Priority settings. PowerCLI makes this nice and simple. First of all, connect to your vCenter server, then find the name of your cluster you would like to search. You can use the Get-Cluster cmdlet on its own to list all clusters in a specific vCenter installation.

 

Connect-VIServer yourvcenterservername
Get-Cluster

 

Next, use this one liner to list all the VMs in your specified cluster along with their respective HARestartPriority settings. This will also sort the list in alphabetical order and export it into a CSV file in C:\temp. If you wish to rather just list the items in your shell instead, remove the Export-CSV bit at the end.

 

Get-Cluster "yourclustername" | Get-VM | Select Name,HARestartPriority | Sort Name | Export-CSV C:\temp\vmrestartpriorities.csv

PowerCLI – Automate adding NFS Datastores to a cluster of ESX or ESXi hosts

 

The other day I needed to add three NFS datastores to a bunch vSphere ESX hosts in a specific cluster. Rather than go through each host in vCenter individually, adding the datastore using the Add Storage wizard, I thought I would script the process in PowerCLI and get it done in a more automated fashion. Using PowerCLI automation, this helped me save some time. I had about 7 ESX hosts to add the Datastores to, so doing this manually would have taken twice the time it took me to whip up this script and run it. Plus, this can be used in the future for other Datastores or other clusters by simply modifying the script and re-running it.

 

Here is the script:

 

# PowerCLI script to add NFS datastores to ESX/ESXi hosts in a specified Cluster
# Only does this to hosts marked as "Connected"
$hostsincluster = Get-Cluster "Cluster 1 - M" | Get-VMHost -State "Connected"
ForEach ($vmhost in $hostsincluster)
{
    ""
    "Adding NFS Datastores to ESX host: $vmhost"
    "-----------------"
    "1st - MER001 - NAS-SATA-RAID6 (Veeam Backups)"
    New-Datastore -VMHost $vmhost -Name "MER001 - NAS-SATA-RAID6 (Veeam Backups)" -Nfs -NfsHost 10.1.35.1 -Path /share/VeeamBackup01
    "2nd - MER002 - NAS-SATA-RAID6 (ISOs)"
    New-Datastore -VMHost $vmhost -Name "MER002 - NAS-SATA-RAID6 (ISOs)" -Nfs -NfsHost 10.1.35.1 -Path /share/Images01
    "3rd - MER003 - NAS-SATA-RAID6 (XenStore01)"
    New-Datastore -VMHost $vmhost -Name "MER003 - NAS-SATA-RAID6 (XenStore01)" -Nfs -NfsHost 10.1.35.1 -Path /share/XenStore01
}
"All Done. Check to ensure no errors were reported above."

 

So the script above looks for ESX or ESXi hosts in a specified cluster that are in a “Connected” state – i.e. they are not disconnected in vCenter (we wouldn’t want to try add Datastores to hosts that don’t exist!). So we use the Get-Cluster cmdlet to say we are only concerned with hosts in this particular cluster (specified by the “Cluster 1 – M” name in my case. Obviously change this to the name of your cluster you will be working with.) We then use Get-VMHost -State “Connected” to list all of the hosts in this cluster that are in a connected state. In my example I had 2 x ESX hosts that were in a disconnected state, and I didn’t want to include these, so this part worked nicely. This list of hosts in then assigned to the $hostsincluster variable. We then use the ForEach loop to iterate through each host in this list of hosts and do the bit in-between the curly brackets for each host.

 

In my case you may notice that I am adding Datastores from the same NFS (NAS) server. They are just being mounted to different paths on the server and being given different names. I had three Datastores to add, so therefore use the New-Datastore cmlet three times for each host. You will need to adjust this to your needs – maybe you just need to add one datastore to each host, therefore remove the two extra New-Datastore cmdlet parts. Also remember to adjust the -NfsHost and -Path sections to suit your own environment.

 

We could improve on the above script by making it more customisable for future / others to use. Lets give that a quick go then and use variables to define everything at the top of the script. This means that the variables can be changed at the top of script without worrying too much about reading through the whole script to check for things to change. We’ll also add a Connect-VIServer cmdlet in there in case you have not already connected to your vCenter server and authenticated in your PowerCLI session that is running the script.

 

# PowerCLI script to add NFS datastores to ESX/ESXi hosts in a specified Cluster
# Only does this to hosts marked as "Connected"

# Define our settings
$vcserver = "vcenter01"
$clustername = "Cluster 1 - M"
$nfshost = "10.1.35.1"
$nfspath1 = "/share/VeeamBackup01"
$nfspath2 = "/share/Images01"
$nfspath3 = "/share/XenStore01"

# Connect to vCenter server
Connect-VIServer $vcserver

# Do the work
$hostsincluster = Get-Cluster $clustername | Get-VMHost -State "Connected"
ForEach ($vmhost in $hostsincluster)
{
    ""
    "Adding NFS Datastores to ESX host: $vmhost"
    "-----------------"
    "1st - MER001 - NAS-SATA-RAID6 (Veeam Backups)"
    New-Datastore -VMHost $vmhost -Name "MER001 - NAS-SATA-RAID6 (Veeam Backups)" -Nfs -NfsHost $nfshost -Path $nfspath1
    "2nd - MER002 - NAS-SATA-RAID6 (ISOs)"
    New-Datastore -VMHost $vmhost -Name "MER002 - NAS-SATA-RAID6 (ISOs)" -Nfs -NfsHost $nfshost -Path $nfspath2
    "3rd - MER003 - NAS-SATA-RAID6 (XenStore01)"
    New-Datastore -VMHost $vmhost -Name "MER003 - NAS-SATA-RAID6 (XenStore01)" -Nfs -NfsHost $nfshost -Path $nfspath3
}
"All Done. Check to ensure no errors were reported above."

So as you can see we have now defined the name of the cluster, our NAS/NFS server and three paths to different NFS shares at the top of the script, then just referenced these variables later on in the script. This means we can now easily adjust the defined variables at the top of our script in the future to work with different clusters, NAS/NFS servers or paths. The output of your final script when run should give you a nice view of what has happened too. It will output that it is adding NFS Datastores to each host it iterates through, and if it comes across any errors those should be marked in red as PowerShell / PowerCLI normally would do, allowing you to amend or update any details as necessary. PS, don’t forget to change the name of each Datastore in the script to something of your own choice (it is the part after the -Name parameter in each New-Datastore line).

 

Here is the download for the full script (with latest improvements):

[download id=”5″]

 

How to set up a VMware vSphere Lab in Virtual Machines, with DRS and HA

 

I recently wrote a (reasonably!) lengthy article on how to set up your own VMware vSphere lab or test environment consisting entirely of Virtual Machines, running off of one piece of host hardware. This is really handy as a lot of people new to Virtualization often think they need to purchase full on server equipment to create a white box, or find second hand servers off of eBay. Even more often, they make the mistake of overlooking the CPU feature set required to run vSphere – Hardware Virtualization, buying 64bit capable servers (good), but lacking the Intel VT or AMD-V feature-set required for vSphere (bad!)

 

This is when running everything virtualized comes in really handy. As well as keeping your hardware and lab requirements/size down, you have everything you need all in one installation of VMware Workstation. You’ll also be able to test out some really cool features that vSphere / vCenter Server has to offer – such as HA (High Availability) and DRS (Distributed Resource Scheduling). In the article I also make reference to a few best practises to have when configuring the real deal for production use. I hope this comprehensive guide is useful for those of you looking to set something like this up!

 

VMware lab consisting - nested VMs running in Virtualized ESXi hypervisors.

 

Read the article here on Simple-Talk.com to get started and see how its all done!

 

 

An update on what I have been busy with lately…

Thought I would do a quick blog post on what I have been busy with lately.

1. My first iPhone / iPod Touch game (released on Cydia for Jailbroken devices)

So this is something I have been busy with over the last few months – coding bits and pieces here and there whenever I get a bit of spare time on my hands. Its nothing special – just a simple Maze game. You use your device’s accelerometer to navigate your character through 5 maze levels. If you touch the walls you lose health. The longer you take to complete the maze, the more score you lose too. So the point is to get through in the quickest possible time without touching walls. I learnt the basics of how to work with the accelerometer, game loops, twitter integration, a little bit of PHP and mySQL for the Highscore system and a fair amount of general objectiveC code. There are a couple of bugs in the game at the moment (like the way you get a little stuck on walls – issue with my game loop) that I don’t really have time to sort out at the moment. But hopefully I’ll get more time in the near future to figure out my mistakes and fix these. You can check the game out in more detail here or you can download it for Jailbroken Apple devices on Cydia. Search for “Speed Maze”.

2. Moving

Well we’re moving house in the next month or so. We have found a place a little closer in to London that is going to offer far more room, an awesome garden, park across the road, and to top it all off, its in a nice quiet cal-de-sac. As such, I have been taking the opportunity to eBay some surpluss hardware and gadgets I have had lying around for a while. This includes various PCs and bits of hardware I have had lying around, plus around 20U worth of Dell PowerEdge servers! (See image above).

3. Other

My home VMware lab – I have also been building various ESX and vCenter labs here at home to play with in a non-production environment. This is great as it allows me to test all kinds of crazy things I really don’t want to try out at work! I have chopped and changed the hardware, but finally have two different labs going. One is a nested set up of virtualized ESX 4.0 hosts, running under a main ESX 4.0 host if you see what I mean.  The other is running on a PowerEdge 6850 server at the moment – 4 x Xeon 3.16GHz CPUs and 4GB RAM. The issue I have here is that there is no Intel VT (hardware virtualization) support on these processors. So although ESX 4.0 runs OK, I can only run 32-bit VMs for now. Exchange 2010 and other 64-bit VMs will have to stay on my main gaming PC for now then. I also found this great WordPress plugin by lynxbat on Twitter. Once set up, it displays statistics from your VMware ESX host or vCenter Server. You can take a look at my current lab stats on the right in the sidebar. Get the plugin over here: WP-vSphereStats. Apart from that, we have also been planning one or two soon to be taken, well deserved holidays. We’ll be off to France soon, after which we’ll be taking a nice long holiday in South Africa. Excited to see friends and family again soon!