1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
<#
.SYNOPSIS
Audit complet d'un environnement VMware vSphere avec export HTML.
.DESCRIPTION
Collecte l'inventaire VMs, datastores, snapshots, VMware Tools,
ressources CPU/RAM et génère un rapport HTML autonome.
.PARAMETER vCenter
FQDN ou IP du vCenter Server.
.PARAMETER Credential
PSCredential pour l'authentification vCenter.
.PARAMETER OutputPath
Chemin du rapport HTML généré. Défaut : .\audit-vmware-<date>.html
.EXAMPLE
.\Audit-VMware.ps1 -vCenter vcenter.mondomaine.local `
-Credential (Get-Credential) `
-OutputPath C:\Rapports\audit.html
.NOTES
Testé sur vSphere 6.7 / 7.0 / 8.0
Modules requis : VMware.PowerCLI >= 13.x
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$vCenter,
[Parameter(Mandatory)]
[System.Management.Automation.PSCredential]$Credential,
[string]$OutputPath = ".\audit-vmware-$(Get-Date -Format 'yyyyMMdd-HHmm').html",
# Seuil alerte snapshot (jours)
[int]$SnapshotAgeDays = 7,
# Seuil alerte datastore (% utilisé)
[int]$DatastoreThresholdPct = 80
)
#region --- Connexion vCenter ---
Write-Host "[*] Connexion à $vCenter..." -ForegroundColor Cyan
try {
$vi = Connect-VIServer -Server $vCenter -Credential $Credential -ErrorAction Stop
Write-Host "[+] Connecté : $($vi.Name) — vSphere $($vi.Version)" -ForegroundColor Green
}
catch {
Write-Error "Connexion impossible : $_"
exit 1
}
#endregion
#region --- Collecte données ---
Write-Host "[*] Collecte en cours..." -ForegroundColor Cyan
# Toutes les VMs avec leurs stats
$AllVMs = Get-VM | Select-Object `
Name,
PowerState,
NumCpu,
MemoryGB,
@{N="UsedSpaceGB"; E={[math]::Round($_.UsedSpaceGB, 2)}},
@{N="ProvisionedSpaceGB"; E={[math]::Round($_.ProvisionedSpaceGB, 2)}},
@{N="ToolsStatus"; E={$_.Guest.ExtensionData.ToolsStatus}},
@{N="ToolsVersion"; E={$_.Guest.ExtensionData.ToolsVersion}},
@{N="GuestOS"; E={$_.Guest.OSFullName}},
@{N="Host"; E={$_.VMHost.Name}},
@{N="Datastore"; E={($_ | Get-Datastore).Name -join ", "}},
@{N="Cluster"; E={
$cl = Get-Cluster -VM $_ -ErrorAction SilentlyContinue
if ($cl) { $cl.Name } else { "Standalone" }
}},
@{N="CPUUsageMHz"; E={
($_ | Get-Stat -Stat cpu.usagemhz.average `
-Start (Get-Date).AddDays(-7) `
-Finish (Get-Date) `
-MaxSamples 1 `
-ErrorAction SilentlyContinue).Value
}},
@{N="MemUsagePct"; E={
($_ | Get-Stat -Stat mem.usage.average `
-Start (Get-Date).AddDays(-7) `
-Finish (Get-Date) `
-MaxSamples 1 `
-ErrorAction SilentlyContinue).Value
}}
# Snapshots — tous les snapshots avec leur âge
$AllSnapshots = Get-VM | Get-Snapshot | Select-Object `
@{N="VM"; E={$_.VM.Name}},
Name,
Description,
Created,
@{N="AgeDays"; E={(New-TimeSpan -Start $_.Created -End (Get-Date)).Days}},
@{N="SizeGB"; E={[math]::Round($_.SizeGB, 2)}},
@{N="Alerte"; E={
if ((New-TimeSpan -Start $_.Created -End (Get-Date)).Days -ge $SnapshotAgeDays) {
"OUI"
} else { "non" }
}}
# Datastores
$AllDatastores = Get-Datastore | Select-Object `
Name,
Type,
@{N="CapacityGB"; E={[math]::Round($_.CapacityGB, 0)}},
@{N="FreeSpaceGB"; E={[math]::Round($_.FreeSpaceGB, 0)}},
@{N="UsedPct"; E={[math]::Round((1 - $_.FreeSpaceGB / $_.CapacityGB) * 100, 1)}},
@{N="Alerte"; E={
$pct = [math]::Round((1 - $_.FreeSpaceGB / $_.CapacityGB) * 100, 1)
if ($pct -ge $DatastoreThresholdPct) { "OUI" } else { "non" }
}},
@{N="VMCount"; E={@(Get-VM -Datastore $_).Count}}
# Hosts ESXi
$AllHosts = Get-VMHost | Select-Object `
Name,
@{N="Version"; E={"ESXi $($_.Version)"}},
ConnectionState,
@{N="CPUTotalGHz"; E={[math]::Round($_.CpuTotalMhz / 1000, 1)}},
@{N="CPUUsageGHz"; E={[math]::Round($_.CpuUsageMhz / 1000, 1)}},
@{N="RAMTotalGB"; E={[math]::Round($_.MemoryTotalGB, 0)}},
@{N="RAMUsageGB"; E={[math]::Round($_.MemoryUsageGB, 0)}},
@{N="VMCount"; E={@(Get-VM -VMHost $_).Count}},
@{N="Cluster"; E={
$cl = Get-Cluster -VMHost $_ -ErrorAction SilentlyContinue
if ($cl) { $cl.Name } else { "Standalone" }
}}
# Résumé global
$Summary = [PSCustomObject]@{
vCenter = $vCenter
DateAudit = (Get-Date -Format "dd/MM/yyyy HH:mm")
TotalVMs = $AllVMs.Count
VMsOn = ($AllVMs | Where-Object PowerState -eq "PoweredOn").Count
VMsOff = ($AllVMs | Where-Object PowerState -eq "PoweredOff").Count
TotalSnapshots = $AllSnapshots.Count
SnapAlertes = ($AllSnapshots | Where-Object Alerte -eq "OUI").Count
TotalHosts = $AllHosts.Count
TotalDatastores= $AllDatastores.Count
DSAlertes = ($AllDatastores | Where-Object Alerte -eq "OUI").Count
VMsNoTools = ($AllVMs | Where-Object { $_.ToolsStatus -ne "toolsOk" }).Count
}
Write-Host "[+] Collecte terminée — $($Summary.TotalVMs) VMs, $($Summary.TotalSnapshots) snapshots" `
-ForegroundColor Green
#endregion
#region --- Génération HTML ---
Write-Host "[*] Génération du rapport HTML..." -ForegroundColor Cyan
function ConvertTo-HtmlTable {
param(
[object[]]$Data,
[string]$AlertColumn = "",
[string]$AlertValue = "OUI"
)
if (-not $Data -or $Data.Count -eq 0) {
return "<p class='empty'>Aucune donnée</p>"
}
$headers = $Data[0].PSObject.Properties.Name
$html = "<table><thead><tr>"
foreach ($h in $headers) {
$html += "<th>$h</th>"
}
$html += "</tr></thead><tbody>"
foreach ($row in $Data) {
$alertClass = ""
if ($AlertColumn -and $row.$AlertColumn -eq $AlertValue) {
$alertClass = " class='alert-row'"
}
$html += "<tr$alertClass>"
foreach ($h in $headers) {
$val = $row.$h
if ($null -eq $val) { $val = "-" }
$html += "<td>$val</td>"
}
$html += "</tr>"
}
$html += "</tbody></table>"
return $html
}
$vmTable = ConvertTo-HtmlTable -Data $AllVMs
$snapshotTable = ConvertTo-HtmlTable -Data $AllSnapshots -AlertColumn "Alerte"
$datastoreTable = ConvertTo-HtmlTable -Data $AllDatastores -AlertColumn "Alerte"
$hostTable = ConvertTo-HtmlTable -Data $AllHosts
$html = @"
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Audit VMware — $($Summary.vCenter) — $($Summary.DateAudit)</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: 'Segoe UI', sans-serif; background: #0f1117; color: #e2e8f0; font-size: 14px; }
header { background: #1a1d2e; padding: 24px 32px; border-bottom: 2px solid #3b4fd8; }
header h1 { font-size: 22px; color: #7c8dff; }
header p { color: #94a3b8; margin-top: 4px; }
.container { max-width: 1400px; margin: 0 auto; padding: 24px 32px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; margin-bottom: 32px; }
.card { background: #1e2235; border-radius: 10px; padding: 20px; text-align: center; border: 1px solid #2d3352; }
.card .val { font-size: 32px; font-weight: 700; color: #7c8dff; }
.card .val.warn { color: #f59e0b; }
.card .val.ok { color: #10b981; }
.card .lbl { font-size: 12px; color: #64748b; margin-top: 6px; text-transform: uppercase; letter-spacing: .5px; }
section { margin-bottom: 40px; }
section h2 { font-size: 16px; color: #7c8dff; margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid #2d3352; }
table { width: 100%; border-collapse: collapse; background: #1e2235; border-radius: 8px; overflow: hidden; }
th { background: #2d3352; color: #94a3b8; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; padding: 10px 12px; text-align: left; }
td { padding: 9px 12px; border-bottom: 1px solid #252a3d; color: #cbd5e1; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #252a3d; }
.alert-row td { background: rgba(239,68,68,.08); color: #fca5a5; }
.alert-row:hover td { background: rgba(239,68,68,.15); }
.empty { color: #64748b; font-style: italic; padding: 12px 0; }
footer { text-align: center; padding: 24px; color: #475569; font-size: 12px; border-top: 1px solid #1e2235; }
</style>
</head>
<body>
<header>
<h1>Audit VMware vSphere</h1>
<p>$($Summary.vCenter) | $($Summary.DateAudit)</p>
</header>
<div class="container">
<div class="cards">
<div class="card"><div class="val ok">$($Summary.TotalVMs)</div><div class="lbl">VMs total</div></div>
<div class="card"><div class="val ok">$($Summary.VMsOn)</div><div class="lbl">VMs allumées</div></div>
<div class="card"><div class="val">$($Summary.VMsOff)</div><div class="lbl">VMs éteintes</div></div>
<div class="card"><div class="val $(if($Summary.SnapAlertes -gt 0){'warn'}else{'ok'})">$($Summary.TotalSnapshots)</div><div class="lbl">Snapshots ($($Summary.SnapAlertes) alertes)</div></div>
<div class="card"><div class="val">$($Summary.TotalHosts)</div><div class="lbl">Hosts ESXi</div></div>
<div class="card"><div class="val $(if($Summary.DSAlertes -gt 0){'warn'}else{'ok'})">$($Summary.TotalDatastores)</div><div class="lbl">Datastores ($($Summary.DSAlertes) alertes)</div></div>
<div class="card"><div class="val $(if($Summary.VMsNoTools -gt 0){'warn'}else{'ok'})">$($Summary.VMsNoTools)</div><div class="lbl">VMware Tools KO</div></div>
</div>
<section>
<h2>Hosts ESXi</h2>
$hostTable
</section>
<section>
<h2>Datastores</h2>
$datastoreTable
</section>
<section>
<h2>Snapshots (alertes en rouge — seuil : $SnapshotAgeDays jours)</h2>
$snapshotTable
</section>
<section>
<h2>Inventaire VMs</h2>
$vmTable
</section>
</div>
<footer>Généré par Audit-VMware.ps1 — PowerCLI $(Get-Module VMware.PowerCLI -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Version)</footer>
</body>
</html>
"@
$html | Out-File -FilePath $OutputPath -Encoding UTF8
Write-Host "[+] Rapport généré : $OutputPath" -ForegroundColor Green
#endregion
#region --- Déconnexion ---
Disconnect-VIServer -Server $vCenter -Confirm:$false
Write-Host "[+] Déconnecté de $vCenter" -ForegroundColor Green
#endregion
|