-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNetworkDiagnostics.psm1
More file actions
424 lines (348 loc) · 13.2 KB
/
NetworkDiagnostics.psm1
File metadata and controls
424 lines (348 loc) · 13.2 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
# NetworkDiagnostics Module
# Provides functions for network diagnostics and troubleshooting
# Supports Windows 10/11 and PowerShell 5.1+
#region Module Requirements
# Import required modules
Import-Module Common
# Check PowerShell version
if ($PSVersionTable.PSVersion.Major -lt 5) {
throw "This module requires PowerShell 5.1 or later."
}
# Check Windows version
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
if (-not ($osInfo.Caption -match "Windows 10|Windows 11")) {
throw "This module requires Windows 10 or Windows 11."
}
#endregion
#region Public Functions
<#
.SYNOPSIS
Performs comprehensive network diagnostics.
.DESCRIPTION
Performs a series of network diagnostic tests including connectivity,
DNS resolution, routing, and latency tests. Can target specific
network components or perform a full diagnostic.
.PARAMETER ComputerName
Optional. The computer to diagnose. Defaults to the local computer.
.PARAMETER Target
Optional. Specific target to test (e.g., "8.8.8.8" for Google DNS).
If not specified, tests general connectivity.
.PARAMETER Tests
Optional. Specific tests to perform (Connectivity, DNS, Routing,
Latency). If not specified, performs all tests.
.PARAMETER Detailed
Optional. When specified, returns detailed diagnostic results.
.EXAMPLE
Start-NetworkDiagnostics
Performs a full network diagnostic on the local computer.
.EXAMPLE
Start-NetworkDiagnostics -Target "8.8.8.8" -Tests "Latency","DNS"
Tests latency and DNS resolution to Google DNS.
.EXAMPLE
Start-NetworkDiagnostics -ComputerName "server01" -Detailed
Performs a detailed network diagnostic on a remote computer.
.OUTPUTS
System.Management.Automation.PSCustomObject
Returns custom objects containing diagnostic results.
.NOTES
Some tests may require administrative privileges.
Results may vary based on network configuration.
#>
function Start-NetworkDiagnostics {
[CmdletBinding()]
param(
[Parameter()]
[string]$ComputerName = $env:COMPUTERNAME,
[Parameter()]
[string]$Target,
[Parameter()]
[ValidateSet('Connectivity', 'DNS', 'Routing', 'Latency')]
[string[]]$Tests = @('Connectivity', 'DNS', 'Routing', 'Latency'),
[Parameter()]
[switch]$Detailed
)
try {
$results = @{}
$target = if ($Target) { $Target } else { "8.8.8.8" }
if ($Tests -contains 'Connectivity') {
Write-Verbose "Testing network connectivity..."
$pingResult = Test-Connection -ComputerName $target -Count 4 -ErrorAction Stop
$results.Connectivity = @{
Success = $pingResult.Status -contains 'Success'
PacketsSent = $pingResult.Count
PacketsReceived = ($pingResult.Status -eq 'Success').Count
AverageLatency = ($pingResult | Where-Object Status -eq 'Success' | Measure-Object -Property ResponseTime -Average).Average
}
}
if ($Tests -contains 'DNS') {
Write-Verbose "Testing DNS resolution..."
$dnsResult = Resolve-DnsName -Name $target -ErrorAction Stop
$results.DNS = @{
Success = $true
ResolvedAddresses = $dnsResult.IPAddress
Type = $dnsResult.Type
}
}
if ($Tests -contains 'Routing') {
Write-Verbose "Testing network routing..."
$routeResult = Test-NetRoute -DestinationPrefix $target -ErrorAction Stop
$results.Routing = @{
Success = $routeResult.Status -eq 'Success'
NextHop = $routeResult.NextHop
InterfaceIndex = $routeResult.InterfaceIndex
}
}
if ($Tests -contains 'Latency') {
Write-Verbose "Testing network latency..."
$latencyResult = Test-Connection -ComputerName $target -Count 10 -ErrorAction Stop
$results.Latency = @{
Average = ($latencyResult | Measure-Object -Property ResponseTime -Average).Average
Minimum = ($latencyResult | Measure-Object -Property ResponseTime -Minimum).Minimum
Maximum = ($latencyResult | Measure-Object -Property ResponseTime -Maximum).Maximum
PacketLoss = (($latencyResult.Status -ne 'Success').Count / $latencyResult.Count) * 100
}
}
if ($Detailed) {
$results | ForEach-Object {
[PSCustomObject]@{
Test = $_.Key
Results = $_.Value
Timestamp = Get-Date
ComputerName = $ComputerName
Target = $target
}
}
} else {
[PSCustomObject]@{
Status = "Completed"
Tests = $Tests
ComputerName = $ComputerName
Target = $target
}
}
}
catch {
Write-Error "Failed to perform network diagnostics: $_"
throw
}
}
<#
.SYNOPSIS
Analyzes network traffic patterns.
.DESCRIPTION
Captures and analyzes network traffic patterns including protocol
distribution, bandwidth usage, and connection statistics.
Can monitor specific interfaces or all network traffic.
.PARAMETER ComputerName
Optional. The computer to analyze. Defaults to the local computer.
.PARAMETER Interface
Optional. Specific network interface to monitor.
If not specified, monitors all interfaces.
.PARAMETER Duration
Optional. Duration of capture in seconds. Defaults to 60 seconds.
.PARAMETER Detailed
Optional. When specified, returns detailed traffic analysis.
.EXAMPLE
Start-NetworkTrafficAnalysis
Analyzes network traffic on all interfaces for 60 seconds.
.EXAMPLE
Start-NetworkTrafficAnalysis -Interface "Ethernet" -Duration 300
Analyzes traffic on the Ethernet interface for 5 minutes.
.EXAMPLE
Start-NetworkTrafficAnalysis -ComputerName "server01" -Detailed
Performs a detailed traffic analysis on a remote computer.
.OUTPUTS
System.Management.Automation.PSCustomObject
Returns custom objects containing traffic analysis results.
.NOTES
Requires administrative privileges.
May impact network performance during capture.
#>
function Start-NetworkTrafficAnalysis {
[CmdletBinding()]
param(
[Parameter()]
[string]$ComputerName = $env:COMPUTERNAME,
[Parameter()]
[string]$Interface,
[Parameter()]
[int]$Duration = 60,
[Parameter()]
[switch]$Detailed
)
try {
$results = @{
StartTime = Get-Date
EndTime = (Get-Date).AddSeconds($Duration)
Traffic = @{}
}
Write-Verbose "Starting network traffic analysis..."
$interfaces = if ($Interface) {
Get-NetAdapter | Where-Object Name -eq $Interface
} else {
Get-NetAdapter | Where-Object Status -eq 'Up'
}
foreach ($if in $interfaces) {
$traffic = @{
BytesReceived = 0
BytesSent = 0
PacketsReceived = 0
PacketsSent = 0
Protocols = @{}
}
$startStats = Get-NetAdapterStatistics -Name $if.Name
Start-Sleep -Seconds $Duration
$endStats = Get-NetAdapterStatistics -Name $if.Name
$traffic.BytesReceived = $endStats.ReceivedBytes - $startStats.ReceivedBytes
$traffic.BytesSent = $endStats.SentBytes - $startStats.SentBytes
$traffic.PacketsReceived = $endStats.ReceivedUnicastPackets - $startStats.ReceivedUnicastPackets
$traffic.PacketsSent = $endStats.SentUnicastPackets - $startStats.SentUnicastPackets
$results.Traffic[$if.Name] = $traffic
}
if ($Detailed) {
$results | ForEach-Object {
[PSCustomObject]@{
Interface = $_.Key
Statistics = $_.Value
StartTime = $results.StartTime
EndTime = $results.EndTime
ComputerName = $ComputerName
}
}
} else {
[PSCustomObject]@{
Status = "Completed"
Interfaces = $interfaces.Name
Duration = $Duration
ComputerName = $ComputerName
}
}
}
catch {
Write-Error "Failed to analyze network traffic: $_"
throw
}
}
<#
.SYNOPSIS
Diagnoses network connectivity issues.
.DESCRIPTION
Performs a series of tests to diagnose common network connectivity
issues including DNS problems, routing issues, and firewall
restrictions. Can target specific issues or perform a full diagnosis.
.PARAMETER ComputerName
Optional. The computer to diagnose. Defaults to the local computer.
.PARAMETER Target
Optional. Specific target to test (e.g., "8.8.8.8" for Google DNS).
If not specified, tests general connectivity.
.PARAMETER Issues
Optional. Specific issues to diagnose (DNS, Routing, Firewall).
If not specified, diagnoses all issues.
.PARAMETER Detailed
Optional. When specified, returns detailed diagnosis results.
.EXAMPLE
Start-NetworkTroubleshooting
Performs a full network troubleshooting on the local computer.
.EXAMPLE
Start-NetworkTroubleshooting -Target "server01" -Issues "DNS","Firewall"
Diagnoses DNS and firewall issues for a specific target.
.EXAMPLE
Start-NetworkTroubleshooting -ComputerName "server01" -Detailed
Performs a detailed network troubleshooting on a remote computer.
.OUTPUTS
System.Management.Automation.PSCustomObject
Returns custom objects containing troubleshooting results.
.NOTES
Requires administrative privileges.
Some tests may require network access to be temporarily modified.
#>
function Start-NetworkTroubleshooting {
[CmdletBinding()]
param(
[Parameter()]
[string]$ComputerName = $env:COMPUTERNAME,
[Parameter()]
[string]$Target,
[Parameter()]
[ValidateSet('DNS', 'Routing', 'Firewall')]
[string[]]$Issues = @('DNS', 'Routing', 'Firewall'),
[Parameter()]
[switch]$Detailed
)
try {
$results = @{}
$target = if ($Target) { $Target } else { "8.8.8.8" }
if ($Issues -contains 'DNS') {
Write-Verbose "Diagnosing DNS issues..."
$dnsResults = @{
LocalDNS = $null
PublicDNS = $null
DNSCache = $null
}
# Test local DNS
$dnsResults.LocalDNS = Resolve-DnsName -Name $target -Server (Get-DnsClientServerAddress).ServerAddresses[0] -ErrorAction SilentlyContinue
# Test public DNS
$dnsResults.PublicDNS = Resolve-DnsName -Name $target -Server "8.8.8.8" -ErrorAction SilentlyContinue
# Check DNS cache
$dnsResults.DNSCache = Get-DnsClientCache -ErrorAction SilentlyContinue
$results.DNS = $dnsResults
}
if ($Issues -contains 'Routing') {
Write-Verbose "Diagnosing routing issues..."
$routeResults = @{
Traceroute = $null
RouteTable = $null
DefaultGateway = $null
}
# Perform traceroute
$routeResults.Traceroute = Test-NetConnection -ComputerName $target -TraceRoute -ErrorAction SilentlyContinue
# Get route table
$routeResults.RouteTable = Get-NetRoute -ErrorAction SilentlyContinue
# Check default gateway
$routeResults.DefaultGateway = Get-NetRoute -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue
$results.Routing = $routeResults
}
if ($Issues -contains 'Firewall') {
Write-Verbose "Diagnosing firewall issues..."
$firewallResults = @{
Rules = $null
Status = $null
BlockedConnections = $null
}
# Get firewall rules
$firewallResults.Rules = Get-NetFirewallRule -ErrorAction SilentlyContinue
# Check firewall status
$firewallResults.Status = Get-NetFirewallProfile -ErrorAction SilentlyContinue
# Check blocked connections
$firewallResults.BlockedConnections = Get-NetFirewallApplicationFilter -ErrorAction SilentlyContinue
$results.Firewall = $firewallResults
}
if ($Detailed) {
$results | ForEach-Object {
[PSCustomObject]@{
Issue = $_.Key
Diagnosis = $_.Value
Timestamp = Get-Date
ComputerName = $ComputerName
Target = $target
}
}
} else {
[PSCustomObject]@{
Status = "Completed"
Issues = $Issues
ComputerName = $ComputerName
Target = $target
}
}
}
catch {
Write-Error "Failed to perform network troubleshooting: $_"
throw
}
}
Export-ModuleMember -Function @(
'Start-NetworkDiagnostics',
'Start-NetworkTrafficAnalysis',
'Start-NetworkTroubleshooting'
)