Unleash the awesome superpowers of the Nutanix PowerShell cmdlets

073015_1027_Unleashthea1.jpgI don’t know if you are like me, but I’m a scripting nut. I figure that if I have to do something once, there is a fair chance I’ll have to do it again multiple times, sometimes months or even years later and I don’t want to have to bother to research it again, so why not simply script it now and sharpen my scripting skills in the process?

I also find that if you can script something, you have generally a better understanding of how it actually works as opposed to just pointing and clicking in a UI.

I started scripting using NT shell commands in Windows NT 4.0, and to this day I still have my beat up copy of Tim Hill’s « Windows NT Shell Scripting » which I use every now and then.

For a few years, I was also a hardcore Perl user and created all kinds of scripts for Windows and Active Directory management.

That was until PowerShell came out a few years ago. That scripting language is just something else. Once you start using it, you can’t stop. I’ve done tons of automation for Active Directory, Windows troubleshooting and VMware vSphere administration and reporting using the outstanding VMware vSphere PowerCLI extensions.

Which is why I’m really glad Nutanix came up with a bunch of PowerShell cmdlets that can help you automate operations on your Nutanix clusters. While those are relatively low touch solutions, there are certain features that can be tedious to setup for a large environment such as protection domains.

Others have already blogged about the Nutanix cmdlets, like Andre Leibovici‘s « Nutanix 4.0 PowerShell goes GA » or Kees Baggerman‘s excellent « Releasing the Nutanix Documentation Script », but I felt there wasn’t any good blog that took you through the process step by step and provided a few good examples of how to apply those cmdlets for true operational purposes. So here goes my two-cent.

 

Step 1 – Setting Nutanix PowerShell cmdlets up

First, let me start by saying that admittedly, the way the cmdlets have to be setup and used currently is not ideal. There isn’t a place on the web where you can simply download the cmdlets – you have to do it from Prism, the Nutanix web management interface.

Also, the built-in documentation of the cmdlets is somewhat lacking and you often have to dig into the support portal API reference document to get the cmdlets to do what you need them to do.

But enough ranting, let’s get down to business.

To install the cmdlets and get started:

  1. Connect to your Prism interface by opening the https://<cluster or CVM IP address>:9440/console/#login URL
  2. Click the Admin menu in the upper right corner and select Download Cmdlets Installer
  3. This downloads a NutanixCmdlets.msi file which you can then install on your Windows workstation or server
  4. You can then start the Nutanix cmdlets prompt by double clicking the NutanixCmdlets shortcut icon placed on your desktop

To double-check which version of the cmdlets you installed, use the following command:

…which should give you an output similar to the following:

That’s it! Fairly painless so far.

Step 2 – Running your first commands interactively

Just like with most other PowerShell extensions, the process with Nutanix cmdlets is quite straightforward:

  1. You start by connecting to a Nutanix cluster
  2. You then use commands to display, create, delete or modify objects in that cluster

To connect to a Nutanix cluster, you need to use the Connect-NutanixCluster cmdlet as in the following example:

The server, username and password are just plain text. I just used variable names in the example because you would typically set those up beforehand, but you could just as easily type them in the same command.

[box type=”info”] Note: At the time of writing (July 2015), the cmdlets were not updated with NOS 4.1.3 or NOS 4.1.4, so you will get a warning stating that the cmdlets version does not match the cluster version. This is also preventing scripts from executing properly because that prompt cannot be worked around. This issue is actively being worked on and should be resolved in the 4.5 release. In the meantime, you can call Nutanix support and get an updated msi package to work around this issue.[/box]

Assuming you connected successfully, you should see something similar to this:

As long as IsConnected displays True, you’re good to go.

What can you do from here? Before we move into specific examples, the full documentation is available on the Nutanix support portal. If you have access to that portal, (it requires registration), then you’ll find the official documentation for the cmdlets in the API Reference document. More specifically, the “About Nutanix PowerShell Cmdlets” section explains how to get started and the “PowerShell Scrip Tutorial” helps you get going. The “PowerShell Cmdlets Reference” section gives you the full references needed for advanced scripting.

For a publicly available list of supported cmdlets, you can also check out the awesome Nutanix PowerShell cmdlet Reference Poster in PDF format.

Of course, when you are done, you should disconnect from the Nutanix cluster by using the following command:

Step 3 – Using the Nutanix PowerShell cmdlets in scripts

Typically in PowerShell, all you need to do is include an Import-Module or Add-PSSnapin statement, and this is no different with the Nutanix cmdlets.

All you need to include in your script to be able to use them are the following two lines of code:

After that, you simply add your Connect-NutanixCluster statement and off you go automating your tedious tasks.

I have included as an attachment a couple of PowerShell script templates that I use which includes those two lines as well as:

  1. Some extra error control when loading the snap-in
  2. Code to add the vSphere PowerCLI snap-in as well (for the template_vsphere.ps1 template)
  3. An OutputLogData function to handle the display of messages in script execution. The messages are color coded (green for informational messages, orange for warnings and red for errors) and optionally can be sent to a log file as well.

Feel free to re-use and/or modify it for your own purposes. The rest of the examples used in this blog have been created using that template.

Example 1 – Initializing storage on a freshly installed Nutanix cluster

In this example, we will create a storage pool and container on a freshly installed Nutanix cluster. The script also gives you the option simply to create a container with or without deduplication and/or compression just to show how that is done.

What the script does exactly:

  1. It connects to the Nutanix cluster you specified
  2. It creates a storage pool with a default or specified name. If the storage pool already exists, it gives you a warning message and continues.
  3. It creates a container with the options you specified (compression/deduplication)
  4. Optionally, it mounts the container as an NFS datastore on all the hosts in the Nutanix cluster, or on all the hosts in a given vSphere cluster

This script was tested on NOS 4.1.2 and ESXi 5.5 update 2. Note that it does only minimal error control.

Example 2 – Setup a protection domain and consistency group for vSphere virtual machines in a given folder

Often, virtualization administrators group virtual machines that make up an application in one or more specific folder(s). Consistency groups in Nutanix enable taking synchronized crash-consistent snapshots for a group of virtual machines. Therefore, it makes sense to be able to create consistency groups based on how your virtual machines have been organized in folders in vCenter.

Specifically, the script will:

  1. Let you specify a vCenter server and a Nutanix cluster, along with one or more VM folder name(s). Scheduling options for the protection domain must also be specified as well as a retention policy for snapshots. You can also specify an interval in minutes if you are processing multiple folders and you would like to spread out the schedules of the protection domains snapshot operations.
  2. Create a protection domain on the Nutanix cluster named after each VM folder name.
  3. Create a consistency group in that protection domain also named after each VM folder and add all the VMs in that folder to that consistency group.
  4. Create a schedule for each protection domain using the local container as a target and at the specified date and time.
  5. Set the specified retention policy on each protection domain.
  6. Optionally, replicate once immediately so that your VMs are protected (use with caution when you have lots of consistency groups).

Note that the script does not cover VSS integration.

This script was tested on NOS 4.1.2 and ESXi 5.5 update 2. Note that it does only minimal error control.

EDIT (August 2016): I was asked to produce a script to enable existing protection domain editing by specifying VM names or part of VMs name.  I have included the source code below.

 

For more coding examples (including in other languages such as Python), make sure to check out the Nutanix GitHub repository.

Conclusion

Using PowerShell is easy, and so is using the Nutanix PowerShell cmdlets. While the Prism UI is very intuitive and easy to use, sometimes you just don’t want to click 100 times for something a script can get done in a few seconds. Now you have all the tools required to automate your environment, so why don’t you get started?

Let me know what you come up with and if you hit any problem.

Don’t forget to leverage the almighty Nutanix Community as well and once you’ve unleashed your scripting superpower, enter our Total Recode coding challenge for a chance to win a 4000$ drone, a 2500$ home lab or 2000$ in cash! Now who said coding skills couldn’t make you rich?

Stephane Bourdeaud

I am a Senior Consultant for Nutanix working out of France. I have over 18 years of experience in the IT industry including 15 as an infrastructure architect at IBM. I am a virtualization specialist holding VCP3, VCP4, VCP5, VCAP5-DCA and VCAP5-DCD certifications.