Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, November 30, 2016 8:11 PM
Good day to everyone,
So I am trying to access the Proxmox API via Powershell in order to use some existing code which generates specific reports for some clients. I am having an issue when trying to debug or when the script has to run through several successive nodes:
When I generate the ticket, the authentication works fine and the connection works fine. On subsequent requests, if trying to do this line-by-line at a Powershell prompt, it will say that the underlying connection has been closed. In the headers I can see that it has the following for Connection: "Close,KeepAlive." That to me seems to indicate an issue.
When I am running the script, as long as the requests were happening in very fast succession, I would get no closed connections. If it was running for more than a few ms, the connection appeared to close, so this was making debugging very hard. I then added a manual TimeoutSec to 10000 because I was reading that there may be a bug in RestMethod. That seemed to alleviate the issue in the script, but it did not while I am trying to do subsequent requests at the command prompt.
I have spent about a week and a half trying to find out why this is not working how I think it should, and this leaves me with posting this in hopes that someone else smarter than myself may know what is going on.
I also am wondering why if there is an error when using Invoke-RestMethod (be it a misspelling or something) the connection then closes also. I would think that any time you invoke the restmethod that it would open a new connection and use the ticket for authentication.
Any help is appreciated and the following is a snippet of my code and is the entirety of the things I place into variables. (variable declarations omitted for brevity)
=====================
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$servername = Read-Host -Prompt 'What is the server IP?'
$uri = omitted
$ticketuri = $uri+'access/ticket'
$C = Get-Credential -Message 'Enter the server login'
#==========Authenticate with the Server===========
$ticket = Invoke-RestMethod -Method Post -uri $ticketuri -body ('username='+$C.UserName+'@pam&password='+$C.GetNetworkCredential().Password) -TimeoutSec 10000 -Verbose
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$cookie = New-Object System.Net.Cookie
$cookie.Name = "PVEAuthCookie"
$cookie.Value = $ticket.data.ticket
$cookie.Domain = $servername
$session.Cookies.Add($cookie);
#=================================================
$nodes = Invoke-RestMethod -uri ($uri+'nodes/') -WebSession $session -TimeoutSec 10000 -Verbose
#This foreach will probably need to go beyond beyond everything to work for multiple nodes
foreach ($node in $nodes.data) {
$qemus = Invoke-RestMethod -uri ($uri+'nodes/'+$node.node+'/qemu') -WebSession $session -TimeoutSec 10000 -Verbose
$lxcs = Invoke-RestMethod -uri ($uri+'nodes/'+$node.node+'/lxc') -WebSession $session -TimeoutSec 10000 -Verbose
$storages = Invoke-RestMethod -uri ($uri+'nodes/'+$node.node+'/storage') -WebSession $session -TimeoutSec 10000 -Verbose
foreach ($storage in $storages.data) {
$tempcontent = Invoke-RestMethod -uri ($uri+'nodes/'+$node.node+'/storage/'+$storage.storage+'/content') -WebSession $session -TimeoutSec 10000 -Verbose
$content = $content + $tempcontent.data
}
}
All replies (2)
Wednesday, November 30, 2016 8:33 PM
The use of private and custom APIs should be addressed to the API provider.
As an alternate you should look at and understand the JavaScript or C# examples from the API provider.
Web sessions usually are maintained via the $session object. Validation errors will likely close the session.
Post here: https://forum.proxmox.com/#proxmox-virtual-environment.11
\(ツ)_/
Wednesday, November 30, 2016 9:45 PM
I have already posted on there as well. Since this appears to be as much a possible issue with Powershell as well as Proxmox, I am asking both forums since I am not sure in which the error may lie.