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 = 'https://'+$servername+':8006/api2/json/'
$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
}
}
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 = 'https://'+$servername+':8006/api2/json/'
$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
}
}