RSS

Tag Archives: restapi

PS: Getting a virtual machine’s properties in vRA

Getting the properties of a VM provisioned in vRA is not straight forward, you need to get those information from the IaaS Web server instead of from vRA

This site shows you how you can get to it.

I am not sure which version the author has, but in version 7.2 the properties are not so straight forward and they strangely don’t expand when exported to a JSON file. However let’s first deal with the NTLM authentication on IaaS Web via a Invoke-RestMethod call. Its actually very straight forward, but hard to find; simply speaking, you use get-credential to capture the windows credentials and input it with -credential parameter. Read the rest of this entry »

Advertisements
 
Leave a comment

Posted by on August 2, 2018 in powershell, vmware

 

Tags: ,

PS: Adding nodes to JSON object

Whilst working on vRA REST API to provision new virtual machines, one of task was to get the template for that catalog item and fill in required values.

One of the values common to most templates is the datacenter_location value. By default it will be null. That is not a big deal if you don’t care which datacenter your VM is being provisioned to, but what if you want to specify a location?

...
"_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location":  null,
...

In this case the values should be as such

 "_cluster": 1,
"_hasChildren": false,
"cpu": 1,
"datacenter_location": {
               "classId": "Infrastructure.Compute.DataCenterLocations",
               "id": "DC1"
            },

This is how you can add the values into the JSON object (I am assuming that you already gotten the bearer token in the $vratoken variable) Read the rest of this entry »

 
Leave a comment

Posted by on August 2, 2018 in powershell, vmware

 

Tags: , ,

Formatting XML output XML file

Below is a format-xml script which a lot of helpful scripters posted on the net. You can make a REST API call and use it to indent the output.

For example, you can indent the XML from an NSX REST API call.

#assumming all other pre-requisites for this call is done
$head = #this contains the authorisations string
$Request = "https://nsxmrg/api/3.0/edges"
[xml]$r = Invoke-WebRequest -Uri $Request -Headers $head -ContentType "application/xml" -ErrorAction:Stop

format-xml1

Read the rest of this entry »

 
Leave a comment

Posted by on April 17, 2018 in powershell

 

Tags: , ,

PS: Making a generic NSX REST api calls

Here is a script that I picked up and mod to make generic GET calls to NSX via REST api. Why did I wrap the rest api call into a function? This is to allow you to build out other logic for processing the output after making the call. For example, you may want to modify the script to get a list of edges and check their statuses.

<#
.SYNOPSIS
   Executes NSX Rest api GET method and returns the XML results
.DESCRIPTION
   Use this script to execute rest api calls to NSX
.NOTES
   File Name  : Get-NSXQuery.ps1
   Author     : Not me!!
   Version    : 1.0
.OUTPUTS
   XML formatted object from NSX's REST api call
.PARAMETER NSXMgr
   FQDN or IP of NSX manager
.PARAMETER ReqText
   api request string, e.g. "/api/3.0/edges"
.PARAMETER Cred
   secure string credentials from get-credentials
.EXAMPLE
.\Get-NSXQuery.ps1  -nsxmgr nsxmgr_fqdn -reqtext "/api/3.0/edges" -cred $cred
#>
param(
[Parameter(Mandatory=$true)]
[String]$NSXMgr,
[Parameter(Mandatory=$false)]
[String]$reqtext,
[Parameter(Mandatory=$false)]
[PSCredential]$Cred
)

function Get-NSXRestCall {

[CmdletBinding()]
    param(
        [Parameter(Mandatory=$true,Position=0)]
        [String]$Request,
        [Parameter(Mandatory=$false,Position=1)]
        [String]$Username,
        [Parameter(Mandatory=$true)]
        [String]$Password
    )

    Process {

    ### Ignore TLS/SSL errors
    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

    ### Create authorization string and store in $head
    $auth = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Username + ":" + $Password))
    $head = @{"Authorization"="Basic $auth"}

    ### Connect to NSX Manager via API
    $r = Invoke-WebRequest -Uri $Request -Headers $head -ContentType "application/xml" -ErrorAction:Stop
    if ($r.StatusCode -eq "200") {Write-Host -BackgroundColor:Black -ForegroundColor:Green Status: Connected to NSX successfully.}
    else {Write-Host -BackgroundColor:Black -ForegroundColor:Red Status: Error connecting to NSX!}
    return [xml]$r.Content

    } # End of process

}

if (!$Cred) {
  $Cred = get-credential -Message "Please enter your credentials"
}

$request = "https://" + $NSXMgr + $reqtext
$rxml = Get-NSXRestCall -request $request -username $Cred.Username -password $Cred.GetNetworkCredential().password

return [xml]$rxml
 
Leave a comment

Posted by on April 17, 2018 in powershell

 

Tags: ,

PS+REST:NSX Central CLI using REST API

As a second part from my previous post, I modified the script to run Central CLI. One thing to note, you need to add “accept”=”text/plain” into the header when running central CLI command or you will get 406 errors (sorry, I found this out in one of the blogs, but couldn’t give the proper credits).

Lastly the request returns a bunch of text and not XML or JSON.

nsxoutput

Read the rest of this entry »

 
Leave a comment

Posted by on February 15, 2018 in powershell, vmware

 

Tags: , , ,