Skip to content

Respect -Repeat/-MtuSize/-Traceroute:$false in Test-Connection#26479

Merged
iSazonov merged 2 commits intoPowerShell:masterfrom
yotsuda:fix/test-connection-switches
Nov 21, 2025
Merged

Respect -Repeat/-MtuSize/-Traceroute:$false in Test-Connection#26479
iSazonov merged 2 commits intoPowerShell:masterfrom
yotsuda:fix/test-connection-switches

Conversation

@yotsuda
Copy link
Contributor

@yotsuda yotsuda commented Nov 18, 2025

PR Summary

This PR fixes the issue where -Repeat:$false, -MtuSize:$false, and -Traceroute:$false are not respected in Test-Connection, causing them to behave the same as their $true counterparts.

Fixes #26475
Related to #25242

PR Context

When any of these switch parameters is explicitly set to $false, the cmdlet incorrectly performs the associated operation (continuous ping, MTU detection, or traceroute) instead of performing a standard ping. This occurs because the code uses a switch statement on ParameterSetName without checking the actual boolean values of the parameters.

This fix changes the parameter set resolution logic from a switch statement to an if-else chain that directly checks each parameter's boolean value, ensuring that explicit $false values are properly respected.

Changes

  • Modified TestConnectionCommand.cs: Changed switch statement to if-else chain that checks Repeat, MtuSize, and Traceroute property values instead of just the parameter set name
  • Added 3 test cases in Test-Connection.Tests.ps1 to verify each switch parameter respects $false

Testing

New tests added:

  1. "Repeat with explicit false should behave like default ping"
  2. "MtuSize with explicit false should behave like default ping"
  3. "Traceroute with explicit false should behave like default ping"

Test results:

  • Pre-fix: All 3 new tests fail
    • -Repeat:$false runs continuously instead of 4 pings
    • -MtuSize:$false returns PingMtuStatus instead of PingStatus
    • -Traceroute:$false returns TraceStatus instead of PingStatus
  • Post-fix: All 3 new tests pass
    • Each returns standard PingStatus with 4 pings
  • All existing tests continue to pass

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Nov 19, 2025
@iSazonov iSazonov requested a review from Copilot November 19, 2025 05:57
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug where explicitly setting switch parameters to $false (e.g., -Repeat:$false, -MtuSize:$false, -Traceroute:$false) in Test-Connection was incorrectly treated the same as setting them to $true. The root cause was that the code checked which parameter set was active rather than the actual boolean values of the switch parameters.

Key changes:

  • Changed parameter set resolution from switch statement to if-else chain that checks boolean values of Repeat, MtuSize, and Traceroute
  • Updated Repeat.IsPresent to Repeat for consistency with the new approach
  • Added three test cases to verify each switch parameter respects explicit $false values

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs Modified BeginProcessing() and ProcessRecord() to check switch parameter boolean values instead of parameter set names; updated SetCountForTcpTest() to use implicit bool conversion
test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 Added three test cases verifying that -Repeat:$false, -MtuSize:$false, and -Traceroute:$false behave like default ping operations

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@iSazonov iSazonov merged commit fb6403d into PowerShell:master Nov 21, 2025
42 of 43 checks passed
@yotsuda yotsuda deleted the fix/test-connection-switches branch November 21, 2025 12:12
@iSazonov iSazonov self-assigned this Nov 22, 2025
SIRMARGIN pushed a commit to SIRMARGIN/PowerShell that referenced this pull request Dec 12, 2025
kilasuit pushed a commit to kilasuit/PowerShell that referenced this pull request Jan 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

-Repeat/-MtuSize/-Traceroute:$false not respected in Test-Connection

3 participants