Advertisement
Demonslay335

sepm-api-test.ps1

Oct 2nd, 2018
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. # Ignore self-certs
  2. if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
  3. {
  4. $certCallback = @"
  5. using System;
  6. using System.Net;
  7. using System.Net.Security;
  8. using System.Security.Cryptography.X509Certificates;
  9. public class ServerCertificateValidationCallback
  10. {
  11. public static void Ignore()
  12. {
  13. if(ServicePointManager.ServerCertificateValidationCallback ==null)
  14. {
  15. ServicePointManager.ServerCertificateValidationCallback +=
  16. delegate
  17. (
  18. Object obj,
  19. X509Certificate certificate,
  20. X509Chain chain,
  21. SslPolicyErrors errors
  22. )
  23. {
  24. return true;
  25. };
  26. }
  27. }
  28. }
  29. "@
  30. Add-Type $certCallback
  31. }
  32. [ServerCertificateValidationCallback]::Ignore()
  33.  
  34. # TLS 1.2 requires PowerShell 3.0+
  35. [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
  36.  
  37. $SEPMClass = New-Object -TypeName PSObject -Prop @{Username = $null; Password = $null; _BaseUrl = $null; _Token = $null}
  38.  
  39. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_InitSession" -Value {
  40. If(-Not $this._SID.Length){
  41. $this._Login()
  42. }
  43. }
  44.  
  45. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_HandleResponse" -Value {
  46. param([Parameter(Mandatory=$True)]
  47. $request)
  48.  
  49. If($request.StatusCode -ne 200){
  50. Return $NULL
  51. }
  52.  
  53. Return $request.Content | ConvertFrom-Json
  54. }
  55.  
  56. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_ExecuteGet" -Value {
  57. param([Parameter(Mandatory=$True)]
  58. [string]$url,
  59. [Parameter(Mandatory=$False)]
  60. [bool]$appendToken = $True)
  61.  
  62. If($appendToken){
  63. $header = @{Authorization = "Bearer " + $this._Token}
  64. }
  65. Else{ $header = @{} }
  66.  
  67. $request = Invoke-WebRequest -Uri $url -Headers $header -UseBasicParsing
  68.  
  69. Return $this._HandleResponse($request)
  70. }
  71.  
  72. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_ExecutePost" -Value {
  73. param([Parameter(Mandatory=$True)]
  74. [string]$url,
  75. [Parameter(Mandatory=$True)]
  76. $params,
  77. [Parameter(Mandatory=$False)]
  78. [bool]$appendToken = $True)
  79.  
  80. If($appendToken){
  81. $header = @{Authorization = "Bearer " + $this._Token}
  82. }
  83. Else{ $header = @{} }
  84.  
  85. $request = Invoke-WebRequest -Uri $url -Method POST -Body ($params | ConvertTo-Json) -Headers $header -ContentType "application/json" -UseBasicParsing
  86.  
  87. Return $this._HandleResponse($request)
  88. }
  89.  
  90. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_GetUrl" -Value {
  91. param([Parameter(Mandatory=$True)]
  92. [string]$url)
  93.  
  94. $this._InitSession()
  95.  
  96. Return $this._ExecuteGet($url)
  97. }
  98.  
  99. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_Login" -Value {
  100. $params = @{username = $this.Username; password = $this.Password; domain = ""}
  101.  
  102. $url = $this._BaseUrl + 'identity/authenticate'
  103.  
  104. $response = $this._ExecutePost($url, $params, $False)
  105.  
  106. if(!$response){
  107. Return $False
  108. }
  109.  
  110. $this._Token = $response.token
  111.  
  112. $response
  113. }
  114.  
  115. $SEPMClass | Add-Member -MemberType ScriptMethod -Name "GetLicenses" -Value {
  116. $response = $this._GetUrl($this._BaseUrl + 'licenses')
  117. Return $response
  118. }
  119.  
  120. function SEPMClass{
  121. param([Parameter(Mandatory=$True)]
  122. [String]$hostname,
  123. [Parameter(Mandatory=$True)]
  124. [int]$port,
  125. [Parameter(Mandatory=$True)]
  126. [String]$username,
  127. [Parameter(Mandatory=$True)]
  128. [String]$password)
  129.  
  130. $sepmObj = $SEPMClass.psobject.copy()
  131.  
  132. $sepmObj.Username = $username
  133. $sepmObj.Password = $password
  134.  
  135. If(!($hostname.StartsWith('http://') -Or $hostname.StartsWith('https://'))){
  136. $_host = 'https://' + $hostname
  137. }
  138. Else { $_host = $hostname }
  139.  
  140. $sepmObj._BaseUrl = [string]::Format("{0}:{1}/sepm/api/v1/", $_host, $port)
  141.  
  142. $sepmObj
  143. }
  144.  
  145. $sepm = SEPMClass -hostname 'SERVER' -port 8446 -username 'admin' -password 'p%ssword'
  146.  
  147. # Just output license info
  148. $sepm.GetLicenses()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement