Powershell API not working?

maco1717

Active Member
Apr 11, 2017
12
0
41
39
Hi,

I'm quite new to powershell and proxmox im trying to use the restful API service on proxmox to manage nodes from powershell.

at the moment im struggling to get started as when i try to get a ticket, is not taking it, im using part of the code in this post https://forum.proxmox.com/threads/p...to-respond-properly-through-powershell.22507/

Code:
function Create-Cookie($name, $value, $domain, $path){
    $c=New-Object System.Net.Cookie;
    $c.Name=$name;
    $c.Path=$path;
    $c.Value = $value;
    $c.Domain =$domain;
    $c.Expires = ($c.TimeStamp).AddMinutes("20");
    return $c;
}

# Add your variables here......
$uri="https://IP:8006/api2/json"
$domain=".<yourdomain>"
$ticketuri = $uri+'/access/ticket'
$apipath='/api2/json'
$body =@{
 username='user'@<PAM|PVE>'
 password='pass'
}

# First request: Get a ticket
write-host "Requesting ticket from Proxmox:"
$ticket = Invoke-RestMethod -Method Post -Uri $ticketuri -Body $body -SessionVariable $proxmox -verbose

which got me a SSL/TLS error and need this done :

Code:
add-type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    public class TrustAllCertsPolicy : ICertificatePolicy {
        public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy

afther setting this up to ignore ssl error running my code would return me this error:
VERBOSE: POST https://10.20.30.228:8006/api2/json/access/ticket with -1-byte payload
Invoke-RestMethod : {"data":null}
At line:25 char:11
+ $ticket = Invoke-RestMethod -Method Post -Uri $ticketuri -Body $body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I have noticed also that when accesing the url from a browser I get that same result, dont know if it related or I'm doing something wrong.

Any help would be appreciated.

Thanks.
\Marco
 
Using Invoke-WebRequest, example to ticket:

Code:
$params = @{"username"="test@pve";
"password"="password";
}

Invoke-WebRequest -Uri "https://ip:8006/api2/json/access/ticket" -Method POST -Body $params | ConvertFrom-Json
 
Last edited:

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!