Hi all, I have been breaking my head over the following:
I have a ProxMox server with a JSON interface and want to use Powershell to run some commands on it.
With Curl on Linux it is easy:
- curl -k -d “username=username@pam&password=password” https://10.0.0.1:8006/api2/json/access/ticket
- This returns some JSON formatted stuff with a ‘ticket’ and a ‘CSRFPreventionToken’.
- In subsequent requests, pass the ticket or the CSRFPreventionToken as a cookie:
curl -k -b “PVEAuthCookie=PVE:username@pam:4EEC61E2:…” https://10.0.0.1:8006/api2/json/
With Invoke-Restmethod, I was able to get the ticket. However, since the server does not actually set it as a cookie, the -Sessionvariable parameter leaves me with an empty cookiejar.
$ticket = Invoke-RestMethod -Method Post -Uri $uri -Body $body -SessionVariable proxmox
Gives me 2 variables:
$ticket.data -> Holds the tokens (ticket and CSRFPreventionToken)
$proxmox.Cookies -> System.Net.CookieContainer with 0 cookies in it.
I cannot find any useful information on the System.Net.CookieContainer, nor on the properties ‘Add’ or ‘SetCookies’…
How can I add a the two tokens from $ticket.data to my $proxmox.Cookies jar?