Update vCenter-SSL.ps1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env pwsh
|
||||
# -----------------------------------------------------------------------------------
|
||||
# vCenter + Posh-ACME Script (Linux/macOS-safe, fixed HTTP content read)
|
||||
# vCenter + Posh-ACME Script (Linux/macOS-safe, HttpClient login)
|
||||
# -----------------------------------------------------------------------------------
|
||||
|
||||
. /opt/idssys/nodemgmt/conf/powerwall/settings.ps1
|
||||
@@ -105,25 +105,37 @@ if (-not (Get-Module -ListAvailable -Name Posh-ACME)) {
|
||||
Import-Module Posh-ACME -ErrorAction Stop
|
||||
|
||||
# ----------------------------
|
||||
# Connect to vCenter API using Invoke-WebRequest
|
||||
# Connect to vCenter API using HttpClient (robust)
|
||||
# ----------------------------
|
||||
$loginUri = "https://$vCenterURL/rest/com/vmware/cis/session"
|
||||
Write-Host "Connecting to vCenter at $vCenterURL ..." -ForegroundColor Cyan
|
||||
|
||||
try {
|
||||
$resp = Invoke-WebRequest -Uri $loginUri -Method Post -SkipCertificateCheck -UseBasicParsing -Headers @{}
|
||||
$json = $resp.Content | ConvertFrom-Json
|
||||
$sessionToken = $json.value
|
||||
if (-not $sessionToken) {
|
||||
throw [System.Exception] "Unable to obtain vCenter session token"
|
||||
$handler = [System.Net.Http.HttpClientHandler]::new()
|
||||
$handler.ServerCertificateCustomValidationCallback = { $true }
|
||||
|
||||
$client = [System.Net.Http.HttpClient]::new($handler)
|
||||
$client.Timeout = [System.TimeSpan]::FromSeconds(60)
|
||||
|
||||
$loginUri = "https://$vCenterURL/rest/com/vmware/cis/session"
|
||||
$response = $client.PostAsync($loginUri, $null).GetAwaiter().GetResult()
|
||||
|
||||
if (-not $response.IsSuccessStatusCode) {
|
||||
throw "vCenter session request failed: $($response.StatusCode)"
|
||||
}
|
||||
|
||||
$content = $response.Content.ReadAsStringAsync().GetAwaiter().GetResult()
|
||||
$json = $content | ConvertFrom-Json
|
||||
$sessionToken = $json.value
|
||||
|
||||
if (-not $sessionToken) { throw "Failed to obtain vCenter session token" }
|
||||
|
||||
$headers = @{ 'vmware-api-session-id' = $sessionToken }
|
||||
Write-Host "Connected to vCenter API. Session established." -ForegroundColor Green
|
||||
|
||||
} catch {
|
||||
Show-Failure -ErrorRecord $_
|
||||
}
|
||||
|
||||
$headers = @{ 'vmware-api-session-id' = $sessionToken }
|
||||
Write-Host "Connected to vCenter API. Session established." -ForegroundColor Green
|
||||
|
||||
# ----------------------------
|
||||
# Retrieve VM list (optional)
|
||||
# ----------------------------
|
||||
|
||||
Reference in New Issue
Block a user