I am attempting to use invoke-RestMethod to connect to a Citrix Netscaler’s nitro rest api. I then re-use the returned SessionVariable to run several commands through the API. This works fine with http, but when I use https I get back an empty object or a session expired error.
$credential = get-credentials
$login =@{"login"@{"username"="nsroot";"password"="password";"timeout"="360"}}
$loginJson = ConvertTo-Json $login
try {
Write-Verbose "Calling Invoke-RestMethod for login"
$response = Invoke-RestMethod -Uri "https://netscaler.mydomain.com/nitro/v1/config/login" -Body $loginJson -Method POST -SessionVariable saveSession -ContentType application/json -ErrorAction Stop -TimeoutSec 60
if ($response.severity -eq "ERROR") {
throw "Error. See response: `n$($response | fl * | Out-String)"
} else {
Write-Verbose "Response:`n$(ConvertTo-Json $response | Out-String)"
$saveSession.Credentials = $credential
$myNSSession1 = New-Object -TypeName PSObject
$myNSSession1 | Add-Member -NotePropertyName Endpoint -NotePropertyValue "netscaler.mydomain.com" -TypeName String
$myNSSession1 | Add-Member -NotePropertyName WebSession -NotePropertyValue $saveSession -TypeName Microsoft.PowerShell.Commands.WebRequestSession
}
}
catch [Exception] {
throw $_
}
$uri = "https://netscaler.mydomain.com/nitro/v1/config/nsconfig"
$call = Invoke-RestMethod -uri $uri -Method GET -WebSession $myNSSession1.WebSession
This returns:
Invoke-RestMethod : { “errorcode”: 444, “message”: “Session expired or killed. Please login again”, “severity”: “ERROR” }