Files

96 lines
2.8 KiB
PowerShell

Set-StrictMode -Version Latest
function Get-ProxmoxConfig {
$tokenHeader = $null
if ($env:PROXMOX_API_TOKEN_ID -and $env:PROXMOX_API_TOKEN_SECRET) {
$tokenHeader = "PVEAPIToken=$($env:PROXMOX_API_TOKEN_ID)=$($env:PROXMOX_API_TOKEN_SECRET)"
}
[pscustomobject]@{
HostName = if ($env:PROXMOX_HOST) { $env:PROXMOX_HOST } else { "192.168.0.200" }
Node = if ($env:PROXMOX_NODE) { $env:PROXMOX_NODE } else { "thinkcentre" }
User = if ($env:PROXMOX_USER) { $env:PROXMOX_USER } else { "root" }
SshKey = if ($env:PROXMOX_SSH_KEY) { $env:PROXMOX_SSH_KEY } else { "C:\Users\Uriel Jareth\.openclaw\workspace\proxmox_key_win" }
ApiBaseUrl = if ($env:PROXMOX_API_BASE_URL) { $env:PROXMOX_API_BASE_URL } else { "https://192.168.0.200:8006/api2/json" }
ApiTokenHeader = $tokenHeader
CoolifyLxc = if ($env:PROXMOX_COOLIFY_LXC) { $env:PROXMOX_COOLIFY_LXC } else { "102" }
}
}
function Assert-ProxmoxConfig {
$config = Get-ProxmoxConfig
if (-not (Get-Command ssh -ErrorAction SilentlyContinue)) {
throw "ssh executable not found in PATH."
}
if (-not (Test-Path -LiteralPath $config.SshKey)) {
throw "SSH key not found: $($config.SshKey)"
}
return $config
}
function Invoke-ProxmoxSshCommand {
param(
[Parameter(Mandatory = $true)]
[string]$Command,
[int]$ConnectTimeoutSec = 15
)
$config = Assert-ProxmoxConfig
$target = "$($config.User)@$($config.HostName)"
$sshArgs = @(
"-o", "BatchMode=yes",
"-o", "ConnectTimeout=$ConnectTimeoutSec",
"-o", "StrictHostKeyChecking=no",
"-i", $config.SshKey,
$target,
$Command
)
& ssh @sshArgs
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
throw "SSH command failed with exit code $exitCode."
}
}
function Invoke-ProxmoxApi {
param(
[Parameter(Mandatory = $true)]
[string]$Path
)
$config = Get-ProxmoxConfig
if (-not $config.ApiTokenHeader) {
throw "Set PROXMOX_API_TOKEN_ID and PROXMOX_API_TOKEN_SECRET before API calls."
}
if (-not (Get-Command curl.exe -ErrorAction SilentlyContinue)) {
throw "curl.exe not found in PATH."
}
$base = $config.ApiBaseUrl.TrimEnd("/")
$cleanPath = $Path.TrimStart("/")
$uri = "$base/$cleanPath"
$output = & curl.exe -sS -k -H "Authorization: $($config.ApiTokenHeader)" $uri
$exitCode = $LASTEXITCODE
if ($exitCode -ne 0) {
throw "Proxmox API request failed with exit code $exitCode."
}
if (-not $output) {
throw "Proxmox API returned an empty response."
}
try {
return ($output | ConvertFrom-Json)
}
catch {
throw "Proxmox API returned non-JSON output."
}
}