REST Call (GET) with Powershell

This post is about “How can I use Powershell to get data from my vROps Instance over the API”?

It sounds really easy and the line of code for an invoke is really short but there are some painful points.
So we will start with a GET Method with Powershell for vROps.
This is the REST Call:

invoke-webrequest -credential $cred -Uri Invoke-RestMethod -Credential $RESTcredential -Uri https://vrops.pso.local/suite-api/api/adapters -ContentType "application/json" -Method Get

The $cred variable is based on the following code. It is important for me to haven’t the password in plain text.

$user = "admin"
$secpasswd = Get-Content C:\Users\kbruesch\Desktop\Scripts\RESTCallCreds.txt | ConvertTo-SecureString
$RESTcredential = New-Object System.Management.Automation.PSCredential($user, $secpasswd)

My biggest problem with this CALL was that I got always the same error message:
the underlying connection was closed: an unexpected error occurred on a send.

The solution for me wasn’t to easy allow self-signed certificates with: [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

I used this implementation in my script:
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

After your Invoke-Request is running you get an Status Code of 200 which means it was succeeded:
Bildschirmfoto 2018-04-18 um 22.40.52

Now you can use the content part to get all the data you want like Adapter Instance Name to Adapter Instance ID

Leave a Reply

Your email address will not be published. Required fields are marked *