Invoke-RestMethod fails against vCloud Director

I’m trying to work out how to get a call against the vCloud Director API using Invoke-RestMethod to work but it fails and I’m not entirely sure why:

$uri = "https://VCD/api/sessions" $params = @{ accept = "application/*+xml;version=27.0"; } $u = "USER@system" $p = "PASSWORD" | ConvertTo-SecureString -AsPlainText -Force $c = New-Object System.Management.Automation.PSCredential($u,$p) Invoke-RestMethod -Uri $uri -Method Post -Body $params -Credential $c 

The above fails with “Invoke-RestMethod : The remote server returned an error: (406) Not Acceptable.”

If I take the long route and construct a web request it works fine. example:

$uri = "https://VCD/api/sessions" $u = "USER@system" $p = "PASSWORD" | ConvertTo-SecureString -AsPlainText -Force [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true} $webRequest = [System.Net.WebRequest]::Create($uri) $webRequest.ServicePoint.Expect100Continue = $false $webRequest.PreAuthenticate = $true $webRequest.Method = "Post" $webRequest.Accept = "application/*+xml;version=27.0"; $webRequest.Credentials = New-Object System.Net.NetworkCredential($u,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($p))) $resp = $webRequest.GetRequestStream(); $rs = $webRequest.GetResponse() [System.IO.StreamReader]$sr = New-Object System.IO.StreamReader -argumentList $rs.GetResponseStream(); [string]$results = $sr.ReadToEnd(); 

I’ve been looking at examples all afternoon and it’s just not clicking for me. What am I missing/doing wrong?

submitted by /u/9centwhore
[link] [comments]

Leave a Reply