Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Ignore self-certs
- if (-not ([System.Management.Automation.PSTypeName]'ServerCertificateValidationCallback').Type)
- {
- $certCallback = @"
- using System;
- using System.Net;
- using System.Net.Security;
- using System.Security.Cryptography.X509Certificates;
- public class ServerCertificateValidationCallback
- {
- public static void Ignore()
- {
- if(ServicePointManager.ServerCertificateValidationCallback ==null)
- {
- ServicePointManager.ServerCertificateValidationCallback +=
- delegate
- (
- Object obj,
- X509Certificate certificate,
- X509Chain chain,
- SslPolicyErrors errors
- )
- {
- return true;
- };
- }
- }
- }
- "@
- Add-Type $certCallback
- }
- [ServerCertificateValidationCallback]::Ignore()
- # TLS 1.2 requires PowerShell 3.0+
- [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;
- $SEPMClass = New-Object -TypeName PSObject -Prop @{Username = $null; Password = $null; _BaseUrl = $null; _Token = $null}
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_InitSession" -Value {
- If(-Not $this._SID.Length){
- $this._Login()
- }
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_HandleResponse" -Value {
- param([Parameter(Mandatory=$True)]
- $request)
- If($request.StatusCode -ne 200){
- Return $NULL
- }
- Return $request.Content | ConvertFrom-Json
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_ExecuteGet" -Value {
- param([Parameter(Mandatory=$True)]
- [string]$url,
- [Parameter(Mandatory=$False)]
- [bool]$appendToken = $True)
- If($appendToken){
- $header = @{Authorization = "Bearer " + $this._Token}
- }
- Else{ $header = @{} }
- $request = Invoke-WebRequest -Uri $url -Headers $header -UseBasicParsing
- Return $this._HandleResponse($request)
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_ExecutePost" -Value {
- param([Parameter(Mandatory=$True)]
- [string]$url,
- [Parameter(Mandatory=$True)]
- $params,
- [Parameter(Mandatory=$False)]
- [bool]$appendToken = $True)
- If($appendToken){
- $header = @{Authorization = "Bearer " + $this._Token}
- }
- Else{ $header = @{} }
- $request = Invoke-WebRequest -Uri $url -Method POST -Body ($params | ConvertTo-Json) -Headers $header -ContentType "application/json" -UseBasicParsing
- Return $this._HandleResponse($request)
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_GetUrl" -Value {
- param([Parameter(Mandatory=$True)]
- [string]$url)
- $this._InitSession()
- Return $this._ExecuteGet($url)
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "_Login" -Value {
- $params = @{username = $this.Username; password = $this.Password; domain = ""}
- $url = $this._BaseUrl + 'identity/authenticate'
- $response = $this._ExecutePost($url, $params, $False)
- if(!$response){
- Return $False
- }
- $this._Token = $response.token
- $response
- }
- $SEPMClass | Add-Member -MemberType ScriptMethod -Name "GetLicenses" -Value {
- $response = $this._GetUrl($this._BaseUrl + 'licenses')
- Return $response
- }
- function SEPMClass{
- param([Parameter(Mandatory=$True)]
- [String]$hostname,
- [Parameter(Mandatory=$True)]
- [int]$port,
- [Parameter(Mandatory=$True)]
- [String]$username,
- [Parameter(Mandatory=$True)]
- [String]$password)
- $sepmObj = $SEPMClass.psobject.copy()
- $sepmObj.Username = $username
- $sepmObj.Password = $password
- If(!($hostname.StartsWith('http://') -Or $hostname.StartsWith('https://'))){
- $_host = 'https://' + $hostname
- }
- Else { $_host = $hostname }
- $sepmObj._BaseUrl = [string]::Format("{0}:{1}/sepm/api/v1/", $_host, $port)
- $sepmObj
- }
- $sepm = SEPMClass -hostname 'SERVER' -port 8446 -username 'admin' -password 'p%ssword'
- # Just output license info
- $sepm.GetLicenses()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement