Powershell Gurus: Cannot get the API to respond properly through Powershell

mollien

Active Member
Jun 18, 2015
8
0
41
Hi all,Long time lurker, first time poster...I recently got into provisioning various JSON services over Powershell and have been (overall) successful. The only one that keeps alluding me is ProxMox.

So far, I have my Proxmox test machine assigning me a ticket just fine. The data returned is
:ticket : PVE:jason@pam:5583228E::1lQfDndlTzQIAdg7lj8l5Mg........==
CSRFPreventionToken : 5583228E:N9yJ+Xz/X4QZzVNIPD//F2Bhdw0
username : jason@pam

I have successfully created a session cookie with these values, and have stored them in the session variable $proxmox.Cookies. However, whenever I want to grab the basic JSON API and pass the cookie, I get a 401 Unauthorized.

Interestingly, syslog and daemon.log show a 'successful auth for user jason@pam' every time I try to get the JSON files.

The command I run is:
Code:
Invoke-RestMethod https://proxmox.home.mollien.net:8006/api2/json' -SessionVariable $proxmox -Verbose
I have tried several ways of creating the cookie, even just adding the ticket and CSRFPreventionToken directly into the headers.

See a slightly sanitized version of my script (so far) attached. I am hoping to turn this into a useful Cmdlet over time, that I will gladly share with the community.Any ideas or assistance is very welcomed!

Thanks,Cas
 
Last edited:
Uploaded attachment....
 

Attachments

  • ProxMox.txt
    2.2 KB · Views: 53
Last edited:
Wow... This was a clear case of RTFM:

In the second request, where I am passing the cookie, I should have used -WebSession instead of -SessionVariable...

Now it works perfectly!
 
Hi,

Could you elaborate a bit more on which is the command/request you have to changeI tried changing

Code:
$webrequest = Invoke-WebRequest -Uri $url -SessionVariable websession

to

Code:
$webrequest = Invoke-WebRequest -Uri $url -WebSession websession

but keep getting the same error
upload_2017-6-27_15-11-25.png

Thanks in advance.
 

Attachments

  • upload_2017-6-27_15-10-8.png
    upload_2017-6-27_15-10-8.png
    27.9 KB · Views: 9
For future reference on how to start to talk to to the API with Powershell, at least for some basic get API calls. My current code is on github, search for ProxmoxCLI under the quonic user.
I'm working on getting all, if not most of the API implemented.
 

About

The Proxmox community has been around for many years and offers help and support for Proxmox VE, Proxmox Backup Server, and Proxmox Mail Gateway.
We think our community is one of the best thanks to people like you!

Get your subscription!

The Proxmox team works very hard to make sure you are running the best software and getting stable updates and security enhancements, as well as quick enterprise support. Tens of thousands of happy customers have a Proxmox subscription. Get yours easily in our online shop.

Buy now!