-
Notifications
You must be signed in to change notification settings - Fork 2
/
func_PingTrace.ps1
109 lines (97 loc) · 3.72 KB
/
func_PingTrace.ps1
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
<#
.Synopsis
Function that runs a 'PowerShell' ping and trace route - optionally adds it to an alert note
.NOTES
To help with https://thwack.solarwinds.com/product-forums/the-orion-platform/f/alert-lab/91386/embedding-output-into-an-alert-email-action
#>
function Test-PingTrace {
[CmdletBinding(
HelpUri = 'https://thwack.solarwinds.com/product-forums/the-orion-platform/f/alert-lab/91386/embedding-output-into-an-alert-email-action/',
SupportsShouldProcess = $true,
ConfirmImpact = 'Low')]
[Alias()]
[OutputType([String])]
Param
(
# The IP Address of the thing on which to operate
[Parameter(Mandatory = $true)]
[Alias("IP")]
[string]$IPAddress,
# Number of times to try and ping (default to 4)
[int]$NumPings = 4,
# Number of times to hop for a trace (default to 15)
[int]$NumHops = 15
)
Begin {
# Nothing to see here
}
Process {
if ( $pscmdlet.ShouldProcess("$IPAddress", "Ping the thing") ) {
Write-Verbose -Message "Sending $NumPings to '$IPAddress'"
# Using Test-Connection adds a whole bunch of other stuff we don't need, so get the basics
$PingResults = Test-Connection -ComputerName $IPAddress -Count $NumPings -ErrorAction SilentlyContinue | Select-Object -Property @{ Name = "Source"; Expression = { $_.PSComputerName } }, Address, ReplySize, ResponseTime
Write-Verbose -Message "Running a trace to '$IPAddress' with up to $NumHops hops"
# Tracing using Test-NetConnection adds a whole bunch of other stuff we don't need, so get the basics
$TraceRtResults = Test-NetConnection -ComputerName $IPAddress -TraceRoute -Hops $NumHops -ErrorAction SilentlyContinue | Select-Object SourceAddress, RemoteAddress, @{ Name = "RTT"; Expression = { $_.PingReplyDetails.RoundtripTime } }, TraceRoute
# Start with an empty Note
$Note = ""
if ( $PingResults ) {
# Add the ping success information to the Note
$Note += @"
Ping Results:
-----------------------------------------------------
Source Address: $( $env:COMPUTERNAME )
Remote Address: $( $PingResults[0].Address )
Number of Pings: $NumPings
Avg Response Time (ms): $( $PingResults | Measure-Object -Property ResponseTime -Average | Select-Object -ExpandProperty Average )
`n
"@
}
else {
# Add the ping failure information to the Note
$Note += @"
Ping Results:
-----------------------------------------------------
Source Address: $( $env:COMPUTERNAME )
Remote Address: $IPAddress
Number of Pings: $NumPings
Avg Response Time (ms): N/A
***** PING FAILED *****
`n
"@
}
if ( $TraceRtResults ) {
# Add the tracert success information to the Note
$Note += @"
Trace Route Results:
-----------------------------------------------------
Source Address: $( $env:COMPUTERNAME )
Remote Address: $IPAddress
Round Trip Time (ms): $( $TraceRtResults.RTT )
Hops:
$( $TraceRtResults.TraceRoute | ForEach-Object { "`t$( $_ )`n" } )
`n
"@
}
else {
# Add the tracert success information to the Note
$Note += @"
Trace Route Results:
-----------------------------------------------------
Source Address: $( $env:COMPUTERNAME )
Remote Address: $IPAddress
Round Trip Time (ms): N/A
Hops:
N/A
***** TRACE FAILED *****
`n
"@
}
# We've finished building the note, so let's send it back outside the function
$Note
}
}
End {
# Nothing to do here
}
}