Advertisement
Workspace-Guru

HPMoonShotAPI.ps1

Mar 31st, 2019
653
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ### MoonShot API Boot
  2.  
  3. $username = "APIUSERNAME"
  4. $password = "APIPASSWORD"
  5. $Moonshot = "MOONSHOTFQDN"
  6. $Cartridge = "CartridgeName"
  7. $powerstate = "On/Press/PressAndHold"
  8.  
  9. ### Trust Certificate
  10. add-type @"
  11. using System.Net;
  12. using System.Security.Cryptography.X509Certificates;
  13. public class TrustAllCertsPolicy : ICertificatePolicy {
  14.    public bool CheckValidationResult(
  15.        ServicePoint srvPoint, X509Certificate certificate,
  16.        WebRequest request, int certificateProblem) {
  17.        return true;
  18.    }
  19. }
  20. "@
  21. [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
  22.  
  23. # Create the json for the webrequest
  24. $json_password = @{'UserName' = $username;'Password' = $password}
  25. $json_password = convertto-json $json_password
  26.  
  27. # Authenticate to the API and convert the JSON-response to a Powershell object to collect the token
  28. $json_token = Invoke-WebRequest -URI "https://$moonshot/rest/v1/Sessions" -Method POST -Body $json_password -ContentType 'application/json'
  29. $apiKey = $json_token.headers.'X-Auth-Token'
  30.  
  31. # Change PowerState
  32. $powerstate_json = @{'Power' = $powerstate}
  33. $powerstate_json = convertto-json $powerstate_json
  34.  
  35. $result = Invoke-WebRequest -URI "https://$moonshot/rest/v1/Chassis/1/Cartridges/$Cartridge" -Method Patch -Body $powerstate_json -ContentType 'application/json' -Headers @{"X-Auth-Token" = $apiKey}
Advertisement
RAW Paste Data Copied
Advertisement