-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest-ServerPingAvailabilityReport.ps1
More file actions
407 lines (363 loc) · 11.8 KB
/
Copy pathTest-ServerPingAvailabilityReport.ps1
File metadata and controls
407 lines (363 loc) · 11.8 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
<#
.SYNOPSIS
Server Ping Availability Report.
.DESCRIPTION
Testaa määriteltyjen palvelimien ICMP-saavutettavuuden ja muodostaa yksinkertaisen saatavuusraportin.
.REQUIREMENTS
- Verkkoyhteys kohdepalvelimiin ja ICMP sallittuna
.OUTPUTS
- HTML/CSV-raportti palvelimien ping-tilasta
.EXAMPLE
.\Test-ServerPingAvailabilityReport.ps1 -HostsToTest "server01.example.com","server02.example.com"
.NOTES
Author: Repository maintainer
Repository: PowerShell audit and reporting scripts
Script file: Test-ServerPingAvailabilityReport.ps1
.DISCLAIMER
AI-ASSISTED: Parts of this script may have been developed with assistance from ChatGPT/OpenAI.
REVIEW REQUIRED: The author/operator is responsible for reviewing, testing and validating the script before use.
TEST FIRST: Run and validate in a lab, sandbox, pilot group, test tenant or other non-production environment before production use.
USE AT YOUR OWN RISK: Provided as-is, without warranty. The author and AI provider are not responsible for damage, data loss or operational impact.
READ-ONLY INTENT: This script is intended for audit/reporting use. Verify permissions, scopes and commands before execution.
#>
param(
[string]$OutputFolder = (Join-Path $PSScriptRoot "output\ping"),
[ValidateSet("All","SuccessOnly","FailedOnly")]
[string]$Filter = "All",
[int]$TimeoutMs = 2000,
[int]$MaxConcurrentJobs = 15,
[string[]]$HostsToTest = @()
)
$ErrorActionPreference = "Stop"
if (-not $HostsToTest -or $HostsToTest.Count -eq 0) {
throw "No hosts were provided. Use -HostsToTest or wrap this script with your own environment-specific host source. Example: .\Test-ServerPingAvailabilityReport.ps1 -HostsToTest 'server01.example.com','server02.example.com'"
}
# ---------------------------------------
# Varmistukset
# ---------------------------------------
if (-not (Test-Path -LiteralPath $OutputFolder)) {
New-Item -ItemType Directory -Path $OutputFolder -Force | Out-Null
}
$TimeStamp = Get-Date -Format "yyyyMMdd_HHmmss"
$CsvPath = Join-Path $OutputFolder "PingResults_$TimeStamp.csv"
$HtmlPath = Join-Path $OutputFolder "PingResults_$TimeStamp.html"
# ---------------------------------------
# HTML helper
# ---------------------------------------
function New-SafeHtml {
param([object]$Text)
if ($null -eq $Text) { return "" }
return [System.Net.WebUtility]::HtmlEncode([string]$Text)
}
# ---------------------------------------
# Job scriptblock
# ---------------------------------------
$JobScript = {
param(
[string]$HostName,
[int]$TimeoutMs
)
$Resolved4 = @()
$Resolved6 = @()
$ReplyIP = $null
$ReplyRDNS = $null
$PingOk = $false
$Status = "Unknown"
$Rtt = $null
$ErrorText = $null
try {
try {
$dnsEntries = [System.Net.Dns]::GetHostAddresses($HostName)
$Resolved4 = $dnsEntries |
Where-Object { $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork } |
ForEach-Object { $_.IPAddressToString }
$Resolved6 = $dnsEntries |
Where-Object { $_.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6 } |
ForEach-Object { $_.IPAddressToString }
}
catch {
$ErrorText = "DNS resolve failed: $($_.Exception.Message)"
}
try {
$pinger = New-Object System.Net.NetworkInformation.Ping
$reply = $pinger.Send($HostName, $TimeoutMs)
if ($reply -and $reply.Status -eq [System.Net.NetworkInformation.IPStatus]::Success) {
$PingOk = $true
$Status = "Success"
$ReplyIP = $reply.Address.IPAddressToString
$Rtt = $reply.RoundtripTime
try {
$ptr = [System.Net.Dns]::GetHostEntry($ReplyIP)
if ($ptr -and $ptr.HostName) {
$ReplyRDNS = $ptr.HostName
}
}
catch {
$ReplyRDNS = ""
}
}
else {
if ($reply) {
$Status = $reply.Status.ToString()
}
else {
$Status = "NoReply"
}
}
}
catch {
if ([string]::IsNullOrWhiteSpace($ErrorText)) {
$ErrorText = "Ping failed: $($_.Exception.Message)"
}
$Status = "Error"
}
}
catch {
$Status = "Error"
$ErrorText = $_.Exception.Message
}
[PSCustomObject]@{
HostName = $HostName
ResolvedIPv4 = if ($Resolved4.Count -gt 0) { $Resolved4 -join ", " } else { "" }
ResolvedIPv6 = if ($Resolved6.Count -gt 0) { $Resolved6 -join ", " } else { "" }
ReplyIP = if ($ReplyIP) { $ReplyIP } else { "" }
ReplyReverseDNS = if ($ReplyRDNS) { $ReplyRDNS } else { "" }
Status = $Status
Success = $PingOk
ResponseTimeMs = $Rtt
Error = if ($ErrorText) { $ErrorText } else { "" }
}
}
# ---------------------------------------
# Jobit käyntiin throttlen kanssa
# ---------------------------------------
$Jobs = New-Object System.Collections.ArrayList
$Started = 0
$Total = $HostsToTest.Count
foreach ($HostName in $HostsToTest) {
while (($Jobs | Where-Object { $_.State -eq "Running" }).Count -ge $MaxConcurrentJobs) {
Start-Sleep -Milliseconds 300
}
$Started++
Write-Progress -Activity "Käynnistetään ping-jobit" `
-Status "$Started / $Total : $HostName" `
-PercentComplete (($Started / $Total) * 100)
$job = Start-Job -ScriptBlock $JobScript -ArgumentList $HostName, $TimeoutMs
[void]$Jobs.Add($job)
}
# ---------------------------------------
# Odota valmistumista
# ---------------------------------------
$Completed = 0
do {
$DoneNow = ($Jobs | Where-Object { $_.State -in @("Completed","Failed","Stopped") }).Count
if ($DoneNow -ne $Completed) {
$Completed = $DoneNow
Write-Progress -Activity "Odotetaan jobien valmistumista" `
-Status "$Completed / $Total valmiina" `
-PercentComplete (($Completed / $Total) * 100)
}
Start-Sleep -Milliseconds 300
}
while ($Completed -lt $Total)
Write-Progress -Activity "Odotetaan jobien valmistumista" -Completed
# ---------------------------------------
# Kerää tulokset
# ---------------------------------------
$Results = foreach ($job in $Jobs) {
try {
Receive-Job -Job $job -ErrorAction Stop
}
catch {
[PSCustomObject]@{
HostName = "<unknown>"
ResolvedIPv4 = ""
ResolvedIPv6 = ""
ReplyIP = ""
ReplyReverseDNS = ""
Status = "JobError"
Success = $false
ResponseTimeMs = $null
Error = $_.Exception.Message
}
}
}
# Siivotaan jobit pois
$Jobs | Remove-Job -Force -ErrorAction SilentlyContinue
# ---------------------------------------
# Järjestys
# ---------------------------------------
$Results = $Results | Sort-Object HostName
# ---------------------------------------
# Filtteröinti
# ---------------------------------------
$FilteredResults = switch ($Filter) {
"SuccessOnly" { $Results | Where-Object { $_.Success -eq $true } }
"FailedOnly" { $Results | Where-Object { $_.Success -ne $true } }
default { $Results }
}
# ---------------------------------------
# CSV
# ---------------------------------------
$FilteredResults | Export-Csv -Path $CsvPath -NoTypeInformation -Encoding UTF8
# ---------------------------------------
# HTML
# ---------------------------------------
$TotalCount = @($Results).Count
$SuccessCount = @($Results | Where-Object { $_.Success -eq $true }).Count
$FailedCount = $TotalCount - $SuccessCount
$RowsHtml = foreach ($row in $FilteredResults) {
$rowClass = if ($row.Success) { "ok" } else { "fail" }
@"
<tr class="$rowClass">
<td>$(New-SafeHtml $row.HostName)</td>
<td>$(New-SafeHtml $row.ResolvedIPv4)</td>
<td>$(New-SafeHtml $row.ResolvedIPv6)</td>
<td>$(New-SafeHtml $row.ReplyIP)</td>
<td>$(New-SafeHtml $row.ReplyReverseDNS)</td>
<td>$(New-SafeHtml $row.Status)</td>
<td>$(New-SafeHtml $row.ResponseTimeMs)</td>
<td>$(New-SafeHtml $row.Error)</td>
</tr>
"@
}
$Html = @"
<!doctype html>
<html lang="fi">
<head>
<meta charset="utf-8">
<title>Ping-raportti</title>
<style>
body {
font-family: Segoe UI, Arial, sans-serif;
background: #0f172a;
color: #e5e7eb;
margin: 24px;
}
h1, h2 {
margin-bottom: 8px;
}
.meta {
color: #94a3b8;
margin-bottom: 18px;
}
.cards {
display: flex;
gap: 16px;
flex-wrap: wrap;
margin: 16px 0 24px 0;
}
.card {
background: #111827;
border: 1px solid #334155;
border-radius: 10px;
padding: 14px 18px;
min-width: 180px;
}
.card .label {
color: #94a3b8;
font-size: 12px;
text-transform: uppercase;
}
.card .value {
font-size: 28px;
font-weight: 700;
margin-top: 6px;
}
table {
width: 100%;
border-collapse: collapse;
background: #111827;
border: 1px solid #334155;
}
th, td {
padding: 10px 12px;
border-bottom: 1px solid #1f2937;
text-align: left;
vertical-align: top;
font-size: 14px;
}
th {
background: #1e293b;
}
tr.ok td {
background: rgba(34,197,94,0.08);
}
tr.fail td {
background: rgba(239,68,68,0.08);
}
.small {
font-size: 12px;
color: #94a3b8;
}
</style>
</head>
<body>
<h1>Ping-raportti</h1>
<div class="meta">
Luotu: $(Get-Date -Format "dd.MM.yyyy HH:mm:ss")<br>
Filtteri: $(New-SafeHtml $Filter)<br>
Timeout: $(New-SafeHtml $TimeoutMs) ms<br>
MaxConcurrentJobs: $(New-SafeHtml $MaxConcurrentJobs)
</div>
<div class="cards">
<div class="card">
<div class="label">Yhteensä</div>
<div class="value">$TotalCount</div>
</div>
<div class="card">
<div class="label">Onnistuneet</div>
<div class="value">$SuccessCount</div>
</div>
<div class="card">
<div class="label">Epäonnistuneet</div>
<div class="value">$FailedCount</div>
</div>
<div class="card">
<div class="label">Näytetyt rivit</div>
<div class="value">$(@($FilteredResults).Count)</div>
</div>
</div>
<table>
<thead>
<tr>
<th>HostName</th>
<th>Resolved IPv4</th>
<th>Resolved IPv6</th>
<th>Reply IP</th>
<th>Reverse DNS</th>
<th>Status</th>
<th>ResponseTimeMs</th>
<th>Error</th>
</tr>
</thead>
<tbody>
$($RowsHtml -join "`r`n")
</tbody>
</table>
<p class="small">
Success = host vastasi ICMP-pingiin. Reply IP kertoo miltä osoitteelta vastaus tuli. Reverse DNS yrittää tehdä PTR-haun vastaavalle IP:lle.
</p>
</body>
</html>
"@
Set-Content -Path $HtmlPath -Value $Html -Encoding UTF8
# ---------------------------------------
# Ruutunäkymä
# ---------------------------------------
$FilteredResults |
Select-Object HostName, ResolvedIPv4, ReplyIP, ReplyReverseDNS, Status, ResponseTimeMs, Error |
Format-Table -AutoSize
# ---------------------------------------
# Yhteenveto
# ---------------------------------------
Write-Host ""
Write-Host "Valmis." -ForegroundColor Green
Write-Host "CSV : $CsvPath"
Write-Host "HTML: $HtmlPath"
Write-Host ""
$Results |
Group-Object Status |
Sort-Object Count -Descending |
Select-Object Count, Name |
Format-Table -AutoSize