Thursday, October 25, 2018

HPE OneView Invoke-RestMethod

HP's OneView Management Console has a robust API with a number of SDKs, recently we've needed to automate a number of tasks after installing new Synergy Frames that have OneView composer modules and are using the self signed certificates that are default. A recent task was to connect to the API and do some REST calls to get and set some basic configuration tasks.

Using Powershell's Invoke-RestMethod would return a couple different errors. One of which was Could not establish trust relationship for the SSL/TLS Secure, and searching around on Google a number of posts would say to add:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }

even that would throw an error. After trying different settings, I found the following to work:

$appliance = "192.168.1.100"
$url = "https://$appliance"
$web = New-Object Net.WebClient
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
$output = $web.DownloadString($url)

$user = @{
    userName= "Administrator"
    password= "password" 
    authnHost= "$appliance"
    authLoginDomain= "Local"
}
$json = $user | ConvertTo-Json
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-type", 'application/json')
$headers.Add("Accept", 'application/json')
$uri = $url + '/rest/login-sessions'
$response = Invoke-RestMethod -Uri $uri -Method POST -Headers $headers -Body $json -ContentType 'application/json' 

$auth = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$auth.Add("Auth", $response.sessionID)
$auth.Add("X-Api-Version", '300')


With that, I was allowed to get calls from other URIs such as getting the ethernet networks attached to the enclosure with and get other settings.

$uri = $url + '/rest/ethernet-networks'
$Networks = Invoke-RestMethod -Method GET -Headers $auth -Uri $uri

Enjoy!