Powershell example script using REST web services POST

iKnow-IT banner

Below you can find a powershell script to POST a node on a REST web services server.

$Aname = "< Name >"
$BaseUrl = "< base url >"
$nodesURL = "/services/rest/cis/v1/nodes"
$scheduleURL = "/services/rest/cis/v1/schedules/"
$RequestTimeout = 100000 # webrequest timeout in milliseonds
$UserName = "< user >"
$Password = "< password >"
$password_base64 = ConvertTo-SecureString $Password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($UserName, $password_base64)
$Logfile = "$($Env:LogFiles)\schedule.log"

$body = ""
# Functions
Function LogFile ($Type,$Message) {
$Date = get-date -format "yyyy-MM-dd HH:mm:ss"
Out-File -filepath $Logfile -inputobject "$($Date) [$($Type)] $($Message)" -append
Write-Host "$($Date) [$($Type)] $($Message)"
}

# Trust all certificates to avoid certiticate issues with HTTPS
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@ # Do not add a TAB at this line or the script will fail

[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

Logfile "INFO" "-------------------------- Start --------------------------"

# Create a request
Logfile "INFO" "Start request to get list of nodes: $($BaseUrl)$($nodesURL)"

Try {
$response = Invoke-RestMethod "$($BaseUrl)$($nodesURL)" -Method Get -Credential $creds
}
Catch {
$res = $_.Exception.Response

if ([int]$res.StatusCode -eq 406)
{
Logfile "INFO" "Response: $([int]$res.StatusCode) $($res.StatusCode)"
Logfile "INFO" "-------------------------- End --------------------------"
Exit 10
}
else
{
Logfile "ERROR" "Error message: $([int]$res.StatusCode) $($res.StatusCode)"
LogFile "ERROR" "Exit script"
Logfile "INFO" "-------------------------- End --------------------------"
Exit 10
}
}

$Temp = (get-date)
$StartMaint = Get-Date $Temp.ToUniversalTime() -format "yyyy-MM-dd HH:mm"
$Temp = (Get-date).AddMonths(2)
$EndMaint = Get-Date $Temp.ToUniversalTime() -Format "yyyy-MM-dd HH:mm"

foreach ($rsp in $response) {
if ($rsp.name.ToLower().Contains($Aname.ToLower())) {
Logfile "INFO" "Setting maintenance on: $($rsp.name)"

$body = @"

1
Web Services triggered
UTC
$($StartMaint)

$($rsp.name)
$($EndMaint)

"@

Try {
$maintrsp = Invoke-RestMethod "$($BaseUrl)$($scheduleURL)" -Method Post -Credential $creds -Body $body -ContentType 'application/xml'
}
Catch {
$res = $_.Exception.Response

if ([int]$res.StatusCode -eq 406)
{
Logfile "INFO" "Response: $([int]$res.StatusCode) $($res.StatusCode)"
Logfile "INFO" "-------------------------- End --------------------------"
Exit 10
}
else
{
Logfile "ERROR" "Error message: $([int]$res.StatusCode) $($res.StatusCode)"
LogFile "ERROR" "Exit script"
Logfile "INFO" "-------------------------- End --------------------------"
Exit 10
}

}

Logfile "INFO" "Response from setting maintenanceschedule: $($maintrsp) "

break
}
}

Logfile "INFO" "-------------------------- End --------------------------"