PowerShell and AS3
I have been doing some AS3 content lately and in a customer environment I only had powershell available. Generally I use python, so this was a nice opportunity to sit down and use Invoke methods in powershell to post AS3 content. Here is the baseline script I developed. This is using Powershell 5.1 and is accounting for self-signed certificates, to allow the system to interact, as by default powershell 5.1 doesn’t have the ignore flag for the two Invoke requests I’m using.
# This is used to bypass ssl validation checks
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$True}
# Login to F5
$creds = Get-Credential
$url = 'https://IP.ADDR/mgmt/shared/authn/login'
$body = @{
username = $creds.UserName
password = $creds.GetNetworkCredential().Password
loginProviderName = "tmos"
} | ConvertTo-Json
$result = Invoke-RestMethod -Method 'POST' -URI $url -ContentType 'application/json' -Body $body
$token = $($result.token.token)
#Verify Token
$url = 'https://IP.ADDR/mgmt/shared/authz/tokens/' + $token
$headers = @{
'X-F5-Auth-Token' = $token
}
# Get Current AS3 Version (validate it's installed)
$res = Invoke-RestMethod -uri 'https://IP.ADDR/mgmt/shared/appsvcs/info' -Method Get -Headers $headers
Write-Host $res
# Switch to Invoke-WebRequest, Invoke-RestMethod doesn't return the status code. F5 status codes are important, they let you know if things are working.
# Get Current Config
$res = Invoke-WebRequest -uri 'https://IP.ADDR/mgmt/shared/appsvcs/declare' -Method Get -Headers $headers
Write-Host $res.StatusCode
# Load Common partition into the F5
$body = Get-Content .\Common.json
$res = Invoke-WebRequest -uri 'https://IP.ADDR/mgmt/shared/appsvcs/declare' -Method POST -Headers $headers -Body $body
Write-Host $res.StatusCode
Write-Host $res.Content