Update
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
#Requires -Version 7.0
|
||||
[CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
|
||||
param(
|
||||
[Parameter(Mandatory)][string]$VCenter,
|
||||
[Parameter(Mandatory)][string[]]$VMHostName,
|
||||
[string]$BaselinePath = (Join-Path $PSScriptRoot 'baseline.json'),
|
||||
[ValidateSet('Audit', 'Apply')][string]$Mode = 'Audit',
|
||||
[string]$ReportDirectory = (Join-Path $PSScriptRoot 'reports')
|
||||
)
|
||||
Set-StrictMode -Version Latest
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$config = Get-Content -LiteralPath $BaselinePath -Raw | ConvertFrom-Json
|
||||
if ($config.TargetVersion -ne '8.0.3') { throw 'Diese Version des Werkzeugs unterstützt ESXi 8.0.3.' }
|
||||
if (@($config.AdvancedSettings).Count + @($config.Services).Count -eq 0) { throw 'Leere Baseline.' }
|
||||
foreach ($entry in $config.AdvancedSettings) {
|
||||
if ([string]::IsNullOrWhiteSpace($entry.Name) -or $null -eq $entry.Value) { throw 'Ungültiger Einstellungswert.' }
|
||||
}
|
||||
foreach ($entry in $config.Services) {
|
||||
if ($entry.Policy -notin @('on', 'off', 'automatic') -or $entry.Running -isnot [bool]) { throw 'Ungültige Dienstkonfiguration.' }
|
||||
}
|
||||
foreach ($name in $VMHostName) {
|
||||
if ([string]::IsNullOrWhiteSpace($name) -or $name -match '[*?\[\]]') { throw 'Nur explizite Hostnamen, keine Suchmuster angeben.' }
|
||||
}
|
||||
Import-Module VMware.VimAutomation.Core -ErrorAction Stop
|
||||
# Existing authenticated session only; no embedded credentials or certificate bypass.
|
||||
$sessions = @(foreach ($variableName in @('DefaultVIServers', 'DefaultVIServer')) {
|
||||
$variable = Get-Variable -Name $variableName -Scope Global -ErrorAction SilentlyContinue
|
||||
if ($null -ne $variable) { $variable.Value }
|
||||
})
|
||||
$servers = @($sessions | Where-Object { $_.Name -eq $VCenter -and $_.IsConnected } | Sort-Object -Property Name -Unique)
|
||||
if ($servers.Count -ne 1) { throw 'Zuerst mit Connect-VIServer genau eine Sitzung zum angegebenen vCenter herstellen.' }
|
||||
$server = $servers[0]
|
||||
if ([version]$server.Version -lt [version]'8.0' -or [version]$server.Version -ge [version]'9.0') { throw 'vCenter 8 erforderlich.' }
|
||||
# Resolve and validate every target before making any change.
|
||||
$targets = @(foreach ($name in ($VMHostName | Select-Object -Unique)) {
|
||||
$found = @(Get-VMHost -Server $server -Name $name | Where-Object { $_.Name -eq $name })
|
||||
if ($found.Count -ne 1) { throw "Host nicht eindeutig gefunden: $name" }
|
||||
if ($found[0].Version -ne $config.TargetVersion) { throw "Nicht unterstützte ESXi-Version auf $name" }
|
||||
if ([string]$found[0].ConnectionState -notin @('Connected', 'Maintenance')) { throw "Host nicht erreichbar: $name" }
|
||||
$found[0]
|
||||
})
|
||||
$runId = '{0}-{1}' -f (Get-Date -Format 'yyyyMMdd-HHmmss'), ([guid]::NewGuid().ToString('N').Substring(0,8))
|
||||
# Reports are intentionally also written in Audit and -WhatIf modes.
|
||||
$null = [IO.Directory]::CreateDirectory($ReportDirectory)
|
||||
$reportPath = Join-Path $ReportDirectory "$runId.json"
|
||||
$rows = [Collections.Generic.List[object]]::new()
|
||||
$report = [ordered]@{
|
||||
StartedUtc = [DateTime]::UtcNow.ToString('o'); VCenter = $VCenter; Mode = $Mode
|
||||
Baseline = $config; BaselineSha256 = (Get-FileHash -LiteralPath $BaselinePath -Algorithm SHA256).Hash
|
||||
Results = $rows
|
||||
}
|
||||
function Save-Report { $report | ConvertTo-Json -Depth 15 | Set-Content -LiteralPath $reportPath -Encoding utf8 -WhatIf:$false }
|
||||
Save-Report
|
||||
foreach ($target in $targets) {
|
||||
$controls = @(
|
||||
foreach ($entry in $config.AdvancedSettings) { [pscustomobject]@{ Kind = 'Advanced'; Entry = $entry } }
|
||||
foreach ($entry in $config.Services) { [pscustomobject]@{ Kind = 'Service'; Entry = $entry } }
|
||||
)
|
||||
foreach ($control in $controls) {
|
||||
$entry = $control.Entry
|
||||
$key = if ($control.Kind -eq 'Advanced') { $entry.Name } else { $entry.Key }
|
||||
$desired = if ($control.Kind -eq 'Advanced') { [string]$entry.Value } else { "$($entry.Policy)/$($entry.Running)" }
|
||||
$row = [pscustomobject]@{
|
||||
Host = $target.Name; Build = $target.Build; Kind = $control.Kind; Setting = $key
|
||||
Control = $entry.Control; Before = $null; Desired = $desired; After = $null
|
||||
Status = 'Pending'; Error = $null
|
||||
}
|
||||
$rows.Add($row)
|
||||
try {
|
||||
if ($control.Kind -eq 'Advanced') {
|
||||
$items = @(Get-AdvancedSetting -Entity $target -Name $key | Where-Object Name -EQ $key)
|
||||
if ($items.Count -ne 1) { throw 'Einstellung fehlt oder ist nicht eindeutig; wird nicht neu angelegt.' }
|
||||
$item = $items[0]
|
||||
$row.Before = [string]$item.Value
|
||||
} else {
|
||||
$items = @(Get-VMHostService -VMHost $target | Where-Object Key -EQ $key)
|
||||
if ($items.Count -ne 1) { throw 'Dienst fehlt oder ist nicht eindeutig.' }
|
||||
$item = $items[0]
|
||||
$row.Before = "$($item.Policy)/$($item.Running)"
|
||||
}
|
||||
$row.After = $row.Before
|
||||
$row.Status = if ($row.Before -ceq $desired) { 'Match' } else { 'Drift' }
|
||||
if ($row.Status -eq 'Drift' -and $Mode -eq 'Apply') {
|
||||
if ($PSCmdlet.ShouldProcess("$($target.Name): $key", "$($row.Before) -> $desired")) {
|
||||
$row.Status = 'Applying'
|
||||
Save-Report # Persist original value before any remote mutation.
|
||||
if ($control.Kind -eq 'Advanced') {
|
||||
$null = Set-AdvancedSetting -AdvancedSetting $item -Value $entry.Value -Confirm:$false
|
||||
$row.After = [string](Get-AdvancedSetting -Entity $target -Name $key).Value
|
||||
} else {
|
||||
if ($item.Policy -ne $entry.Policy) { $null = Set-VMHostService -HostService $item -Policy $entry.Policy -Confirm:$false }
|
||||
if ($item.Running -ne $entry.Running) {
|
||||
if ($entry.Running) { $null = Start-VMHostService -HostService $item -Confirm:$false }
|
||||
else { $null = Stop-VMHostService -HostService $item -Confirm:$false }
|
||||
}
|
||||
$fresh = Get-VMHostService -VMHost $target | Where-Object Key -EQ $key
|
||||
$row.After = "$($fresh.Policy)/$($fresh.Running)"
|
||||
}
|
||||
$row.Status = if ($row.After -ceq $desired) { 'AppliedVerified' } else { 'VerificationFailed' }
|
||||
} else { $row.Status = 'Skipped' }
|
||||
}
|
||||
} catch { $row.Status = 'Error'; $row.Error = $_.Exception.Message; $row.After = $null }
|
||||
Save-Report
|
||||
}
|
||||
}
|
||||
$rows | Format-Table Host, Setting, Before, Desired, After, Status -AutoSize | Out-Host
|
||||
Write-Host "Bericht: $reportPath"
|
||||
if (@($rows | Where-Object Status -In @('Error','VerificationFailed')).Count) { throw 'Prüfung/Anwendung unvollständig. Bericht beachten; Teiländerungen sind möglich.' }
|
||||
Reference in New Issue
Block a user