Files
NodeMgmt/inc/vCenter-SSL.ps1

127 lines
3.9 KiB
PowerShell

#!/usr/bin/env pwsh
. /opt/idssys/nodemgmt/conf/powerwall/settings.ps1
function Show-Failure {
param($ErrorRecord)
try {
$response = $ErrorRecord.Exception.Response
if ($response -is [System.Net.Http.HttpResponseMessage]) {
# PowerShell 7 (.NET HttpClient)
$global:responseBody = $response.Content.ReadAsStringAsync().Result
}
elseif ($response -is [System.Net.WebResponse]) {
# Legacy (.NET Framework)
$stream = $response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($stream)
$global:responseBody = $reader.ReadToEnd()
}
else {
$global:responseBody = $ErrorRecord.Exception.Message
}
}
catch {
$global:responseBody = $_.Exception.Message
}
Write-Host -BackgroundColor Black -ForegroundColor Red "Status: A system exception was caught."
Write-Host -BackgroundColor Black -ForegroundColor Red $global:responseBody
Write-Host -BackgroundColor Black -ForegroundColor Red "The request body has been saved to `$global:helpme"
break
}
# ----------------------------
# Variables
# ----------------------------
$vCenterURL = $VCENTERHOST
$CommonName = $VCENTERHOST
$EmailContact = $ACMEEMAIL
[PSCredential]$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList $VCENTERUSER, (ConvertTo-SecureString $VCENTERPASS -AsPlainText -Force)
$pArgs = @{
PowerDNSApiHost = $WDNSHOST
PowerDNSApiKey = $PDNSAPI | ConvertTo-SecureString -AsPlainText -Force
PowerDNSUseTLS = $true
PowerDNSPort = 443
PowerDNSServerName = 'localhost'
}
# ----------------------------
# Ensure Posh-ACME Module
# ----------------------------
Write-Host "Checking for Required Module Posh-ACME" -ForegroundColor Green
if (Get-Module -ListAvailable -Name Posh-ACME) {
Write-Host "Posh-ACME Module Already Installed" -ForegroundColor Green
}
else {
Write-Host "Posh-ACME Module Not Found, Installing..." -ForegroundColor Yellow
Install-Module -Name Posh-ACME -Force -Confirm:$false
Write-Host "Please restart this script after module install." -ForegroundColor Yellow
return
}
Do {
Write-Host "Waiting for Posh-ACME Module to load..." -ForegroundColor Cyan
$PoshACME = Get-Module -ListAvailable -Name Posh-ACME
Start-Sleep -Seconds 5
}
While ($PoshACME -eq $null)
# ----------------------------
# vCenter API Session
# ----------------------------
try {
$loginParams = @{
Uri = "https://$vCenterURL/rest/com/vmware/cis/session"
Method = 'Post'
Credential = $Credential
SslProtocol = 'Tls12'
SkipCertificateCheck = $true
ErrorAction = 'Stop'
}
$session = Invoke-RestMethod @loginParams
$sessionToken = $session.value
if (-not $sessionToken) {
throw "Unable to get Session Token"
}
Write-Host "Connected to vCenter API. Session established." -ForegroundColor Green
}
catch {
Show-Failure -ErrorRecord $_
exit 1
}
# ----------------------------
# Example REST Call (replace with actual logic)
# ----------------------------
try {
$headers = @{ 'vmware-api-session-id' = $sessionToken }
$vmListParams = @{
Uri = "https://$vCenterURL/rest/vcenter/vm"
Method = 'Get'
Headers = $headers
SslProtocol = 'Tls12'
ErrorAction = 'Stop'
}
$vmList = Invoke-RestMethod @vmListParams
Write-Host "Retrieved VM list from vCenter:" -ForegroundColor Cyan
$vmList.value | ForEach-Object { Write-Host " - $($_.name)" }
}
catch {
Show-Failure -ErrorRecord $_
exit 1
}
# ----------------------------
# (Continue with ACME + certificate automation)
# ----------------------------
# At this point, all network calls use Invoke-RestMethod/Invoke-WebRequest with modern TLS.
# Extend with your ACME challenge/PowerDNS automation here.