Update vCenter-SSL.ps1

This commit is contained in:
2025-11-19 23:04:47 -06:00
parent 7d89c10954
commit b409a95133

View File

@@ -49,10 +49,10 @@ function Show-Failure {
param([System.Management.Automation.ErrorRecord]$ErrorRecord) param([System.Management.Automation.ErrorRecord]$ErrorRecord)
$msg = $ErrorRecord.Exception.Message $msg = $ErrorRecord.Exception.Message
Write-Log ERROR $msg -ForegroundColor Red Write-Log ERROR $msg Red
Write-Host "======================================================" -ForegroundColor Red Write-Host "======================================================" Red
Write-Host "ERROR: $msg" -ForegroundColor Red Write-Host "ERROR: $msg" Red
Write-Host "======================================================" -ForegroundColor Red Write-Host "======================================================" Red
exit 1 exit 1
} }
@@ -82,7 +82,7 @@ try {
if (-not (Get-Module -ListAvailable -Name Posh-ACME)) { if (-not (Get-Module -ListAvailable -Name Posh-ACME)) {
Install-Module Posh-ACME -Force -Scope AllUsers Install-Module Posh-ACME -Force -Scope AllUsers
} }
Import-Module Posh-ACME -ErrorAction Stop *>$null -ForegroundColor Red Import-Module Posh-ACME -ErrorAction Stop *>$null
Write-Log INFO "Posh-ACME loaded." Write-Log INFO "Posh-ACME loaded."
} catch { Show-Failure $_ } } catch { Show-Failure $_ }
@@ -105,7 +105,7 @@ try {
$vms = Get-VM $vms = Get-VM
Write-Log INFO "Retrieved $($vms.Count) VMs from vCenter." Write-Log INFO "Retrieved $($vms.Count) VMs from vCenter."
} catch { } catch {
Write-Log WARN "Failed to enumerate VMs: $($_.Exception.Message)" -ForegroundColor Orange Write-Log WARN "Failed to enumerate VMs: $($_.Exception.Message)" Orange
} }
# ---------------------------- # ----------------------------
@@ -124,7 +124,7 @@ try {
Select-Object -First 1 Select-Object -First 1
} }
} catch { } catch {
Write-Log WARN "Failed to query Posh-ACME certificates: $($_.Exception.Message)" -ForegroundColor Orange Write-Log WARN "Failed to query Posh-ACME certificates: $($_.Exception.Message)" Orange
} }
if ($paCert) { if ($paCert) {
@@ -139,7 +139,7 @@ if ($paCert) {
$needNewCert = $false $needNewCert = $false
} }
} else { } else {
Write-Log WARN "No existing cert found — issuance required." -ForegroundColor Orange Write-Log WARN "No existing cert found — issuance required." Orange
$needNewCert = $true $needNewCert = $true
} }
@@ -172,17 +172,17 @@ if ($needNewCert) {
$paCert = Get-PACertificate $paCert = Get-PACertificate
Write-Log INFO ("New certificate issued: NotAfter={0}" -f $paCert.NotAfter) Write-Log INFO ("New certificate issued: NotAfter={0}" -f $paCert.NotAfter)
} catch { } catch {
Write-Log ERROR ("ACME issuance failed: {0}" -f $_.Exception.Message) -ForegroundColor Red Write-Log ERROR ("ACME issuance failed: {0}" -f $_.Exception.Message) Red
if (-not $paCert) { if (-not $paCert) {
Write-Log ERROR "No fallback certificate exists — aborting." -ForegroundColor Red Write-Log ERROR "No fallback certificate exists — aborting." Red
exit 1 exit 1
} }
Write-Log WARN "Using existing Posh-ACME certificate." -ForegroundColor Orange Write-Log WARN "Using existing Posh-ACME certificate." Orange
} }
} }
if (-not $paCert) { if (-not $paCert) {
Write-Log ERROR "No usable certificate available — aborting." -ForegroundColor Red Write-Log ERROR "No usable certificate available — aborting." Red
exit 1 exit 1
} }
@@ -203,7 +203,7 @@ if ($needNewCert) {
foreach ($f in @($certPath,$keyPath,$chainPath)) { foreach ($f in @($certPath,$keyPath,$chainPath)) {
if (-not (Test-Path $f)) { if (-not (Test-Path $f)) {
Write-Log ERROR "Missing cert file: $f" -ForegroundColor Red Write-Log ERROR "Missing cert file: $f" Red
exit 1 exit 1
} }
} }
@@ -240,7 +240,7 @@ if ($needNewCert) {
$needPush = $false $needPush = $false
} }
} catch { } catch {
Write-Log WARN "Unable to read vCenter cert, assuming update required." -ForegroundColor Orange Write-Log WARN "Unable to read vCenter cert, assuming update required." Orange
} }
# ---------------------------- # ----------------------------
@@ -257,7 +257,7 @@ if ($needNewCert) {
Set-VIMachineCertificate -PemCertificate $leafPem -PemKey $keyPem -Confirm:$false | Out-Null Set-VIMachineCertificate -PemCertificate $leafPem -PemKey $keyPem -Confirm:$false | Out-Null
Write-Host "===========================================================" Write-Host "==========================================================="
Write-Host "SUCCESS: vCenter Machine SSL certificate updated." -ForegroundColor Green Write-Host "SUCCESS: vCenter Machine SSL certificate updated." ForegroundColor Green
Write-Host "===========================================================" Write-Host "==========================================================="
Write-Log INFO "Certificate updated successfully." Write-Log INFO "Certificate updated successfully."
@@ -271,7 +271,7 @@ if ($needNewCert) {
$null = $svc | Restart-VIApplianceService -Confirm:$false $null = $svc | Restart-VIApplianceService -Confirm:$false
Write-Log INFO "vpxd restarted successfully." Write-Log INFO "vpxd restarted successfully."
} catch { } catch {
Write-Log WARN "vpxd restart failed: $($_.Exception.Message)" -ForegroundColor Orange Write-Log WARN "vpxd restart failed: $($_.Exception.Message)" Orange
} }
# ---------------------------- # ----------------------------
@@ -285,7 +285,7 @@ if ($needNewCert) {
$result = bash -c $sshCmd $result = bash -c $sshCmd
Write-Log INFO "Veeam rescan result: $result" Write-Log INFO "Veeam rescan result: $result"
} catch { } catch {
Write-Log WARN "Veeam rescan failed: $($_.Exception.Message)" -ForegroundColor Orange Write-Log WARN "Veeam rescan failed: $($_.Exception.Message)" Orange
} }
} }