Skip to content

Commit

Permalink
v2.1.3. Fix bug in Invoke-SshCommand
Browse files Browse the repository at this point in the history
Invoke-SshCommand no longer terminates calling scripts when used with -InvokeOnAll and an empty SSH client store/pool.
  • Loading branch information
EliteLoser authored Feb 26, 2018
1 parent 2255f06 commit a733a63
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions SSHSessions.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ function Invoke-SshCommand {
if you specify both parameters.
#>
[CmdletBinding(
DefaultParameterSetName = "Command"
DefaultParameterSetName = "String"
)]
param(
[Parameter(ValueFromPipeline = $true,
Expand All @@ -264,40 +264,39 @@ function Invoke-SshCommand {
Position = 1)]
[String] $Command,

[Parameter(Mandatory = $true,
[Parameter(Mandatory = $True,
ParameterSetName="ScriptBlock",
Position = 1)]
[ScriptBlock] $ScriptBlock,

[Switch] $Quiet,
[Switch] $InvokeOnAll)
begin {
$WtfSkipFlag = $False
if ($InvokeOnAll) {
if ($ComputerName) {
$Answer = Read-Host -Prompt "You specified both -InvokeOnAll and -ComputerName. -InvokeOnAll overrides and targets all hosts.`nAre you sure you want to continue? (y/n) [yes]"
if ($Answer -imatch '^n') {
Write-Warning -Message "Aborting." -ErrorAction Stop
if ($Answer -imatch 'n') {
Write-Warning -Message "Aborting."
break
}
}
if ($Global:SshSessions.Keys.Count -eq 0) {
Write-Warning -Message "-InvokeOnAll specified, but no hosts found. See Get-Help New-SshSession."
break
# Remove the below 'break' so calling scripts don't terminate unwantedly. Done in v2.1.3.
# $ComputerName will be empty below so it doesn't really matter and then we avoid the unwanted calling
# script termination.
#break
}
# Get all computer names from the global SshSessions hashtable.
$ComputerName = $Global:SshSessions.Keys | Sort-Object -Property @{ Expression = {
# Intent: Sort IP addresses correctly.
[Regex]::Replace($_, '(\d+)', { '{0:D16}' -f [int] $args[0].Value }) }
}, @{ Expression = { $_ } }
}
# Better pipeline support requires this to be removed / commented out.
#if (-not $ComputerName) {
# "No computer names specified and -InvokeOnAll not specified. Can not continue."
# break
#}
}
process {
, @(foreach ($Computer in $ComputerName) {
,@(foreach ($Computer in $ComputerName) {
if (-not $Global:SshSessions.ContainsKey($Computer)) {
#Write-Verbose -Message "No SSH session found for $Computer. See Get-Help New-SshSession. Skipping."
Write-Warning -Message "[$Computer] No SSH session found. See Get-Help New-SshSession. Skipping."
Expand Down

0 comments on commit a733a63

Please sign in to comment.