«

»

Feb 20 2014

How to Establish PowerShell Connection to Nutanix and Execute your 1st Query

In my article Nutanix Automation, Policies and the SDDC I discussed how Nutanix exposes all features and functions via REST API, enabling programmatic access to datacenter services within mainstream enterprises.
 
The current list of features and services that can be fully managed by programmatic access is long and include the following:

  • Alerts
  • Authconfig
  • Cluster
  • Containers
  • Disks
  • Events
  • Hosts
  • Http proxies
  • Protection Domains (Disaster Recovery)
  • Remote Sites
  • SNMP
  • Storage Pools
  • Vdisks
  • VMs
  • Vstores

 
In this article I demonstrate how each of these API calls can be used for powerful datacenter automation using PowerShell. Currently PowerShell calls must be done using the Invoke-RestMethod method, but Nutanix will soon be releasing a fully featured PowerShell snap-in, making automation more efficient and easier.
 
Establishing a connection to PRISM and retrieving cluster information:

add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

$Uri = "https://10.20.18.10:9440/PrismGateway/services/rest/v1/cluster/"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))}
$username = "username"
$password = "password"
Invoke-RestMethod -Method Get -Uri $Uri -Headers $Header

 
Results for the Invoke-RestMethod method using PrismGateway/services/rest/v1/cluster. (Click to Enlarge)

 

Try replacing /cluster with any of the methods listed above and you will start to get the individual properties for VMs, VMDKs, vStores, Storage Pools etc…
 
This article was first published by Andre Leibovici (@andreleibovici) at myvirtualcloud.net.

Similar Posts:

Permanent link to this article: http://myvirtualcloud.net/?p=5938

3 comments

1 ping

  1. Josh Sinclair

    To get the connection to authenticate correctly I needed to put the $username and $password instantiations before the $Header instantiation.

    Also it is worth noting that Invoke-RestMethod was introduced in Powershell 3.0.

  2. Dominic

    I try the code about on a Windows 8.1 client and getting the following error

    Invoke-RestMethod : HTTP Status 401 – Bad credentials
    type Status report
    message Bad credentials
    description This request requires HTTP authentication (Bad credentials).
    Apache Tomcat/6.0.35
    At C:\POC\PS.ps1:21 char:1
    + Invoke-RestMethod -Method Get -Uri $Uri -Headers $Header
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
    eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

    I know the username and password is correct. What am I missing?

    1. Andre Leibovici

      @Dominic, I had that before with complex passwords. Try setting your user password to something simpler, without special characters, and see if works. Powershell require special handling with special characters in a string.

  1. Disaster Recovery – Failover and Failback with Nutanix » myvirtualcloud.net

    [...] « How to Establish PowerShell Connection to Nutanix and Execute your 1st Query [...]

Leave a Reply