Skip to content

Commit d7c3383

Browse files
author
Alain Herve
committed
Update Test-Connections.ps1
Indentation and fix PSScriptAnalyzer 's [false positive](PowerShell/PSScriptAnalyzer#1504) at: ```powershell $Targets += [Target]::new($Target,( Start-Job -ScriptBlock -scriptblock { ... } -ArgumentList ```
1 parent f0893b5 commit d7c3383

File tree

1 file changed

+114
-108
lines changed

1 file changed

+114
-108
lines changed

Test-Connections/Public/Test-Connections.ps1

Lines changed: 114 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -34,134 +34,140 @@ function Test-Connections {
3434
none
3535
#Requires -Version 2.0
3636
#>
37-
[CmdletBinding(SupportsShouldProcess=$True)]
38-
Param
39-
(
40-
[Parameter(Mandatory=$True,ValueFromPipeline=$True,HelpMessage="Stop after sending Count pings")]
41-
[string[]]$TargetName,
37+
[CmdletBinding(SupportsShouldProcess = $True)]
38+
Param (
39+
[Parameter(Mandatory = $True, ValueFromPipeline = $True, HelpMessage = "Stop after sending Count pings")]
40+
[string[]]$TargetName,
4241

43-
[Parameter(Mandatory=$False)]
44-
[Alias("c")]
45-
[int]$Count=4,
42+
[Parameter(Mandatory = $False)]
43+
[Alias("c")]
44+
[int]$Count = 4,
4645

47-
[Parameter(Mandatory=$False,HelpMessage="Continjously send pings")]
48-
[Alias("Continuous","t")]
49-
[switch]$Repeat,
46+
[Parameter(Mandatory = $False, HelpMessage = "Continjously send pings")]
47+
[Alias("Continuous", "t")]
48+
[switch]$Repeat,
5049

51-
[Parameter(Mandatory=$False,HelpMessage="Delay between pings")]
52-
[int]$Delay=1,
50+
[Parameter(Mandatory = $False, HelpMessage = "Delay between pings")]
51+
[int]$Delay = 1,
5352

54-
[Parameter(Mandatory=$False,HelpMessage="Interval between pings")]
55-
[Alias("u")]
56-
[int]$Update=1000,
53+
[Parameter(Mandatory = $False, HelpMessage = "Interval between pings")]
54+
[Alias("u")]
55+
[int]$Update = 1000,
5756

58-
[Parameter(Mandatory=$False,HelpMessage="Watch")]
59-
[Alias("w")]
60-
[Switch]$Watch
61-
)
62-
Begin {
63-
Write-Verbose -Message "Begin $($MyInvocation.MyCommand)"
64-
$Targets = @()
65-
# Destingwish between Windows PowerShell and PowerShell Core
66-
$WindowsPowerShell = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq 'Desktop'
67-
$PowerShellCore = ! $WindowsPowerShell
68-
}
69-
Process {
70-
Write-Verbose -Message "Process $($MyInvocation.MyCommand)"
71-
If ($pscmdlet.ShouldProcess("$TargetName")) {
72-
ForEach ($Target in $TargetName) {
73-
Write-Verbose -Message "$Target, $Count, $Delay, $Repeat"
74-
Try {
57+
[Parameter(Mandatory = $False, HelpMessage = "Watch")]
58+
[Alias("w")]
59+
[Switch]$Watch
60+
)
61+
62+
Begin {
63+
Write-Verbose -Message "Begin $($MyInvocation.MyCommand)"
64+
$Targets = @()
65+
# Destingwish between Windows PowerShell and PowerShell Core
66+
$WindowsPowerShell = $PSVersionTable.PSEdition -and $PSVersionTable.PSEdition -eq 'Desktop'
67+
$PowerShellCore = ! $WindowsPowerShell
68+
}
69+
70+
Process {
71+
Write-Verbose -Message "Process $($MyInvocation.MyCommand)"
72+
If ($pscmdlet.ShouldProcess("$TargetName")) {
73+
ForEach ($Target in $TargetName) {
74+
Write-Verbose -Message "$Target, $Count, $Delay, $Repeat"
75+
Try {
7576
If ($WindowsPowerShell) {
7677
# Create new Target and Start-Job
7778
# Windows PowerShell 5.1 Test-Connection sucks, wrapper for Test-Connection to behave more like Test-Connection in PowerShell Core
78-
$Targets += [Target]::new($Target,(Start-Job -ScriptBlock {
79-
Param ([String]$TargetName, [int]$Count=4, [int]$Delay=1, [bool]$Repeat)
80-
$Ping = 0
81-
82-
While ($Repeat -or $Count -gt $Ping) {
83-
Write-Verbose "$($Repeat) $($Count) $($Ping)"
84-
$Ping++
85-
$icmp = Test-Connection -ComputerName $TargetName -Count 1 -ErrorAction SilentlyContinue
86-
If ($icmp) {
87-
[PSCustomObject]@{
88-
Ping = $Ping;
89-
Status = "Success"
90-
Latency = $icmp.ResponseTime
91-
}
92-
} else {
93-
[PSCustomObject]@{
94-
Ping = $Ping
95-
Status = "Failed"
96-
Latency = 9999
97-
}
98-
}
99-
Start-Sleep -Seconds $Delay
100-
}
101-
} -ArgumentList $Target, $Count, $Delay, $Repeat))
79+
$Targets += [Target]::new($Target, $(Start-Job -ScriptBlock $([ScriptBlock]::Create({
80+
Param ([String]$TargetName, [int]$Count = 4, [int]$Delay = 1, [bool]$Repeat)
81+
$Ping = 0
82+
While ($Repeat -or $Count -gt $Ping) {
83+
Write-Verbose "$($Repeat) $($Count) $($Ping)"
84+
$Ping++
85+
$icmp = Test-Connection -ComputerName $TargetName -Count 1 -ErrorAction SilentlyContinue
86+
If ($icmp) {
87+
[PSCustomObject]@{
88+
Ping = $Ping;
89+
Status = "Success"
90+
Latency = $icmp.ResponseTime
91+
}
92+
} else {
93+
[PSCustomObject]@{
94+
Ping = $Ping
95+
Status = "Failed"
96+
Latency = 9999
97+
}
98+
}
99+
Start-Sleep -Seconds $Delay
100+
}
101+
}
102+
)
103+
) -ArgumentList $Target, $Count, $Delay, $Repeat
104+
)
105+
)
102106
} else {
103107
If ($Repeat) {
104-
$Targets += [Target]::new($Target,(Start-Job -ScriptBlock {Param ($Target) Test-Connection -TargetName $Target -Ping -Repeat} -ArgumentList $Target))
108+
$Targets += [Target]::new($Target, $(Start-Job -ScriptBlock $([ScriptBlock]::Create({ Param ($Target) Test-Connection -TargetName $Target -Ping -Repeat })) -ArgumentList $Target))
105109
} else {
106-
$Targets += [Target]::new($Target,(Start-Job -ScriptBlock {Param ($Target, $Count) Test-Connection -TargetName $Target -Ping -Count $Count} -ArgumentList $Target, $Count))
110+
$Targets += [Target]::new($Target, $(Start-Job -ScriptBlock $([ScriptBlock]::Create({ Param ($Target, $Count) Test-Connection -TargetName $Target -Ping -Count $Count })) -ArgumentList $Target, $Count))
107111
}
108-
} } Catch { $_ }
109-
}
112+
}
113+
} Catch { $_ }
110114
}
111115
}
112-
End {
113-
Write-Verbose -Message "End $($MyInvocation.MyCommand)"
114-
If ($pscmdlet.ShouldProcess("$TargetName")) {
115-
# https://blog.sheehans.org/2018/10/27/powershell-taking-control-over-ctrl-c/
116-
# Change the default behavior of CTRL-C so that the script can intercept and use it versus just terminating the script.
117-
[Console]::TreatControlCAsInput=$True
118-
# Sleep for 1 second and then flush the key buffer so any previously pressed keys are discarded and the loop can monitor for the use of
119-
# CTRL-C. The sleep command ensures the buffer flushes correctly.
120-
Start-Sleep -Seconds 1
121-
$Host.UI.RawUI.FlushInputBuffer()
122-
# Continue to loop while there are pending or currently executing jobs.
123-
While ($Targets.Job.HasMoreData -contains "True") {
124-
# If a key was pressed during the loop execution, check to see if it was CTRL-C (aka "3"), and if so exit the script after clearing
125-
# out any running jobs and setting CTRL-C back to normal.
126-
If ($Host.UI.RawUI.KeyAvailable -and ($Key=$Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) {
127-
If ([Int]$Key.Character -eq 3) {
128-
Write-Warning -Message "Removing Test-Connection Jobs"
129-
If ($PowerShellCore) { Write-Host "`e[2A" }
130-
$Targets.Job | Remove-Job -Force
131-
$killed = $True
132-
[Console]::TreatControlCAsInput=$False
133-
134-
break
135-
}
136-
# Flush the key buffer again for the next loop.
137-
$Host.UI.RawUI.FlushInputBuffer()
138-
}
139-
# Perform other work here such as process pending jobs or process out current jobs.
140-
141-
# Get Test-Connection updates
142-
$Targets.Update()
116+
}
143117

144-
# Print Output
145-
$Targets.ToTable() | Format-Table
118+
End {
119+
Write-Verbose -Message "End $($MyInvocation.MyCommand)"
120+
If ($pscmdlet.ShouldProcess("$TargetName")) {
121+
# https://blog.sheehans.org/2018/10/27/powershell-taking-control-over-ctrl-c/
122+
# Change the default behavior of CTRL-C so that the script can intercept and use it versus just terminating the script.
123+
[Console]::TreatControlCAsInput = $True
124+
# Sleep for 1 second and then flush the key buffer so any previously pressed keys are discarded and the loop can monitor for the use of
125+
# CTRL-C. The sleep command ensures the buffer flushes correctly.
126+
Start-Sleep -Seconds 1
127+
$Host.UI.RawUI.FlushInputBuffer()
128+
# Continue to loop while there are pending or currently executing jobs.
129+
While ($Targets.Job.HasMoreData -contains "True") {
130+
# If a key was pressed during the loop execution, check to see if it was CTRL-C (aka "3"), and if so exit the script after clearing
131+
# out any running jobs and setting CTRL-C back to normal.
132+
If ($Host.UI.RawUI.KeyAvailable -and ($Key = $Host.UI.RawUI.ReadKey("AllowCtrlC,NoEcho,IncludeKeyUp"))) {
133+
If ([Int]$Key.Character -eq 3) {
134+
Write-Warning -Message "Removing Test-Connection Jobs"
135+
If ($PowerShellCore) { Write-Host "`e[2A" }
136+
$Targets.Job | Remove-Job -Force
137+
$killed = $True
138+
[Console]::TreatControlCAsInput = $False
146139

147-
# Move cursor up to overwrite old output
148-
If ($Watch -and $PowerShellCore) {
149-
Write-Host "`e[$($Targets.length+5)A"
140+
break
150141
}
151-
152-
# Output update delay
153-
Start-Sleep -Milliseconds $Update
142+
# Flush the key buffer again for the next loop.
143+
$Host.UI.RawUI.FlushInputBuffer()
154144
}
145+
# Perform other work here such as process pending jobs or process out current jobs.
155146

156-
# Clean up jobs
157-
If (!$killed) {
158-
$Targets.Job | Remove-Job -Force
159-
}
147+
# Get Test-Connection updates
148+
$Targets.Update()
149+
150+
# Print Output
151+
$Targets.ToTable() | Format-Table
160152

161-
# If in "Watch" mode, print output one last time
162-
If ($Watch) {
163-
$Targets.ToTable() | Format-Table
153+
# Move cursor up to overwrite old output
154+
If ($Watch -and $PowerShellCore) {
155+
Write-Host "`e[$($Targets.length+5)A"
164156
}
157+
158+
# Output update delay
159+
Start-Sleep -Milliseconds $Update
160+
}
161+
162+
# Clean up jobs
163+
If (!$killed) {
164+
$Targets.Job | Remove-Job -Force
165+
}
166+
167+
# If in "Watch" mode, print output one last time
168+
If ($Watch) {
169+
$Targets.ToTable() | Format-Table
165170
}
166171
}
167-
} #End function
172+
}
173+
} #End function

0 commit comments

Comments
 (0)