Using the API with Windows PowerShell

This sample Windows PowerShell code performs authentication, issues an example GET request, performs a POST operation (in this case, renaming a backup), and monitors the status of the operation using a task instance.

When using Windows PowerShell to develop clients:

  • Allow the use of self-signed SSL certifications, as follows: [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }

  • Alternatively, you can use the -Insecure option.

  • Use PowerShell version 3 or above to support Invoke-RestMethod.

  • HTTP Basic Authentication requires the manual creation of the base 64 encoding, as follows:

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "simplivity","")))
Invoke-RestMethod -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$body = @{grant_type='password';username='username';password='password'} -Method POST

Consider the following code:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }
$token =  Invoke-RestMethod -Uri -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -Body $body -Method Post

Which returns a response similar to the following:

access_token  : 45718cb1-528b-430f-8a6d-89e25ae9c7ea
token_type    : bearer
expires_in    : 83649
scope         : read write
updated_at    : 1459805657628

This example shows how to make a GET call for all backups:

$header = @{Authorization='Bearer '+$token.access_token}
$backups = Invoke-RestMethod -Insecure -Header $header -Uri

This example finds the type of the 22nd backup: $backups.backups[22].type

This example renames this backup:

$uri = ''
$uri += $backups.backups[22].id
$uri += '/rename'
$body = @{backup_name='new_name'}
$body = $body | ConvertTo-Json
TunableSSLValidator\Invoke-RestMethod -Insecure -Header $header -Uri $uri -Method Post -Body $body

Sample code

# Set the base URL for REST API requests.
$BASE_URL = 'https://[host]/api/'

# Set the username and password.
$hms_username = 'HMS_USER'
$hms_password = 'HMS_PASS'

# Allow the use of self signed certificates.
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $True }

# Create a base64 encoding for HTTP Authentication.
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "simplivity","")))

# Create a JSON body with username, password, and grant_type.
$body = @{grant_type='password';username=$hms_username;password=$hms_password}

# Authenticate user and generate access token.
$url = $BASE_URL+'oauth/token'
$header = @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$response= Invoke-RestMethod -Uri $url -Headers $header -Body $body -Method Post
$access_token = $response.access_token;

# Add the access_token to the header.
$header =@{Authorization='Bearer '+$access_token}

# Issue a GET request: GET /backups.
$url = $BASE_URL+'backups'
$backups = Invoke-RestMethod -Header $header -Uri $url

# Issue a POST request: Rename the second backup from the GET results.
# Find the ID of the second backup from the GET results.
$backupid = $backups.backups[2].id

# Create a JSON body for the rename action.
$body = @{backup_name='new_name'}
$body = $body | ConvertTo-Json

# Form the URI.
$url = $BASE_URL+'backups/'
$url += $backupid
$url += '/rename'

# Issue the POST operation and expect a task object in return.
$response = Invoke-RestMethod -Header $header -Uri $url -Method Post -Body $body -ContentType 'application/vnd.simplivity.v1+json'

# Monitor the status of the rename operation by using a loop to query the task while this task is IN_PROGRESS.
# The state field in the JSON response body indicates the status.
$taskid = $
$state = $response.task.state
$url = $BASE_URL+'tasks/'+$taskid
  while ($state -eq 'IN_PROGRESS')
    # Wait one second and try again.
    Start-Sleep -s 1
    $response = Invoke-RestMethod -Header $header -Uri $url
    $state = $response.task.state

# Print out the task result.

Related Blogs

Roy Atkins

HPE SimpliVity PowerShell Module

May 16, 2019
Ron Dharma

Using Postman with HPE SimpliVity

Nov 9, 2018

HPE Developer Newsletter

Stay in the loop.

Sign up for the HPE Developer Newsletter or visit the Newsletter Archive to see past content.

By clicking on “Subscribe Now”, I agree to HPE sending me personalized email communication about HPE and select HPE-Partner products, services, offers and events. I understand that my email address will be used in accordance with HPE Privacy Statement. You may unsubscribe from receiving HPE and HPE-Partner news and offers at any time by clicking on the Unsubscribe button at the bottom of the newsletter.

For more information on how HPE manages, uses, and protects your personal data please refer to HPE Privacy Statement.