Skip to content

Instantly share code, notes, and snippets.

@itsthedoc
Created August 31, 2019 18:42
Show Gist options
  • Save itsthedoc/51b7e3256a183bab714048e4813353eb to your computer and use it in GitHub Desktop.
Save itsthedoc/51b7e3256a183bab714048e4813353eb to your computer and use it in GitHub Desktop.
Example on logging into SDWAN and uploading a license file
### Start security settings ###
### If server's SSL cert is invalid/not trusted, we need to ignore the cert for Invoke-WebRequest
function Ignore-SSLCertificates
{
$Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
$Compiler = $Provider.CreateCompiler()
$Params = New-Object System.CodeDom.Compiler.CompilerParameters
$Params.GenerateExecutable = $false
$Params.GenerateInMemory = $true
$Params.IncludeDebugInformation = $false
$Params.ReferencedAssemblies.Add("System.DLL") > $null
$TASource=@'
namespace Local.ToolkitExtensions.Net.CertificatePolicy
{
public class TrustAll : System.Net.ICertificatePolicy
{
public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
{
return true;
}
}
}
'@
$TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
$TAAssembly=$TAResults.CompiledAssembly
## We create an instance of TrustAll and attach it to the ServicePointManager
$TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
[System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}
### Run Ignore SSL function for the session
Ignore-SSLCertificates
### set TLS to 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#### End security
### login and get cookie
$data = @{
login=[ordered]@{
username="admin"
password="password"
}
} | ConvertTo-Json
$uri = "https://192.168.40.62/sdwan/nitro/v1/config/login"
$cookies = Invoke-WebRequest $uri -Method post -Body $data -ContentType "application/json"
$cookie = $cookies.Headers.'Set-Cookie'
$cookieName = ($cookie -split "=")[0]
$cookieValue = (($cookie -split "=")[1] -split ";")[0]
## create session and add cookie
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie.Name = $cookieName
$cookie.Value = $cookieValue
$cookie.Domain = "192.168.40.62"
$cookie.Path = "/"
$cookie.Secure = $true
$cookie.HttpOnly = $true
$session.Cookies.Add($cookie);
## set upload URL and get file content
$uploadURI = "http://192.168.40.62/sdwan/nitro/v1/config/file_upload_to_inbox?action=upload_license"
$license = [IO.File]::ReadAllText('D:\downloads\FID_625155c1-3c5b-4b7e-bc22-096da168a57e.lic')
$upload = Invoke-WebRequest -Uri $uploadURI -Method Post -WebSession $session -Body $license -ContentType 'multipart/form-data'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment