Adding a cookie to a System.Net.CookieContainer object

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:

  1. curl -k -d “username=username@pam&password=password”
  2. This returns some JSON formatted stuff with a ‘ticket’ and a ‘CSRFPreventionToken’.
  3. In subsequent requests, pass the ticket or the CSRFPreventionToken as a cookie:
    curl -k -b “PVEAuthCookie=PVE:username@pam:4EEC61E2:…”

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.

$body =@{
$ticket = Invoke-RestMethod -Method Post -Uri $uri -Body $body -SessionVariable proxmox

Gives me 2 variables:
$ -> 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 $ to my $proxmox.Cookies jar?

So… I mean, this is really kind of a .NET question, so it’s possible a site like StackOverflow will get you a more immediate answer. And, I know zip about ProxMox.

According to, though, you have to construct a Cookie object, and then call the Add() method of the CookieContainer to add it.

Thanks for the response, Don. I figured that I would look at some .Net stuff, but since I am not a developer, it is not really helpful Some if the MSDN documentation I found (and tried) included constructing cookies like:

$proxmox.Cookies.Add(System.Net.Cookie “name”)
$proxmox.Cookies.SetCookies(uri $uri)

etc… But all to no avail. If I would just understand the construct of Add() or SetCookies(), I might get somewhere…

Quick update on my own thread. I found how to put the cookie in place:

function Create-Cookie($name, $value, $domain, $path="/"){
$c=New-Object System.Net.Cookie;
$c.Value = $value
$c.Domain =$domain;
return $c;

$body =@{
$ticketuri = $uri+’/access/ticket’
$ticket = Invoke-RestMethod -Method Post -Uri $ticketuri -Body $body -SessionVariable proxmox

#Create the cookies for the session based on the data returned

$proxmox.Cookies.Add((Create-Cookie -name “PVEAuthCookie” -value “$PVEAuthCookie” -domain “proxmox”));

Invoke-RestMethod $uri -SessionVariable $proxmox

(I know my code is quite verbose and is not as clean as it could be. Makes it easier to troubleshoot)

The cookie is passed and the Proxmox log shows:
Jun 8 18:10:43 PROXMOX pvedaemon[730732]: successful auth for user ‘user@pve’

Oddly enough, Powershell bleeds with:
Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

If I figure it out, I will post here… In the meantime, any ideas are welcome.

OK - I found the issue. Amazing that nobody noticed this one:

Invoke-RestMethod $uri -SessionVariable $proxmox

Should be:
Invoke-RestMethod $uri -WebSession $proxmox

Works fine now… :smiley:

I hope this is useful to somebody at some point…