Skip to content

Instantly share code, notes, and snippets.

@dsaves
Created July 5, 2018 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dsaves/6bb0794400782afe29b0af68c74a9711 to your computer and use it in GitHub Desktop.
Save dsaves/6bb0794400782afe29b0af68c74a9711 to your computer and use it in GitHub Desktop.
Powershell script for REST API over HTTPS
################################################
# Fix registry entries for SSL related dlls,
# otherwise SSL might not work on your
# Windows machine
################################################
regsvr32 Softpub.dll /s
regsvr32 Initpki.dll /s
regsvr32 Wintrust.dll /s
regsvr32 Mssip32.dll /s
################################################
# Configure the variables below for the Cluster
################################################
$RESTAPIServer = "your.ip.goes.here"
# Prompting for credentials
$Credentials = Get-Credential -Credential $null
$RESTAPIUser = $Credentials.UserName
$Credentials.Password | ConvertFrom-SecureString
$RESTAPIPassword = $Credentials.GetNetworkCredential().password
################################################
# Nothing to configure below this line - Starting the main function of the script
################################################
# Adding certificate exception to prevent API 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
#dsaves below
#############################################################################################
#ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
#################################################################################################
################################################
# Building API string & invoking REST API
################################################
$BaseURL = "https://" + $RESTAPIServer + "/api/v1/"
$SessionURL = $BaseURL + "session"
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))}
$Type = "application/json"
# Authenticating with API
Try
{
$SessionResponse = Invoke-RestMethod -Uri $SessionURL -Headers $Header -Method POST -ContentType $Type
}
Catch
{
$_.Exception.ToString()
$error[0] | Format-List -Force
}
# Extracting the token from the JSON response
$SessionHeader = @{'Authorization' = "Bearer $($SessionResponse.token)"}
###############################################
# Getting list of VMs
###############################################
$VMListURL = $BaseURL+"vmware/vm?limit=5000"
Try
{
$VMListJSON = Invoke-RestMethod -Uri $VMListURL -TimeoutSec 100 -Headers $SessionHeader -ContentType $TypeJSON
$VMList = $VMListJSON.data
}
Catch
{
$_.Exception.ToString()
$Error[0] | Format-List -Force
}
$VMList | Format-Table -AutoSize
###############################################
# Get some other REST API info
###############################################
$RestURL = "https://" + $RESTAPIServer +"/api/internal/cluster/me/name"
Try
{
$RestJSON = Invoke-RestMethod -Uri $RestURL -TimeoutSec 100 -Headers $SessionHeader -ContentType $TypeJSON
$data = $RestJSON.data
}
Catch
{
$_.Exception.ToString()
$Error[0] | Format-List -Force
}
#$data | Format-Table -AutoSize
$data
###############################################
# End of script
###############################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment