Update vCenter-SSL.ps1
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#!/usr/bin/env pwsh
|
||||
# -----------------------------------------------------------------------------------
|
||||
# vCenter + Posh-ACME Script using PowerCLI (Final Fully Automated Version)
|
||||
# vCenter + Posh-ACME Script (Fully Automated with Verbose Logging)
|
||||
# -----------------------------------------------------------------------------------
|
||||
|
||||
. /opt/idssys/nodemgmt/conf/powerwall/settings.ps1
|
||||
@@ -90,6 +90,7 @@ $pArgs = @{
|
||||
|
||||
$certName = "vcenter-cert"
|
||||
$certSuccess = $false
|
||||
|
||||
try {
|
||||
Write-Host "Requesting certificate via Posh-ACME..." -ForegroundColor Cyan
|
||||
New-PACertificate -Domain $VCENTERHOST -DnsPlugin PowerDNS -PluginArgs $pArgs -Contact $ACMEEMAIL -AcceptTOS -Verbose -Force -DnsSleep 15
|
||||
@@ -103,13 +104,13 @@ try {
|
||||
# Collect certificate paths dynamically
|
||||
# ----------------------------
|
||||
if ($certSuccess) {
|
||||
# Get the most recent certificate order
|
||||
# Get the most recent order
|
||||
$paOrder = Get-PAOrder | Sort-Object Created -Descending | Select-Object -First 1
|
||||
$certFolder = $paOrder.CertFolder
|
||||
|
||||
$certPath = Join-Path $certFolder "$certName\cert.pem"
|
||||
$keyPath = Join-Path $certFolder "$certName\privkey.pem"
|
||||
$chainPath = Join-Path $certFolder "$certName\chain.pem"
|
||||
$certPath = Join-Path $certFolder "cert.pem"
|
||||
$keyPath = Join-Path $certFolder "privkey.pem"
|
||||
$chainPath = Join-Path $certFolder "chain.pem"
|
||||
|
||||
foreach ($f in @($certPath, $keyPath, $chainPath)) {
|
||||
if (-not (Test-Path $f)) {
|
||||
@@ -120,7 +121,7 @@ if ($certSuccess) {
|
||||
}
|
||||
|
||||
# ----------------------------
|
||||
# Upload and apply certificate via REST
|
||||
# Upload and apply certificate via REST with verbose logging
|
||||
# ----------------------------
|
||||
if ($certSuccess) {
|
||||
try {
|
||||
@@ -135,14 +136,16 @@ if ($certSuccess) {
|
||||
}
|
||||
|
||||
$uriUpload = "https://$VCENTERHOST/rest/vcenter/certificate-management/vcenter/tls"
|
||||
Write-Host "Uploading TLS certificate to vCenter..." -ForegroundColor Cyan
|
||||
Invoke-RestMethod -Uri $uriUpload -Method Post -Body ($body | ConvertTo-Json -Compress) -ContentType 'application/json' -Headers $sessionHeaders -SkipCertificateCheck
|
||||
Write-Host "Certificate uploaded successfully." -ForegroundColor Green
|
||||
|
||||
$uriApply = "https://$VCENTERHOST/rest/vcenter/certificate-management/vcenter/tls?action=apply"
|
||||
Write-Host "Applying TLS certificate to vCenter..." -ForegroundColor Cyan
|
||||
Invoke-RestMethod -Uri $uriApply -Method Post -Headers $sessionHeaders -SkipCertificateCheck
|
||||
|
||||
Write-Host "TLS certificate uploaded and applied successfully." -ForegroundColor Green
|
||||
Write-Host "Certificate applied successfully." -ForegroundColor Green
|
||||
} catch {
|
||||
Write-Host "Certificate upload/apply failed: $($_.Exception.Message)" -ForegroundColor Yellow
|
||||
Write-Host "Certificate upload/apply failed: $($_.Exception.Message)" -ForegroundColor Red
|
||||
$global:helpme = $_.Exception.Message
|
||||
}
|
||||
} else {
|
||||
@@ -150,7 +153,7 @@ if ($certSuccess) {
|
||||
}
|
||||
|
||||
# ----------------------------
|
||||
# Automatic vpxd restart via REST (polling)
|
||||
# Automatic vpxd restart via REST with verbose logging
|
||||
# ----------------------------
|
||||
$maxRetries = 20
|
||||
$retryCount = 0
|
||||
@@ -160,22 +163,25 @@ while ($retryCount -lt $maxRetries -and -not $restartSucceeded) {
|
||||
try {
|
||||
# Test if REST endpoint is available
|
||||
$healthUri = "https://$VCENTERHOST/rest/appliance/health/system"
|
||||
Write-Host "Checking vCenter REST health endpoint..." -ForegroundColor Cyan
|
||||
$resp = Invoke-RestMethod -Uri $healthUri -Method Get -SkipCertificateCheck -ErrorAction Stop
|
||||
|
||||
# Restart vpxd service
|
||||
$restartUri = "https://$VCENTERHOST/rest/appliance/system/services/vpxd?action=restart"
|
||||
Write-Host "Requesting vpxd service restart via REST..." -ForegroundColor Cyan
|
||||
Invoke-RestMethod -Uri $restartUri -Method Post -SkipCertificateCheck -ErrorAction Stop
|
||||
Write-Host "vpxd service restart requested via REST." -ForegroundColor Green
|
||||
Write-Host "vpxd service restart requested successfully." -ForegroundColor Green
|
||||
$restartSucceeded = $true
|
||||
} catch {
|
||||
Write-Host "vpxd REST endpoint not ready yet, retrying in 15 seconds..." -ForegroundColor Yellow
|
||||
Write-Host "vpxd REST endpoint not ready yet, retrying in 15 seconds... (Attempt $($retryCount+1)/$maxRetries)" -ForegroundColor Yellow
|
||||
Start-Sleep -Seconds 15
|
||||
$retryCount++
|
||||
}
|
||||
}
|
||||
|
||||
if (-not $restartSucceeded) {
|
||||
Write-Host "Automatic vpxd restart failed. Please restart manually via SSH:" -ForegroundColor Red
|
||||
Write-Host "Automatic vpxd restart failed after $maxRetries attempts." -ForegroundColor Red
|
||||
Write-Host "Please restart manually via SSH:" -ForegroundColor Red
|
||||
Write-Host "ssh root@$VCENTERHOST 'service-control --stop vpxd; service-control --start vpxd'" -ForegroundColor Red
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user