Using Powershell with the Platform API - The Basics


Using Powershell with the Platform API - The Basics




Note: This topic is part of the Isilon SDK - Isilon Info Hub.

Introduction
With the introduction of the new RESTful platform API (PAPI) there is now a much easier and more elegant way to automate and manage Isilon clusters using Powershell. This article will explain the basics on how to connect to a cluster using Powershell and the PAPI.

It is highly recommended that you download and read the PAPI reference guide for your version of OneFS here.

Because the PAPI provides access to the cluster via REST you can manipulate resources using HTTP methods like GET, POST, PUT, and DELETE. The representations of objects and collections are exchanged as JSON formatted documents. To use the PAPI you will need to enable HTTP on the cluster by logging into the WebUI and navigating to: Protocols > HTTP Settings > Enable HTTP.

Powershell v3

Powershell v3 has built-in methods that simplify RESTful access. You can check your current Powershell version by looking at the $PSVersionTable value:



If you don't have Powershell v3 download and install .NET Framework version 4 found here.
Then download and install the appropriate v3 management framework here.

Connecting to the cluster

The below script accepts parameters for an Isilon IP address or hostname, a username and password:

# Accept input parameters
Param([String]$isilonip,[String]$username,[String]$password)

To access the resources on the cluster we will use the "Invoke-RestMethod" with the appropriate parameters. This method takes several parameters such as a URI string, a body (used for a POST), headers, etc. The following code will build the header information we need and the base URL for access:

# Encode basic authorization header and create baseurl
$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ':' + $password)
$EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization)
$headers = @{"Authorization"="Basic $($EncodedPassword)"
$baseurl = 'https://' + $isilonip +":8080"

In this example we are accessing quotas. Add it to the base URL to create the final URI that we'll pass to "Invoke-RestMethod". If you look at the PAPI documentation you'll see that a collection of resources are accessed in the following format "/platform/1/<namespace>/<collection-name>". If for example, we want to get the collection of objects representing all of the quotas defined on the cluster we need to get "/platform/1/quota/quotas".

$resourceurl = "/platform/1/quota/quotas"
$uri = $baseurl + $resourceurl

Now we call Invoke-RestMethod and then access to the returned object that contains the quota collection:
$ISIObject = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
$ISIObject.quotas

Here's an example of the script output:




There is a list of all of the quotas and their properties defined on the cluster. Using this method, PSv3 automatically converts the JSON output into objects.

Script body:
######################################################################################################################################################
#
# Created: 09/18/2013 by Jeremy Smith, EMC NAS Specialist Central Division
# Modified:
#
#
# ps_isilon_get_quotas_via_papi.ps1 - get all quotas on an Isilon cluster
# Note: You need to be at OneFS 7.0.2 or greater to leverage the PAPI and HTTP Access must be enabled on the cluster.
#
# PARAMETERS
#
# -isilonip = node IP
# -username
# -password
# -path = /ifs directory path containing the directories that need quotas
# -quoatasize = size of quota bytes (ex "500000")
#
#
# EXAMPLE
# .\ps_isilon_get_quotas_via_papi.ps1 -isilonip 10.10.10.1 -user root -password P@ssword1
#
#
########################################################################################################################################


# Accept input parameters
Param([String]$isilonip,[String]$username,[String]$password)


# With a default cert you would normally see a cert error (code from blogs.msdn.com)
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;

public class TrustAll : ICertificatePolicy {
public TrustAll() {}
public bool CheckValidationResult(
ServicePoint sPoint, X509Certificate cert,
WebRequest req, int problem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = new-object TrustAll



#if the correct parameters were not passed we exit after a message
if (!($isilonip -and $username -and $password )) {
write "failed to specify parameters";
write "Example: .\ps_isilon_get_quotas_via_papi.ps1 -isilonip 192.168.1.2 -username root -password p@ssw0rd1" ;
exit
}




# Encode basic authorization header and create baseurl
$EncodedAuthorization = [System.Text.Encoding]::UTF8.GetBytes($username + ':' + $password)
$EncodedPassword = [System.Convert]::ToBase64String($EncodedAuthorization)
$headers = @{"Authorization"="Basic $($EncodedPassword)"}
$baseurl = 'https://' + $isilonip +":8080"


# Get all defined quotas
$resourceurl = "/platform/1/quota/quotas"
$uri = $baseurl + $resourceurl
$ISIObject = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get
$ISIObject.quotas


Article ID: SLN319397

Last Date Modified: 02/03/2020 11:59 AM

Rate this article

Accurate
Useful
Easy to understand
Was this article helpful?
0/3000 characters
Please provide ratings (1-5 stars).
Please provide ratings (1-5 stars).
Please provide ratings (1-5 stars).
Please select whether the article was helpful or not.
Comments cannot contain these special characters: <>()\
characters left.