Recently I came across an inquiry that asked how to protect VMs running on a Nutanix cluster by adding them to a protection domain. I was intrigued on how to do this via a script. So I set about on a quest to build a menu driven Powershell script, which if you have seen some of my previous scripts when I worked for another Tech company, these are scripts I like to write. Here is what I came up with:
First, Pre-Requisites for my example script are below:
- Nutanix Powershell cmdlets installed – If you do not currently have them installed it is very easy to install them onto your Windows host. Here are the 10K foot steps:
- Login to your Nutanix cluster and click on the login icon in the upper right corner of the browser (the one with the username)
- Select “Download Cmdlets Installer”
- Run the installer from the host you would like the Nutanix Powershell cmdlets to be available on
- Nutanix Protection Domain created in advance of testing the script (not outlined in this post, but it’s very easy to do)
- Test VMs you can work with
- Additional cmdlet installed that will pull a stored credential from the Windows Credential Manager (see an older post I did on how to set this up located HERE , go to the section “Lets Get Started…Storing Credentials”)
The script that I wrote can be expanded upon or changed to suit your needs. So please feel free to change or modify whatever you want. Lets jump in:
Start out by creating a stored credential in the Windows Credential Manager. This will be used later in the script and will keep you from having to store passwords in scripts or having to be concerned with typing one in each time it’s run. The input details for the stored credential should be the username and password of the Nutanix cluster (i.e.: Prism) you will be working with as part of this script. To get to Credential Manager, just click on the Windows icon on your Windows 10 desktop and type “Credential”, then select “Credential Manager”. Click on “Windows Credentials” and then add a new Windows credential.
The first part of the script just confirms that you have specified a Windows Credential Manager entry. From this, the script will pull the credential username and secured password from the Credential Manager store. Absent the added variable, the script will stop and advised you to add in a credential entry:
# Script command to use credential manager to auto login the intended user for the script.
# Relies on the ability for the user to have a local entry in their credential manager and
# the use of an add-in module that pulls from the credential store the login details. This
# keeps from having to manually add credentials or store them in a file locally.
#
#
param(
[string]$wincredstor
)
if([string]::IsNullOrEmpty($wincredstor))
{
write-host “”
write-host “You must supply Windows credential manager target URI, for example:” -ForegroundColor Green
write-host “”
write-host “PS C:\data\scripts> .\ntnxvmmgt.ps1 marctmesvm” -ForegroundColor Red
write-host “”
write-host “Where marctmesvm is an entry created in the local Windows clients” -ForegroundColor Green
write-host “Windows Credential Store.” -foregroundcolor green
write-host “”
write-host “For more details on Windows Credential Manager see the following:” -ForegroundColor Green
write-host “http://windows.microsoft.com/en-us/windows7/what-is-credential-manager” -ForegroundColor Green
write-host “”
return
}
Set-ExecutionPolicy bypass
$ntnxuser = get-storedcredential $wincredstor | select -expand UserName
$ntnxpwd = get-storedcredential $wincredstor | select -expand Password
From there the script will ensure the Nutanix Powershell cmdlets are loaded and available. This will ensure that you don’t have to modify any Windows associated Powershell variables to ensure the cmdlets are available.
#
# Load the Nutanix Snap-ins to ensure cmdlets are available
$ntnxpsLoaded = Get-PSSnapin -Name NutanixCmdletsPSSnapin -ErrorAction SilentlyContinue | % {$_.Name}
if ($ntnxpsLoaded -eq $null){Add-PSSnapin NutanixCmdletsPSSnapin}
After that it will setup a function that can be called over and over to ensure you are logged into the cluster. Currently you will have to hardcode in the IP address for the individual cluster Prism IP. If you have a cluster virtual IP, it can be used. I will work to make this a passable parameter from the Powershell CLI, but know for now you will need to add in your “server” IP.
#
# Function created to call when connect to the SVM is necessary
# Easier than extra coding of the connect-nccontroller cmdlet
# Make sure to change the server from x.x.x.x to one within your environment
function Connectntnx
{
Connect-ntnxcluster -server x.x.x.x -username $ntnxuser -password $ntnxpwd -AcceptInvalidSSLCerts | format-table -property name,version | out-null
}
# Test variable output
#write-host “$ntnxuser”
#write-host “$ntnxpwd”
The script continues from there and builds the menu that will be used throughout to provide the admin with the specifics on what workflow they would like to accomplish. For now it only references a single working option, with references for “Future”. I did this to fill out text and not just have a single entry. Ideas for additional entries are: create / delete / update VMs, Restore VMs…etc. For now only Option 1 has additional code that works at this time.
#
# Start off by asking what tasks they are interested in completing
# In order to add additional tasks, add in additional write-host
# lines and then create a function to allow the option to be run
#
Write-host
Write-host “This script will allow you to do the following for a given cluster:”
Write-host “”
Write-host ” **List All VMs, all unprotected VMs or Add VMs to a Protection Domain” -foregroundcolor white
write-host “”
Do {
Write-host “#####################################################################” -foregroundcolor Green
Write-host “#####################################################################” -foregroundcolor Green
Write-host “”
Write-host “Please select from the following options:” -foregroundcolor cyan
Write-host “Option 1: List ALL VMs, all unprotected VMs or add a VM to a Protection Domain on a particular cluster” -foregroundcolor White
write-host “Option 2: Future…DOES NOT WORK” -ForegroundColor Magenta
write-host “Option 3: Future…DOES NOT WORK” -ForegroundColor Magenta
Write-host “”
Write-host “”
Write-host “#####################################################################” -foregroundcolor Green
Write-host “#####################################################################” -foregroundcolor Green
Write-host “”
$initoptions = Read-host “Please enter 1, 2 or 3”
write-host
} until ($initoptions -eq “1” -or $initoptions -eq “2” -or $initoptions -eq “3”)
# Call Connect to test
Connectntnx
From there the remaining of the script are the endpoints depending on options chosen, that initiate workflows. The switch statements within option #1 were easier to do that a series of if…then…else type statements. I tried to comment the script as much as possible. Should make following the flow fairly easily.
##########################
# If they select option 1
# jump to here
##########################
if ($initoptions -eq 1)
{
#
# List VMs on Nutanix Cluster
#
Do {
Write-host “This will list current AHV VMs, list all the unprotected VMs and Add VMs to a protection domain”
write-host “”
write-host “All = Display all VMs” -ForegroundColor Cyan
write-host “Unprotected = Display all unprotected VMs” -ForegroundColor Cyan
write-host “Protectvm = Protect a single unprotected VM” -ForegroundColor Cyan
write-host “”
$ntnxsav = read-host “Please enter All, Unprotected or Protectvm”
Write-host
} until ($ntnxsav -eq “all” -or $ntnxsav -eq “unprotected” -or $ntnxsav -eq “protectvm”)
#
# Using Powershell switch statement to keep from having a large if/else series of statements
# It is comparing the values for $list, $unprotect or $protect
#
# Requires that user type in list, unprotect or protect otherwise the above will loop continuing to ask the use to enter
# the correct statement. The switch statement is a compare of the value of the response to the available “functions” below it.
#
# Only valid responses are: add or remove
#
#
switch ($ntnxsav)
{
all{
#
# Calling the connectnx function
# This will connect to the Nutanix cluster hostname or IP when the script was started
#
write-host “”
write-host “Connecting to Nutanix cluster $ntnxclus as the user $ntnxuser” -foregroundcolor green
#
# Commented out for now, no need as the user is connected to the cluster earlier
#Connectntnx
#
# Retrives the names of the VMs, listing only the vmname, host running on and VMID
write-host “Time”
Get-Date
write-host “”
write-host “Displaying all the VMs and the NTNX host they are running on” -ForegroundColor Magenta
write-host “They are sorted alphabetically by VMName” -ForegroundColor Magenta
get-ntnxvm | where hypervisortype -eq kkvm | select vmname,hostname | Sort-Object vmname | ft -AutoSize
write-host “”
Read-host “Press the ENTER key to return to main menu…”
}
unprotected{
#
# Calling the connectnc function
# This will connect to the Nutanix cluster hostname or IP when the script was started
#
write-host “”
write-host “Connecting to Nutanix cluster $ntnxclus as the user $ntnxuser” -foregroundcolor green
write-host “”
#
# Retrives the names of the VMs, listing only the vmname, host running on
write-host “Start time” -foregroundcolor green
Get-Date
write-host “”
write-host “The current unprotected VMs on the cluster is:” -foregroundcolor green
write-host “”
get-ntnxunprotectedvm | where hypervisortype -eq kkvm | select vmname -ExpandProperty vmname | ft -AutoSize
}
Protectvm{
#
# Protecting a specific VM, will change later to all unprotected VMs
#
write-host “Listing all the currently unprotected VMs on this cluster” -ForegroundColor Magenta
write-host “”
get-ntnxunprotectedvm | where hypervisortype -eq kkvm | select vmname -ExpandProperty vmname | ft -AutoSize
#
# Select the name of the VM that you want to protect
#
#
write-host “”
write-host “Enter the name of the VM you want to Protect” -ForegroundColor Magenta
$ntnxvmname = read-host “Type in just the VM Name”
write-host “”
# Display all the available NTNX Protection Domains
write-host “Listing the current Protection Domains on the cluster” -ForegroundColor Magenta
Get-NTNXProtectionDomain | select name -ExpandProperty name
write-host “”
write-host “Enter the name of the Protection Domain you want the VM placed in” -ForegroundColor Magenta
# Capture the protection domain user input
$ntnxpd = read-host “Type in the cluster Protection Domain”
Add-NTNXProtectionDomainVM -pdname $ntnxpd -names $ntnxvmname
write-host “”
# confirm took by pulling vmdetails and listing only the vmname and the NTNX PD
write-host “Display the VM name and it’s new setting for Protection Domain” -ForegroundColor Magenta
get-ntnxvm | where vmname -eq $ntnxvmname | select vmname, protectiondomainname
}
}
}
As always, feel free to comment and if you have better ideas for the script, definitely open to new ideas. Below is example output as the script is being run:

