From 9d29809e289e4125981960ee90b394f0f02dfc31 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 15:57:49 -0400 Subject: [PATCH 01/63] :art: Apply Roslyn analyzer fixes --- .../management/TestConnectionCommand.cs | 54 +++++++++---------- 1 file changed, 25 insertions(+), 29 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 19974c8891b..2fcd9566c04 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -232,10 +232,8 @@ protected override void ProcessRecord() private void ProcessConnectionByTCPPort(String targetNameOrAddress) { - string resolvedTargetName = null; - IPAddress targetAddress = null; - if (!InitProcessPing(targetNameOrAddress, out resolvedTargetName, out targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) { return; } @@ -302,8 +300,10 @@ private void WriteConnectionTestProgress(string targetNameOrAddress, string targ private void WriteConnectionTestFooter() { - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - record.RecordType = ProgressRecordType.Completed; + ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + { + RecordType = ProgressRecordType.Completed + }; WriteProgress(record); } @@ -312,11 +312,9 @@ private void WriteConnectionTestFooter() #region TracerouteTest private void ProcessTraceroute(String targetNameOrAddress) { - string resolvedTargetName = null; - IPAddress targetAddress = null; byte[] buffer = GetSendBuffer(BufferSize); - if (!InitProcessPing(targetNameOrAddress, out resolvedTargetName, out targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) { return; } @@ -406,12 +404,11 @@ private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targ } private string _testConnectionProgressBarActivity; - private static string[] s_PSHostTag = new string[] { "PSHOST" }; + private static readonly string[] s_PSHostTag = new string[] { "PSHOST" }; private void WriteTraceRouteProgress(TraceRouteReply traceRouteReply) { - string msg = string.Empty; - + string msg; if (traceRouteReply.PingReplies[2].Status == IPStatus.TtlExpired || traceRouteReply.PingReplies[2].Status == IPStatus.Success) { var routerAddress = traceRouteReply.ReplyRouterAddress.ToString(); @@ -437,8 +434,10 @@ private void WriteTraceRouteFooter() { WriteInformation(TestConnectionResources.TraceRouteComplete, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - record.RecordType = ProgressRecordType.Completed; + ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + { + RecordType = ProgressRecordType.Completed + }; WriteProgress(record); } @@ -513,10 +512,7 @@ private void ProcessMTUSize(String targetNameOrAddress) { PingReply reply, replyResult = null; - string resolvedTargetName = null; - IPAddress targetAddress = null; - - if (!InitProcessPing(targetNameOrAddress, out resolvedTargetName, out targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) { return; } @@ -641,8 +637,10 @@ private void WriteMTUSizeProgress(int currentMTUSize, int retry) private void WriteMTUSizeFooter() { - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - record.RecordType = ProgressRecordType.Completed; + ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + { + RecordType = ProgressRecordType.Completed + }; WriteProgress(record); } @@ -652,10 +650,7 @@ private void WriteMTUSizeFooter() private void ProcessPing(String targetNameOrAddress) { - string resolvedTargetName = null; - IPAddress targetAddress = null; - - if (!InitProcessPing(targetNameOrAddress, out resolvedTargetName, out targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) { return; } @@ -670,7 +665,7 @@ private void ProcessPing(String targetNameOrAddress) Ping sender = new Ping(); PingOptions pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); - PingReply reply = null; + PingReply reply; PingReport pingReport = new PingReport(Source, resolvedTargetName); Int32 timeout = TimeoutSeconds * 1000; Int32 delay = Delay * 1000; @@ -753,7 +748,7 @@ private void WritePingHeader(string resolvedTargetName, string targetAddress) private void WritePingProgress(PingReply reply) { - string msg = string.Empty; + string msg; if (reply.Status != IPStatus.Success) { msg = TestConnectionResources.PingTimeOut; @@ -777,8 +772,10 @@ private void WritePingFooter() { WriteInformation(TestConnectionResources.PingComplete, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - record.RecordType = ProgressRecordType.Completed; + ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + { + RecordType = ProgressRecordType.Completed + }; WriteProgress(record); } @@ -814,10 +811,9 @@ internal PingReport(string source, string destination) private bool InitProcessPing(String targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress) { - IPHostEntry hostEntry = null; - resolvedTargetName = targetNameOrAddress; + IPHostEntry hostEntry; if (IPAddress.TryParse(targetNameOrAddress, out targetAddress)) { if (ResolveDestination) From 8603a4bf8d9d85cb332e8b7108226a14e3931b29 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 16:05:03 -0400 Subject: [PATCH 02/63] :art: Whitespace style fixes --- .../management/TestConnectionCommand.cs | 144 ++++++++++-------- 1 file changed, 80 insertions(+), 64 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 2fcd9566c04..1954ae29de0 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -158,10 +158,11 @@ public class TestConnectionCommand : PSCmdlet /// /// Destination - computer name or IP address. /// - [Parameter(Mandatory = true, - Position = 0, - ValueFromPipeline = true, - ValueFromPipelineByPropertyName = true)] + [Parameter( + Mandatory = true, + Position = 0, + ValueFromPipeline = true, + ValueFromPipelineByPropertyName = true)] [ValidateNotNullOrEmpty] [Alias("ComputerName")] public string[] TargetName { get; set; } @@ -290,10 +291,11 @@ private void WriteConnectionTestHeader(string resolvedTargetName, string targetA private void WriteConnectionTestProgress(string targetNameOrAddress, string targetAddress, int timeout) { - var msg = StringUtil.Format(TestConnectionResources.ConnectionTestDescription, - targetNameOrAddress, - targetAddress, - timeout); + var msg = StringUtil.Format( + TestConnectionResources.ConnectionTestDescription, + targetNameOrAddress, + targetAddress, + timeout); ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); WriteProgress(record); } @@ -348,14 +350,16 @@ private void ProcessTraceroute(String targetNameOrAddress) } catch (PingException ex) { - string message = StringUtil.Format(TestConnectionResources.NoPingResult, - resolvedTargetName, - ex.Message); + string message = StringUtil.Format( + TestConnectionResources.NoPingResult, + resolvedTargetName, + ex.Message); Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - resolvedTargetName); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + resolvedTargetName); WriteError(errorRecord); continue; @@ -415,9 +419,10 @@ private void WriteTraceRouteProgress(TraceRouteReply traceRouteReply) var routerName = traceRouteReply.ReplyRouterName ?? routerAddress; var roundtripTime0 = traceRouteReply.PingReplies[0].Status == IPStatus.TimedOut ? "*" : traceRouteReply.PingReplies[0].RoundtripTime.ToString(); var roundtripTime1 = traceRouteReply.PingReplies[1].Status == IPStatus.TimedOut ? "*" : traceRouteReply.PingReplies[1].RoundtripTime.ToString(); - msg = StringUtil.Format(TestConnectionResources.TraceRouteReply, - traceRouteReply.Hop, roundtripTime0, roundtripTime1, traceRouteReply.PingReplies[2].RoundtripTime.ToString(), - routerName, routerAddress); + msg = StringUtil.Format( + TestConnectionResources.TraceRouteReply, + traceRouteReply.Hop, roundtripTime0, roundtripTime1, traceRouteReply.PingReplies[2].RoundtripTime.ToString(), + routerName, routerAddress); } else { @@ -536,7 +541,6 @@ private void ProcessMTUSize(String targetNameOrAddress) byte[] buffer = GetSendBuffer(CurrentMTUSize); WriteMTUSizeProgress(CurrentMTUSize, retry); - WriteDebug(StringUtil.Format("LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2}", LowMTUSize, CurrentMTUSize, HighMTUSize)); reply = sender.Send(targetAddress, timeout, buffer, pingOptions); @@ -558,14 +562,16 @@ private void ProcessMTUSize(String targetNameOrAddress) // Target host don't reply - try again up to 'Count'. if (retry >= Count) { - string message = StringUtil.Format(TestConnectionResources.NoPingResult, - targetAddress, - reply.Status.ToString()); + string message = StringUtil.Format( + TestConnectionResources.NoPingResult, + targetAddress, + reply.Status.ToString()); Exception pingException = new System.Net.NetworkInformation.PingException(message); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - targetAddress); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + targetAddress); WriteError(errorRecord); return; } @@ -586,10 +592,11 @@ private void ProcessMTUSize(String targetNameOrAddress) { string message = StringUtil.Format(TestConnectionResources.NoPingResult, targetAddress, ex.Message); Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - targetAddress); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + targetAddress); WriteError(errorRecord); return; } @@ -618,10 +625,11 @@ private void ProcessMTUSize(String targetNameOrAddress) private void WriteMTUSizeHeader(string resolvedTargetName, string targetAddress) { - _testConnectionProgressBarActivity = StringUtil.Format(TestConnectionResources.MTUSizeDetectStart, - resolvedTargetName, - targetAddress, - BufferSize); + _testConnectionProgressBarActivity = StringUtil.Format( + TestConnectionResources.MTUSizeDetectStart, + resolvedTargetName, + targetAddress, + BufferSize); ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); WriteProgress(record); @@ -680,10 +688,11 @@ private void ProcessPing(String targetNameOrAddress) { string message = StringUtil.Format(TestConnectionResources.NoPingResult, resolvedTargetName, ex.Message); Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - resolvedTargetName); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + resolvedTargetName); WriteError(errorRecord); quietResult = false; @@ -733,16 +742,18 @@ private void ProcessPing(String targetNameOrAddress) private void WritePingHeader(string resolvedTargetName, string targetAddress) { - _testConnectionProgressBarActivity = StringUtil.Format(TestConnectionResources.MTUSizeDetectStart, - resolvedTargetName, - targetAddress, - BufferSize); + _testConnectionProgressBarActivity = StringUtil.Format( + TestConnectionResources.MTUSizeDetectStart, + resolvedTargetName, + targetAddress, + BufferSize); WriteInformation(_testConnectionProgressBarActivity, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, - _testConnectionProgressBarActivity, - ProgressRecordSpace); + ProgressRecord record = new ProgressRecord( + s_ProgressId, + _testConnectionProgressBarActivity, + ProgressRecordSpace); WriteProgress(record); } @@ -755,11 +766,12 @@ private void WritePingProgress(PingReply reply) } else { - msg = StringUtil.Format(TestConnectionResources.PingReply, - reply.Address.ToString(), - reply.Buffer.Length, - reply.RoundtripTime, - reply.Options?.Ttl); + msg = StringUtil.Format( + TestConnectionResources.PingReply, + reply.Address.ToString(), + reply.Buffer.Length, + reply.RoundtripTime, + reply.Options?.Ttl); } WriteInformation(msg, s_PSHostTag); @@ -836,14 +848,16 @@ private bool InitProcessPing(String targetNameOrAddress, out string resolvedTarg } catch (Exception ex) { - string message = StringUtil.Format(TestConnectionResources.NoPingResult, - resolvedTargetName, - TestConnectionResources.CannotResolveTargetName); + string message = StringUtil.Format( + TestConnectionResources.NoPingResult, + resolvedTargetName, + TestConnectionResources.CannotResolveTargetName); Exception pingException = new System.Net.NetworkInformation.PingException(message, ex); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - resolvedTargetName); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + resolvedTargetName); WriteError(errorRecord); return false; } @@ -863,14 +877,16 @@ private bool InitProcessPing(String targetNameOrAddress, out string resolvedTarg if (targetAddress == null) { - string message = StringUtil.Format(TestConnectionResources.NoPingResult, - resolvedTargetName, - TestConnectionResources.TargetAddressAbsent); + string message = StringUtil.Format( + TestConnectionResources.NoPingResult, + resolvedTargetName, + TestConnectionResources.TargetAddressAbsent); Exception pingException = new System.Net.NetworkInformation.PingException(message, null); - ErrorRecord errorRecord = new ErrorRecord(pingException, - TestConnectionExceptionId, - ErrorCategory.ResourceUnavailable, - resolvedTargetName); + ErrorRecord errorRecord = new ErrorRecord( + pingException, + TestConnectionExceptionId, + ErrorCategory.ResourceUnavailable, + resolvedTargetName); WriteError(errorRecord); return false; } From ea04a0b0ce8d87fe0b7cdf08f1a1142b0d733966 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 16:23:49 -0400 Subject: [PATCH 03/63] :art: style pass for odds and ends Simplify some variable declarations, fix up some uber long lines, etc. --- .../management/TestConnectionCommand.cs | 150 +++++++++++------- 1 file changed, 91 insertions(+), 59 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 1954ae29de0..a2b4270df7d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -16,12 +16,13 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "Test-Connection" cmdlet. /// - [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPingCount, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] - [OutputType(typeof(PingReport), ParameterSetName = new string[] { ParameterSetPingCount })] - [OutputType(typeof(PingReply), ParameterSetName = new string[] { ParameterSetPingContinues, ParameterSetDetectionOfMTUSize })] - [OutputType(typeof(bool), ParameterSetName = new string[] { ParameterSetPingCount, ParameterSetPingContinues, ParameterSetConnectionByTCPPort })] - [OutputType(typeof(Int32), ParameterSetName = new string[] { ParameterSetDetectionOfMTUSize })] - [OutputType(typeof(TraceRouteReply), ParameterSetName = new string[] { ParameterSetTraceRoute })] + [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPingCount, + HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] + [OutputType(typeof(PingReport), ParameterSetName = new[] { ParameterSetPingCount })] + [OutputType(typeof(PingReply), ParameterSetName = new[] { ParameterSetPingContinues, ParameterSetDetectionOfMTUSize })] + [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPingCount, ParameterSetPingContinues, ParameterSetConnectionByTCPPort })] + [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] + [OutputType(typeof(TraceRouteReply), ParameterSetName = new[] { ParameterSetTraceRoute })] public class TestConnectionCommand : PSCmdlet { private const string ParameterSetPingCount = "PingCount"; @@ -231,7 +232,7 @@ protected override void ProcessRecord() #region ConnectionTest - private void ProcessConnectionByTCPPort(String targetNameOrAddress) + private void ProcessConnectionByTCPPort(string targetNameOrAddress) { if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) @@ -241,12 +242,12 @@ private void ProcessConnectionByTCPPort(String targetNameOrAddress) WriteConnectionTestHeader(resolvedTargetName, targetAddress.ToString()); - TcpClient client = new TcpClient(); + var client = new TcpClient(); try { Task connectionTask = client.ConnectAsync(targetAddress, TCPPort); - string targetString = targetAddress.ToString(); + var targetString = targetAddress.ToString(); for (var i = 1; i <= TimeoutSeconds; i++) { @@ -284,8 +285,11 @@ private void ProcessConnectionByTCPPort(String targetNameOrAddress) private void WriteConnectionTestHeader(string resolvedTargetName, string targetAddress) { - _testConnectionProgressBarActivity = StringUtil.Format(TestConnectionResources.ConnectionTestStart, resolvedTargetName, targetAddress); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); + _testConnectionProgressBarActivity = StringUtil.Format( + TestConnectionResources.ConnectionTestStart, + resolvedTargetName, + targetAddress); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); WriteProgress(record); } @@ -296,13 +300,13 @@ private void WriteConnectionTestProgress(string targetNameOrAddress, string targ targetNameOrAddress, targetAddress, timeout); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); WriteProgress(record); } private void WriteConnectionTestFooter() { - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) { RecordType = ProgressRecordType.Completed }; @@ -312,7 +316,7 @@ private void WriteConnectionTestFooter() #endregion ConnectionTest #region TracerouteTest - private void ProcessTraceroute(String targetNameOrAddress) + private void ProcessTraceroute(string targetNameOrAddress) { byte[] buffer = GetSendBuffer(BufferSize); @@ -323,13 +327,13 @@ private void ProcessTraceroute(String targetNameOrAddress) WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); - TraceRouteResult traceRouteResult = new TraceRouteResult(Source, targetAddress, resolvedTargetName); + var traceRouteResult = new TraceRouteResult(Source, targetAddress, resolvedTargetName); - Int32 currentHop = 1; - Ping sender = new Ping(); - PingOptions pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); + int currentHop = 1; + var sender = new Ping(); + var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); PingReply reply = null; - Int32 timeout = TimeoutSeconds * 1000; + int timeout = TimeoutSeconds * 1000; do { @@ -354,8 +358,8 @@ private void ProcessTraceroute(String targetNameOrAddress) TestConnectionResources.NoPingResult, resolvedTargetName, ex.Message); - Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord( + var pingException = new PingException(message, ex.InnerException); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, @@ -383,7 +387,10 @@ private void ProcessTraceroute(String targetNameOrAddress) WriteTraceRouteProgress(traceRouteReply); traceRouteResult.Replies.Add(traceRouteReply); - } while (reply != null && currentHop <= sMaxHops && (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)); + } while ( + reply != null + && currentHop <= sMaxHops + && (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)); WriteTraceRouteFooter(); @@ -399,11 +406,15 @@ private void ProcessTraceroute(String targetNameOrAddress) private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targetAddress) { - _testConnectionProgressBarActivity = StringUtil.Format(TestConnectionResources.TraceRouteStart, resolvedTargetName, targetAddress, MaxHops); + _testConnectionProgressBarActivity = StringUtil.Format( + TestConnectionResources.TraceRouteStart, + resolvedTargetName, + targetAddress, + MaxHops); WriteInformation(_testConnectionProgressBarActivity, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); WriteProgress(record); } @@ -413,15 +424,23 @@ private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targ private void WriteTraceRouteProgress(TraceRouteReply traceRouteReply) { string msg; - if (traceRouteReply.PingReplies[2].Status == IPStatus.TtlExpired || traceRouteReply.PingReplies[2].Status == IPStatus.Success) + if (traceRouteReply.PingReplies[2].Status == IPStatus.TtlExpired + || traceRouteReply.PingReplies[2].Status == IPStatus.Success) { var routerAddress = traceRouteReply.ReplyRouterAddress.ToString(); var routerName = traceRouteReply.ReplyRouterName ?? routerAddress; - var roundtripTime0 = traceRouteReply.PingReplies[0].Status == IPStatus.TimedOut ? "*" : traceRouteReply.PingReplies[0].RoundtripTime.ToString(); - var roundtripTime1 = traceRouteReply.PingReplies[1].Status == IPStatus.TimedOut ? "*" : traceRouteReply.PingReplies[1].RoundtripTime.ToString(); + var roundtripTime0 = traceRouteReply.PingReplies[0].Status == IPStatus.TimedOut + ? "*" + : traceRouteReply.PingReplies[0].RoundtripTime.ToString(); + var roundtripTime1 = traceRouteReply.PingReplies[1].Status == IPStatus.TimedOut + ? "*" + : traceRouteReply.PingReplies[1].RoundtripTime.ToString(); msg = StringUtil.Format( TestConnectionResources.TraceRouteReply, - traceRouteReply.Hop, roundtripTime0, roundtripTime1, traceRouteReply.PingReplies[2].RoundtripTime.ToString(), + traceRouteReply.Hop, + roundtripTime0, + roundtripTime1, + traceRouteReply.PingReplies[2].RoundtripTime.ToString(), routerName, routerAddress); } else @@ -431,7 +450,7 @@ private void WriteTraceRouteProgress(TraceRouteReply traceRouteReply) WriteInformation(msg, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); WriteProgress(record); } @@ -439,7 +458,7 @@ private void WriteTraceRouteFooter() { WriteInformation(TestConnectionResources.TraceRouteComplete, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) { RecordType = ProgressRecordType.Completed }; @@ -513,7 +532,7 @@ internal TraceRouteResult(string source, IPAddress destinationAddress, string de #endregion TracerouteTest #region MTUSizeTest - private void ProcessMTUSize(String targetNameOrAddress) + private void ProcessMTUSize(string targetNameOrAddress) { PingReply reply, replyResult = null; @@ -528,12 +547,12 @@ private void ProcessMTUSize(String targetNameOrAddress) int HighMTUSize = 10000; int CurrentMTUSize = 1473; int LowMTUSize = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 1280 : 68; - Int32 timeout = TimeoutSeconds * 1000; + int timeout = TimeoutSeconds * 1000; try { - Ping sender = new Ping(); - PingOptions pingOptions = new PingOptions(MaxHops, true); + var sender = new Ping(); + var pingOptions = new PingOptions(MaxHops, true); int retry = 1; while (LowMTUSize < (HighMTUSize - 1)) @@ -541,7 +560,11 @@ private void ProcessMTUSize(String targetNameOrAddress) byte[] buffer = GetSendBuffer(CurrentMTUSize); WriteMTUSizeProgress(CurrentMTUSize, retry); - WriteDebug(StringUtil.Format("LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2}", LowMTUSize, CurrentMTUSize, HighMTUSize)); + WriteDebug(StringUtil.Format( + "LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2}", + LowMTUSize, + CurrentMTUSize, + HighMTUSize)); reply = sender.Send(targetAddress, timeout, buffer, pingOptions); @@ -566,8 +589,8 @@ private void ProcessMTUSize(String targetNameOrAddress) TestConnectionResources.NoPingResult, targetAddress, reply.Status.ToString()); - Exception pingException = new System.Net.NetworkInformation.PingException(message); - ErrorRecord errorRecord = new ErrorRecord( + var pingException = new System.Net.NetworkInformation.PingException(message); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, @@ -591,8 +614,8 @@ private void ProcessMTUSize(String targetNameOrAddress) catch (PingException ex) { string message = StringUtil.Format(TestConnectionResources.NoPingResult, targetAddress, ex.Message); - Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord( + var pingException = new PingException(message, ex.InnerException); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, @@ -631,7 +654,7 @@ private void WriteMTUSizeHeader(string resolvedTargetName, string targetAddress) targetAddress, BufferSize); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); WriteProgress(record); } @@ -639,13 +662,13 @@ private void WriteMTUSizeProgress(int currentMTUSize, int retry) { var msg = StringUtil.Format(TestConnectionResources.MTUSizeDetectDescription, currentMTUSize, retry); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); WriteProgress(record); } private void WriteMTUSizeFooter() { - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) { RecordType = ProgressRecordType.Completed }; @@ -656,7 +679,7 @@ private void WriteMTUSizeFooter() #region PingTest - private void ProcessPing(String targetNameOrAddress) + private void ProcessPing(string targetNameOrAddress) { if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) { @@ -671,12 +694,12 @@ private void ProcessPing(String targetNameOrAddress) bool quietResult = true; byte[] buffer = GetSendBuffer(BufferSize); - Ping sender = new Ping(); - PingOptions pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); PingReply reply; - PingReport pingReport = new PingReport(Source, resolvedTargetName); - Int32 timeout = TimeoutSeconds * 1000; - Int32 delay = Delay * 1000; + var sender = new Ping(); + var pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); + var pingReport = new PingReport(Source, resolvedTargetName); + int timeout = TimeoutSeconds * 1000; + int delay = Delay * 1000; for (int i = 1; i <= Count; i++) { @@ -686,9 +709,12 @@ private void ProcessPing(String targetNameOrAddress) } catch (PingException ex) { - string message = StringUtil.Format(TestConnectionResources.NoPingResult, resolvedTargetName, ex.Message); - Exception pingException = new System.Net.NetworkInformation.PingException(message, ex.InnerException); - ErrorRecord errorRecord = new ErrorRecord( + string message = StringUtil.Format( + TestConnectionResources.NoPingResult, + resolvedTargetName, + ex.Message); + var pingException = new PingException(message, ex.InnerException); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, @@ -750,7 +776,7 @@ private void WritePingHeader(string resolvedTargetName, string targetAddress) WriteInformation(_testConnectionProgressBarActivity, s_PSHostTag); - ProgressRecord record = new ProgressRecord( + var record = new ProgressRecord( s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); @@ -784,7 +810,10 @@ private void WritePingFooter() { WriteInformation(TestConnectionResources.PingComplete, s_PSHostTag); - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) + ProgressRecord record = new ProgressRecord( + s_ProgressId, + _testConnectionProgressBarActivity, + ProgressRecordSpace) { RecordType = ProgressRecordType.Completed }; @@ -821,7 +850,10 @@ internal PingReport(string source, string destination) #endregion PingTest - private bool InitProcessPing(String targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress) + private bool InitProcessPing( + string targetNameOrAddress, + out string resolvedTargetName, + out IPAddress targetAddress) { resolvedTargetName = targetNameOrAddress; @@ -852,8 +884,8 @@ private bool InitProcessPing(String targetNameOrAddress, out string resolvedTarg TestConnectionResources.NoPingResult, resolvedTargetName, TestConnectionResources.CannotResolveTargetName); - Exception pingException = new System.Net.NetworkInformation.PingException(message, ex); - ErrorRecord errorRecord = new ErrorRecord( + var pingException = new PingException(message, ex); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, @@ -864,7 +896,7 @@ private bool InitProcessPing(String targetNameOrAddress, out string resolvedTarg if (IPv6 || IPv4) { - AddressFamily addressFamily = IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; + var addressFamily = IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; foreach (var address in hostEntry.AddressList) { @@ -881,8 +913,8 @@ private bool InitProcessPing(String targetNameOrAddress, out string resolvedTarg TestConnectionResources.NoPingResult, resolvedTargetName, TestConnectionResources.TargetAddressAbsent); - Exception pingException = new System.Net.NetworkInformation.PingException(message, null); - ErrorRecord errorRecord = new ErrorRecord( + var pingException = new PingException(message, null); + var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, ErrorCategory.ResourceUnavailable, From 7a70349cfb5ae88b43fff0135c6f08bb8ff40827 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 18:27:28 -0400 Subject: [PATCH 04/63] :sparkles: Add new PingStatus class --- .../management/TestConnectionCommand.cs | 222 ++++++++++-------- 1 file changed, 130 insertions(+), 92 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index a2b4270df7d..7a8ed39f5f8 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -465,70 +465,6 @@ private void WriteTraceRouteFooter() WriteProgress(record); } - /// - /// The class contains an information about a trace route attempt. - /// - public class TraceRouteReply - { - internal TraceRouteReply() - { - PingReplies = new List(DefaultTraceRoutePingCount); - } - - /// - /// Number of current hop (router). - /// - public int Hop; - - /// - /// List of ping replies for current hop (router). - /// - public List PingReplies; - - /// - /// Router IP address. - /// - public IPAddress ReplyRouterAddress; - - /// - /// Resolved router name. - /// - public string ReplyRouterName; - } - - /// - /// The class contains an information about the source, the destination and trace route results. - /// - public class TraceRouteResult - { - internal TraceRouteResult(string source, IPAddress destinationAddress, string destinationHost) - { - Source = source; - DestinationAddress = destinationAddress; - DestinationHost = destinationHost; - Replies = new List(); - } - - /// - /// Source from which to trace route. - /// - public string Source { get; } - - /// - /// Destination to which to trace route. - /// - public IPAddress DestinationAddress { get; } - - /// - /// Destination to which to trace route. - /// - public string DestinationHost { get; } - - /// - /// - public List Replies { get; } - } - #endregion TracerouteTest #region MTUSizeTest @@ -820,34 +756,6 @@ private void WritePingFooter() WriteProgress(record); } - /// - /// The class contains an information about the source, the destination and ping results. - /// - public class PingReport - { - internal PingReport(string source, string destination) - { - Source = source; - Destination = destination; - Replies = new List(); - } - - /// - /// Source from which to ping. - /// - public string Source { get; } - - /// - /// Destination to which to ping. - /// - public string Destination { get; } - - /// - /// Ping results for every ping attempt. - /// - public List Replies { get; } - } - #endregion PingTest private bool InitProcessPing( @@ -956,6 +864,136 @@ private byte[] GetSendBuffer(int bufferSize) return sendBuffer; } + /// + /// IDisposable implementation. + /// + public void Dispose() + { + _sender?.Dispose(); + } + + /// + /// The class contains an information about a trace route attempt. + /// + public class TraceRouteReply + { + internal TraceRouteReply() + { + PingReplies = new List(DefaultTraceRoutePingCount); + } + + /// + /// Number of current hop (router). + /// + public int Hop; + + /// + /// List of ping replies for current hop (router). + /// + public List PingReplies; + + /// + /// Router IP address. + /// + public IPAddress ReplyRouterAddress; + + /// + /// Resolved router name. + /// + public string ReplyRouterName; + } + + /// + /// The class contains an information about the source, the destination and trace route results. + /// + public class TraceRouteResult + { + internal TraceRouteResult(string source, IPAddress destinationAddress, string destinationHost) + { + Source = source; + DestinationAddress = destinationAddress; + DestinationHost = destinationHost; + Replies = new List(); + } + + /// + /// Source from which to trace route. + /// + public string Source { get; } + + /// + /// Destination to which to trace route. + /// + public IPAddress DestinationAddress { get; } + + /// + /// Destination to which to trace route. + /// + public string DestinationHost { get; } + + /// + /// + public List Replies { get; } + } + + /// + /// The class contains information about the source, the destination and ping results. + /// + public class PingStatus + { + internal PingStatus(string source, string destination, uint mtuSize, PingReply reply) + : this(source, destination, reply) => MtuSize = mtuSize; + + internal PingStatus(string source, string destination, PingReply reply) + { + _reply = reply; + Source = source; + Destination = destination; + } + + private readonly PingReply _reply; + + /// + /// Source from which to ping. + /// + public string Source { get; } + + /// + /// The target address of the ping. + /// + /// + public IPAddress Address { get => _reply.Address; } + + /// + /// Destination to which to ping. + /// + public string Destination { get; } + + /// + /// The roundtrip time of the ping in milliseconds. + /// + /// + public long Latency { get => _reply.RoundtripTime; } + + /// + /// The size in bytes of the buffer data sent in the ping. + /// + /// + public int BufferSize { get => _reply.Buffer.Length; } + + /// + /// The options used when sending the ping. + /// + /// + public PingOptions Options { get => _reply.Options; } + + /// + /// The maximum transmission unit size on the network path between the source and destination. + /// + /// + public uint? MtuSize { get; } + } + // Count of pings sent per each trace route hop. // Default = 3 (from Windows). // If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string. From 89cda884eadab02be610d5eb2339433c6560c7de Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 18:29:40 -0400 Subject: [PATCH 05/63] :sparkles: implement IDisposable & some misc cleanup. Implementing IDisposable allows us to put the Ping() instances used everywhere into a single field and dispose it whenever the command is. --- .../management/TestConnectionCommand.cs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 7a8ed39f5f8..17aa5f47549 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -18,18 +18,19 @@ namespace Microsoft.PowerShell.Commands /// [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPingCount, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] - [OutputType(typeof(PingReport), ParameterSetName = new[] { ParameterSetPingCount })] + [OutputType(typeof(PingStatus), ParameterSetName = new[] { ParameterSetPingCount })] [OutputType(typeof(PingReply), ParameterSetName = new[] { ParameterSetPingContinues, ParameterSetDetectionOfMTUSize })] [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPingCount, ParameterSetPingContinues, ParameterSetConnectionByTCPPort })] [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] [OutputType(typeof(TraceRouteReply), ParameterSetName = new[] { ParameterSetTraceRoute })] - public class TestConnectionCommand : PSCmdlet + public class TestConnectionCommand : PSCmdlet, IDisposable { private const string ParameterSetPingCount = "PingCount"; private const string ParameterSetPingContinues = "PingContinues"; private const string ParameterSetTraceRoute = "TraceRoute"; private const string ParameterSetConnectionByTCPPort = "ConnectionByTCPPort"; private const string ParameterSetDetectionOfMTUSize = "DetectionOfMTUSize"; + private const int sMaxHops = 128; #region Parameters @@ -94,8 +95,6 @@ public class TestConnectionCommand : PSCmdlet [Alias("Ttl", "TimeToLive", "Hops")] public int MaxHops { get; set; } = sMaxHops; - private const int sMaxHops = 128; - /// /// Count of attempts. /// The default (from Windows) is 4 times. @@ -189,6 +188,8 @@ public class TestConnectionCommand : PSCmdlet #endregion Parameters + private readonly Ping _sender = new Ping(); + /// /// Init the cmdlet. /// @@ -330,7 +331,6 @@ private void ProcessTraceroute(string targetNameOrAddress) var traceRouteResult = new TraceRouteResult(Source, targetAddress, resolvedTargetName); int currentHop = 1; - var sender = new Ping(); var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); PingReply reply = null; int timeout = TimeoutSeconds * 1000; @@ -348,7 +348,7 @@ private void ProcessTraceroute(string targetNameOrAddress) { try { - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); traceRouteReply.PingReplies.Add(reply); } @@ -487,7 +487,6 @@ private void ProcessMTUSize(string targetNameOrAddress) try { - var sender = new Ping(); var pingOptions = new PingOptions(MaxHops, true); int retry = 1; @@ -502,7 +501,7 @@ private void ProcessMTUSize(string targetNameOrAddress) CurrentMTUSize, HighMTUSize)); - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); // Cautious! Algorithm is sensitive to changing boundary values. if (reply.Status == IPStatus.PacketTooBig) @@ -525,7 +524,7 @@ private void ProcessMTUSize(string targetNameOrAddress) TestConnectionResources.NoPingResult, targetAddress, reply.Status.ToString()); - var pingException = new System.Net.NetworkInformation.PingException(message); + var pingException = new PingException(message); var errorRecord = new ErrorRecord( pingException, TestConnectionExceptionId, @@ -631,9 +630,8 @@ private void ProcessPing(string targetNameOrAddress) byte[] buffer = GetSendBuffer(BufferSize); PingReply reply; - var sender = new Ping(); var pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); - var pingReport = new PingReport(Source, resolvedTargetName); + var pingReport = new PingStatus(Source, resolvedTargetName); int timeout = TimeoutSeconds * 1000; int delay = Delay * 1000; @@ -641,7 +639,7 @@ private void ProcessPing(string targetNameOrAddress) { try { - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); } catch (PingException ex) { From 4c14c17e0d55914f9e5622c43946ee45639d9dc4 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 18 Jun 2019 20:58:05 -0400 Subject: [PATCH 06/63] :sparkles: Update traceroute functionality --- .../management/TestConnectionCommand.cs | 167 ++++++++++-------- 1 file changed, 98 insertions(+), 69 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 17aa5f47549..353669ff55a 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Management.Automation; using System.Management.Automation.Internal; using System.Net; @@ -22,7 +23,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(PingReply), ParameterSetName = new[] { ParameterSetPingContinues, ParameterSetDetectionOfMTUSize })] [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPingCount, ParameterSetPingContinues, ParameterSetConnectionByTCPPort })] [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] - [OutputType(typeof(TraceRouteReply), ParameterSetName = new[] { ParameterSetTraceRoute })] + [OutputType(typeof(TraceStatus), ParameterSetName = new[] { ParameterSetTraceRoute })] public class TestConnectionCommand : PSCmdlet, IDisposable { private const string ParameterSetPingCount = "PingCount"; @@ -326,31 +327,44 @@ private void ProcessTraceroute(string targetNameOrAddress) return; } - WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); - - var traceRouteResult = new TraceRouteResult(Source, targetAddress, resolvedTargetName); + //WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); int currentHop = 1; var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); - PingReply reply = null; + var replies = new List(DefaultTraceRoutePingCount); int timeout = TimeoutSeconds * 1000; + string hostname = null; + var timer = new Stopwatch(); + PingReply reply; do { - TraceRouteReply traceRouteReply = new TraceRouteReply(); - - pingOptions.Ttl = traceRouteReply.Hop = currentHop; - currentHop++; + reply = null; + pingOptions.Ttl = currentHop; - // In the specific case we don't use 'Count' property. + // We don't allow -Count parameter for -TraceRoute. // If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string. for (int i = 1; i <= DefaultTraceRoutePingCount; i++) { try { + timer.Start(); reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); + timer.Stop(); - traceRouteReply.PingReplies.Add(reply); + if (ResolveDestination && reply.Status == IPStatus.Success) + { + hostname = Dns.GetHostEntry(reply.Address).HostName; + } + + replies.Add(new PingStatus( + Source, + hostname, + reply, + pingOptions, + timer.ElapsedMilliseconds, + buffer.Length)); + timer.Reset(); } catch (PingException ex) { @@ -377,31 +391,25 @@ private void ProcessTraceroute(string targetNameOrAddress) Thread.Sleep(200); } - if (ResolveDestination && reply.Status == IPStatus.Success) + if (!Quiet.IsPresent) { - traceRouteReply.ReplyRouterName = Dns.GetHostEntry(reply.Address).HostName; + WriteObject(new TraceStatus(currentHop, replies, Source, targetNameOrAddress, targetAddress)); } - traceRouteReply.ReplyRouterAddress = reply.Address; - - WriteTraceRouteProgress(traceRouteReply); - - traceRouteResult.Replies.Add(traceRouteReply); - } while ( - reply != null + currentHop++; + replies.Clear(); + //WriteTraceRouteProgress(traceRouteReply); + } + while (reply != null && currentHop <= sMaxHops && (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)); - WriteTraceRouteFooter(); + //WriteTraceRouteFooter(); if (Quiet.IsPresent) { WriteObject(currentHop <= sMaxHops); } - else - { - WriteObject(traceRouteResult); - } } private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targetAddress) @@ -421,26 +429,26 @@ private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targ private string _testConnectionProgressBarActivity; private static readonly string[] s_PSHostTag = new string[] { "PSHOST" }; - private void WriteTraceRouteProgress(TraceRouteReply traceRouteReply) + private void WriteTraceRouteProgress(TraceStatus traceRouteReply) { string msg; - if (traceRouteReply.PingReplies[2].Status == IPStatus.TtlExpired - || traceRouteReply.PingReplies[2].Status == IPStatus.Success) + if (traceRouteReply.Replies[2].Status == IPStatus.TtlExpired + || traceRouteReply.Replies[2].Status == IPStatus.Success) { var routerAddress = traceRouteReply.ReplyRouterAddress.ToString(); var routerName = traceRouteReply.ReplyRouterName ?? routerAddress; - var roundtripTime0 = traceRouteReply.PingReplies[0].Status == IPStatus.TimedOut + var roundtripTime0 = traceRouteReply.Replies[0].Status == IPStatus.TimedOut ? "*" - : traceRouteReply.PingReplies[0].RoundtripTime.ToString(); - var roundtripTime1 = traceRouteReply.PingReplies[1].Status == IPStatus.TimedOut + : traceRouteReply.Replies[0].RoundtripTime.ToString(); + var roundtripTime1 = traceRouteReply.Replies[1].Status == IPStatus.TimedOut ? "*" - : traceRouteReply.PingReplies[1].RoundtripTime.ToString(); + : traceRouteReply.Replies[1].RoundtripTime.ToString(); msg = StringUtil.Format( TestConnectionResources.TraceRouteReply, traceRouteReply.Hop, roundtripTime0, roundtripTime1, - traceRouteReply.PingReplies[2].RoundtripTime.ToString(), + traceRouteReply.Replies[2].RoundtripTime.ToString(), routerName, routerAddress); } else @@ -873,65 +881,70 @@ public void Dispose() /// /// The class contains an information about a trace route attempt. /// - public class TraceRouteReply + public class TraceStatus { - internal TraceRouteReply() + internal TraceStatus( + int hop, + IList replies, + string source, + string destination, + IPAddress destinationAddress) { - PingReplies = new List(DefaultTraceRoutePingCount); + Hop = hop; + Replies = (PingStatus[])replies; + Source = source; + Destination = destination; + DestinationAddress = destinationAddress; + + Latency = new long[replies.Count]; + for (int index = 0; index < replies.Count; index++) + { + Latency[index] = replies[index].Latency; + } } /// /// Number of current hop (router). /// - public int Hop; + public int Hop { get; } /// - /// List of ping replies for current hop (router). + /// The source address of the trace route command. /// - public List PingReplies; + /// + public string Source { get; } /// - /// Router IP address. + /// The latency values of each ping to the current hop point. /// - public IPAddress ReplyRouterAddress; + public long[] Latency { get; } /// - /// Resolved router name. + /// List of ping replies from the current hop point. /// - public string ReplyRouterName; - } - - /// - /// The class contains an information about the source, the destination and trace route results. - /// - public class TraceRouteResult - { - internal TraceRouteResult(string source, IPAddress destinationAddress, string destinationHost) - { - Source = source; - DestinationAddress = destinationAddress; - DestinationHost = destinationHost; - Replies = new List(); - } + public PingStatus[] Replies { get; } /// - /// Source from which to trace route. + /// The hostname of the current hop point. /// - public string Source { get; } + /// + public string HopName { get => Replies[0].Destination; } /// - /// Destination to which to trace route. + /// The IP address of the current hop point. /// - public IPAddress DestinationAddress { get; } + public IPAddress HopAddress { get => Replies[0].Address; } /// - /// Destination to which to trace route. + /// The final destination hostname of the trace. /// - public string DestinationHost { get; } + /// + public string Destination { get; } /// + /// The final destination IP address of the trace. /// - public List Replies { get; } + public IPAddress DestinationAddress { get; } } /// @@ -942,11 +955,25 @@ public class PingStatus internal PingStatus(string source, string destination, uint mtuSize, PingReply reply) : this(source, destination, reply) => MtuSize = mtuSize; + internal PingStatus( + string source, + string destination, + PingReply reply, + PingOptions options, + long latency, + int bufferSize) + : this(source, destination, reply) + { + _options = options; + _latency = latency; + _bufferSize = bufferSize; + } + internal PingStatus(string source, string destination, PingReply reply) { _reply = reply; Source = source; - Destination = destination; + Destination = destination ?? reply.Address.ToString(); } private readonly PingReply _reply; @@ -967,23 +994,25 @@ internal PingStatus(string source, string destination, PingReply reply) /// public string Destination { get; } + private readonly long _latency = -1; /// /// The roundtrip time of the ping in milliseconds. /// /// - public long Latency { get => _reply.RoundtripTime; } + public long Latency { get => _latency >= 0 ? _latency : _reply.RoundtripTime; } + private readonly int _bufferSize = -1; /// /// The size in bytes of the buffer data sent in the ping. /// - /// - public int BufferSize { get => _reply.Buffer.Length; } + public int BufferSize { get => _bufferSize >= 0 ? _bufferSize : _reply.Buffer.Length; } + private readonly PingOptions _options; /// /// The options used when sending the ping. /// /// - public PingOptions Options { get => _reply.Options; } + public PingOptions Options { get => _options ?? _reply.Options; } /// /// The maximum transmission unit size on the network path between the source and destination. From 86c556658e37aa52ed826982f652286ca0ba8a44 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Wed, 19 Jun 2019 21:19:05 -0400 Subject: [PATCH 07/63] :construction: Fiddling with information displays. --- .../management/TestConnectionCommand.cs | 63 ++++++++++--------- 1 file changed, 33 insertions(+), 30 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 353669ff55a..9bc93e8737d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -327,7 +327,7 @@ private void ProcessTraceroute(string targetNameOrAddress) return; } - //WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); + WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); int currentHop = 1; var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); @@ -357,13 +357,20 @@ private void ProcessTraceroute(string targetNameOrAddress) hostname = Dns.GetHostEntry(reply.Address).HostName; } - replies.Add(new PingStatus( - Source, - hostname, - reply, - pingOptions, - timer.ElapsedMilliseconds, - buffer.Length)); + if (currentHop == sMaxHops) + { + replies.Add(new PingStatus(Source, hostname, reply)); + } + else + { + replies.Add(new PingStatus( + Source, + hostname, + reply, + pingOptions, + timer.ElapsedMilliseconds, + buffer.Length)); + } timer.Reset(); } catch (PingException ex) @@ -393,18 +400,19 @@ private void ProcessTraceroute(string targetNameOrAddress) if (!Quiet.IsPresent) { - WriteObject(new TraceStatus(currentHop, replies, Source, targetNameOrAddress, targetAddress)); + var traceStatus = new TraceStatus(currentHop, replies, Source, targetNameOrAddress, targetAddress); + WriteObject(traceStatus); + WriteTraceRouteProgress(traceStatus); } currentHop++; replies.Clear(); - //WriteTraceRouteProgress(traceRouteReply); } while (reply != null && currentHop <= sMaxHops && (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)); - //WriteTraceRouteFooter(); + WriteTraceRouteFooter(); if (Quiet.IsPresent) { @@ -414,46 +422,41 @@ private void ProcessTraceroute(string targetNameOrAddress) private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targetAddress) { - _testConnectionProgressBarActivity = StringUtil.Format( + WriteVerbose(StringUtil.Format( TestConnectionResources.TraceRouteStart, resolvedTargetName, targetAddress, - MaxHops); - - WriteInformation(_testConnectionProgressBarActivity, s_PSHostTag); - - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - WriteProgress(record); + MaxHops)); } private string _testConnectionProgressBarActivity; private static readonly string[] s_PSHostTag = new string[] { "PSHOST" }; - private void WriteTraceRouteProgress(TraceStatus traceRouteReply) + private void WriteTraceRouteProgress(TraceStatus traceStatus) { string msg; - if (traceRouteReply.Replies[2].Status == IPStatus.TtlExpired - || traceRouteReply.Replies[2].Status == IPStatus.Success) + if (traceStatus.Replies[2].Status == IPStatus.TtlExpired + || traceStatus.Replies[2].Status == IPStatus.Success) { - var routerAddress = traceRouteReply.ReplyRouterAddress.ToString(); - var routerName = traceRouteReply.ReplyRouterName ?? routerAddress; - var roundtripTime0 = traceRouteReply.Replies[0].Status == IPStatus.TimedOut + var routerAddress = traceStatus.ReplyRouterAddress.ToString(); + var routerName = traceStatus.ReplyRouterName ?? routerAddress; + var roundtripTime0 = traceStatus.Replies[0].Status == IPStatus.TimedOut ? "*" - : traceRouteReply.Replies[0].RoundtripTime.ToString(); - var roundtripTime1 = traceRouteReply.Replies[1].Status == IPStatus.TimedOut + : traceStatus.Replies[0].RoundtripTime.ToString(); + var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut ? "*" - : traceRouteReply.Replies[1].RoundtripTime.ToString(); + : traceStatus.Replies[1].RoundtripTime.ToString(); msg = StringUtil.Format( TestConnectionResources.TraceRouteReply, - traceRouteReply.Hop, + traceStatus.Hop, roundtripTime0, roundtripTime1, - traceRouteReply.Replies[2].RoundtripTime.ToString(), + traceStatus.Replies[2].RoundtripTime.ToString(), routerName, routerAddress); } else { - msg = StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceRouteReply.Hop); + msg = StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceStatus.Hop); } WriteInformation(msg, s_PSHostTag); From 04c57cdccbb3b69501d7eae6c4741dc80f04ee74 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Wed, 19 Jun 2019 21:19:27 -0400 Subject: [PATCH 08/63] :construction: Add Status property to PingStatus Kinda in the name aight? --- .../commands/management/TestConnectionCommand.cs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 9bc93e8737d..7b5c7a256af 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -981,6 +981,11 @@ internal PingStatus(string source, string destination, PingReply reply) private readonly PingReply _reply; + /// + /// The returned status of the ping. + /// + public IPStatus Status { get => _reply.Status; } + /// /// Source from which to ping. /// @@ -993,7 +998,7 @@ internal PingStatus(string source, string destination, PingReply reply) public IPAddress Address { get => _reply.Address; } /// - /// Destination to which to ping. + /// Destination which was pinged. /// public string Destination { get; } From 23aba08778a8945d5eee193b5b3c5374a341f7fe Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Wed, 19 Jun 2019 22:16:58 -0400 Subject: [PATCH 09/63] :sparkles: tidy up traceroutes --- .../management/TestConnectionCommand.cs | 85 ++++++++----------- 1 file changed, 34 insertions(+), 51 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 7b5c7a256af..567e206a9a7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -327,13 +327,19 @@ private void ProcessTraceroute(string targetNameOrAddress) return; } - WriteConsoleTraceRouteHeader(resolvedTargetName, targetAddress.ToString()); + WriteVerbose(StringUtil.Format( + TestConnectionResources.TraceRouteStart, + resolvedTargetName, + targetAddress, + MaxHops)); int currentHop = 1; - var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); - var replies = new List(DefaultTraceRoutePingCount); int timeout = TimeoutSeconds * 1000; string hostname = null; + + var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); + var replies = new List(DefaultTraceRoutePingCount); + var timer = new Stopwatch(); PingReply reply; @@ -352,25 +358,19 @@ private void ProcessTraceroute(string targetNameOrAddress) reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); timer.Stop(); - if (ResolveDestination && reply.Status == IPStatus.Success) + if (ResolveDestination + && (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired)) { hostname = Dns.GetHostEntry(reply.Address).HostName; } - if (currentHop == sMaxHops) - { - replies.Add(new PingStatus(Source, hostname, reply)); - } - else - { - replies.Add(new PingStatus( - Source, - hostname, - reply, - pingOptions, - timer.ElapsedMilliseconds, - buffer.Length)); - } + replies.Add(new PingStatus( + Source, + hostname, + reply, + pingOptions, + timer.ElapsedMilliseconds, + buffer.Length)); timer.Reset(); } catch (PingException ex) @@ -409,26 +409,17 @@ private void ProcessTraceroute(string targetNameOrAddress) replies.Clear(); } while (reply != null - && currentHop <= sMaxHops + && currentHop <= MaxHops && (reply.Status == IPStatus.TtlExpired || reply.Status == IPStatus.TimedOut)); - WriteTraceRouteFooter(); + WriteVerbose(TestConnectionResources.TraceRouteComplete); if (Quiet.IsPresent) { - WriteObject(currentHop <= sMaxHops); + WriteObject(currentHop <= MaxHops); } } - private void WriteConsoleTraceRouteHeader(string resolvedTargetName, string targetAddress) - { - WriteVerbose(StringUtil.Format( - TestConnectionResources.TraceRouteStart, - resolvedTargetName, - targetAddress, - MaxHops)); - } - private string _testConnectionProgressBarActivity; private static readonly string[] s_PSHostTag = new string[] { "PSHOST" }; @@ -438,20 +429,20 @@ private void WriteTraceRouteProgress(TraceStatus traceStatus) if (traceStatus.Replies[2].Status == IPStatus.TtlExpired || traceStatus.Replies[2].Status == IPStatus.Success) { - var routerAddress = traceStatus.ReplyRouterAddress.ToString(); - var routerName = traceStatus.ReplyRouterName ?? routerAddress; + var routerAddress = traceStatus.HopAddress.ToString(); + var routerName = traceStatus.HopName; var roundtripTime0 = traceStatus.Replies[0].Status == IPStatus.TimedOut ? "*" - : traceStatus.Replies[0].RoundtripTime.ToString(); + : traceStatus.Replies[0].Latency.ToString(); var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut ? "*" - : traceStatus.Replies[1].RoundtripTime.ToString(); + : traceStatus.Replies[1].Latency.ToString(); msg = StringUtil.Format( TestConnectionResources.TraceRouteReply, traceStatus.Hop, roundtripTime0, roundtripTime1, - traceStatus.Replies[2].RoundtripTime.ToString(), + traceStatus.Replies[2].Latency.ToString(), routerName, routerAddress); } else @@ -459,21 +450,7 @@ private void WriteTraceRouteProgress(TraceStatus traceStatus) msg = StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceStatus.Hop); } - WriteInformation(msg, s_PSHostTag); - - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); - WriteProgress(record); - } - - private void WriteTraceRouteFooter() - { - WriteInformation(TestConnectionResources.TraceRouteComplete, s_PSHostTag); - - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) - { - RecordType = ProgressRecordType.Completed - }; - WriteProgress(record); + WriteVerbose(msg); } #endregion TracerouteTest @@ -984,7 +961,13 @@ internal PingStatus(string source, string destination, PingReply reply) /// /// The returned status of the ping. /// - public IPStatus Status { get => _reply.Status; } + public IPStatus Status + { + get => Options.Ttl < 128 && _reply.Status == IPStatus.TtlExpired + // Treat "TtlExpired" as "Success" when this is a traceroute hop as that is the expected outcome. + ? IPStatus.Success + : _reply.Status; + } /// /// Source from which to ping. From 712afbe359589e9f65326238df6fc8aee2a3c8cf Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Wed, 19 Jun 2019 23:32:43 -0400 Subject: [PATCH 10/63] :construction: Flesh out PingStatus class --- .../management/TestConnectionCommand.cs | 82 +++++++++++++------ 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 567e206a9a7..c58460302a0 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -370,7 +370,8 @@ private void ProcessTraceroute(string targetNameOrAddress) reply, pingOptions, timer.ElapsedMilliseconds, - buffer.Length)); + buffer.Length, + isTraceHop: true)); timer.Reset(); } catch (PingException ex) @@ -932,9 +933,29 @@ internal TraceStatus( /// public class PingStatus { - internal PingStatus(string source, string destination, uint mtuSize, PingReply reply) - : this(source, destination, reply) => MtuSize = mtuSize; + internal PingStatus(string source, string destination, int mtuSize, PingReply reply) + : this(source, destination, reply) + { + if (mtuSize <= 0) + { + throw new PSArgumentException(nameof(mtuSize)); + } + + MtuSize = mtuSize; + } + /// + /// Creates a new instance of the PingStatus class. + /// This constructor allows manually specifying the initial values for the cases where the PingReply + /// object may be missing some information, specifically in the instances where PingReply objects are + /// utilised to perform a traceroute. + /// + /// The source machine name or IP of the ping. + /// The destination machine name of the ping. + /// The response from the ping attempt. + /// The PingOptions specified when the ping was sent. + /// The manually measured latency of the ping attempt. + /// The buffer size internal PingStatus( string source, string destination, @@ -949,67 +970,76 @@ internal PingStatus( _bufferSize = bufferSize; } + /// + /// Creates a new instance of the PingStatus class. + /// + /// The source machine name or IP of the ping. + /// The destination machine name of the ping. + /// The response from the ping attempt. internal PingStatus(string source, string destination, PingReply reply) { - _reply = reply; + Reply = reply; Source = source; Destination = destination ?? reply.Address.ToString(); } - private readonly PingReply _reply; + // These values should only be set if this PingStatus was created as part of a traceroute. + private readonly int _bufferSize = -1; + private readonly long _latency = -1; + private readonly PingOptions _options; + + /// + /// Gets the reply object from this ping. + /// + public PingReply Reply { get; } /// - /// The returned status of the ping. + /// Gets the returned status of the ping. /// public IPStatus Status { - get => Options.Ttl < 128 && _reply.Status == IPStatus.TtlExpired - // Treat "TtlExpired" as "Success" when this is a traceroute hop as that is the expected outcome. + // Treat "TtlExpired" as "Success" when this is a traceroute hop, as that is the expected outcome. + get => _options != null && Reply.Status == IPStatus.TtlExpired ? IPStatus.Success - : _reply.Status; + : Reply.Status; } /// - /// Source from which to ping. + /// Gets the source from which the ping was sent. /// public string Source { get; } /// - /// The target address of the ping. + /// Gets the target address of the ping. /// /// - public IPAddress Address { get => _reply.Address; } + public IPAddress Address { get => Reply.Address; } /// - /// Destination which was pinged. + /// Gets the destination which was pinged. /// public string Destination { get; } - private readonly long _latency = -1; /// - /// The roundtrip time of the ping in milliseconds. + /// Gets the roundtrip time of the ping in milliseconds. /// /// - public long Latency { get => _latency >= 0 ? _latency : _reply.RoundtripTime; } + public long Latency { get => _latency >= 0 ? _latency : Reply.RoundtripTime; } - private readonly int _bufferSize = -1; /// - /// The size in bytes of the buffer data sent in the ping. + /// Gets the size in bytes of the buffer data sent in the ping. /// - public int BufferSize { get => _bufferSize >= 0 ? _bufferSize : _reply.Buffer.Length; } + public int BufferSize { get => _bufferSize >= 0 ? _bufferSize : Reply.Buffer.Length; } - private readonly PingOptions _options; /// - /// The options used when sending the ping. + /// Gets the options used when sending the ping. /// - /// - public PingOptions Options { get => _options ?? _reply.Options; } + public PingOptions Options { get => _options ?? Reply.Options; } /// - /// The maximum transmission unit size on the network path between the source and destination. + /// Gets the maximum transmission unit size on the network path between the source and destination. /// - /// - public uint? MtuSize { get; } + public int MtuSize { get; } = -1; } // Count of pings sent per each trace route hop. From 02821e12e4ba84c476d0e864d43b9b8b15251393 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:00:28 -0400 Subject: [PATCH 11/63] :construction: Update MtuSize and Ping methods --- .../management/TestConnectionCommand.cs | 123 ++++-------------- 1 file changed, 24 insertions(+), 99 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index c58460302a0..a64fc800bac 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -366,12 +366,11 @@ private void ProcessTraceroute(string targetNameOrAddress) replies.Add(new PingStatus( Source, - hostname, + hostname ?? resolvedTargetName, reply, pingOptions, timer.ElapsedMilliseconds, - buffer.Length, - isTraceHop: true)); + buffer.Length)); timer.Reset(); } catch (PingException ex) @@ -461,14 +460,12 @@ private void ProcessMTUSize(string targetNameOrAddress) { PingReply reply, replyResult = null; - if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out _, out IPAddress targetAddress)) { return; } - WriteMTUSizeHeader(resolvedTargetName, targetAddress.ToString()); - - // Cautious! Algorithm is sensitive to changing boundary values. + // Caution! Algorithm is sensitive to changing boundary values. int HighMTUSize = 10000; int CurrentMTUSize = 1473; int LowMTUSize = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 1280 : 68; @@ -483,7 +480,6 @@ private void ProcessMTUSize(string targetNameOrAddress) { byte[] buffer = GetSendBuffer(CurrentMTUSize); - WriteMTUSizeProgress(CurrentMTUSize, retry); WriteDebug(StringUtil.Format( "LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2}", LowMTUSize, @@ -548,57 +544,16 @@ private void ProcessMTUSize(string targetNameOrAddress) return; } - WriteMTUSizeFooter(); - if (Quiet.IsPresent) { WriteObject(CurrentMTUSize); } else { - var res = PSObject.AsPSObject(replyResult); - - PSMemberInfo sourceProperty = new PSNoteProperty("Source", Source); - res.Members.Add(sourceProperty); - PSMemberInfo destinationProperty = new PSNoteProperty("Destination", targetNameOrAddress); - res.Members.Add(destinationProperty); - PSMemberInfo mtuSizeProperty = new PSNoteProperty("MTUSize", CurrentMTUSize); - res.Members.Add(mtuSizeProperty); - res.TypeNames.Insert(0, "PingReply#MTUSize"); - - WriteObject(res); + WriteObject(new PingStatus(Source, targetNameOrAddress, CurrentMTUSize, replyResult)); } } - private void WriteMTUSizeHeader(string resolvedTargetName, string targetAddress) - { - _testConnectionProgressBarActivity = StringUtil.Format( - TestConnectionResources.MTUSizeDetectStart, - resolvedTargetName, - targetAddress, - BufferSize); - - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - WriteProgress(record); - } - - private void WriteMTUSizeProgress(int currentMTUSize, int retry) - { - var msg = StringUtil.Format(TestConnectionResources.MTUSizeDetectDescription, currentMTUSize, retry); - - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); - WriteProgress(record); - } - - private void WriteMTUSizeFooter() - { - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) - { - RecordType = ProgressRecordType.Completed - }; - WriteProgress(record); - } - #endregion MTUSizeTest #region PingTest @@ -612,7 +567,11 @@ private void ProcessPing(string targetNameOrAddress) if (!Continues.IsPresent) { - WritePingHeader(resolvedTargetName, targetAddress.ToString()); + WriteVerbose(StringUtil.Format( + TestConnectionResources.MTUSizeDetectStart, + resolvedTargetName, + targetAddress, + BufferSize)); } bool quietResult = true; @@ -620,7 +579,6 @@ private void ProcessPing(string targetNameOrAddress) PingReply reply; var pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); - var pingReport = new PingStatus(Source, resolvedTargetName); int timeout = TimeoutSeconds * 1000; int delay = Delay * 1000; @@ -661,7 +619,7 @@ private void ProcessPing(string targetNameOrAddress) } else { - pingReport.Replies.Add(reply); + WriteObject(new PingStatus(Source, resolvedTargetName, reply)); } WritePingProgress(reply); @@ -676,71 +634,30 @@ private void ProcessPing(string targetNameOrAddress) if (!Continues.IsPresent) { - WritePingFooter(); + WriteVerbose(TestConnectionResources.PingComplete); } if (Quiet.IsPresent) { WriteObject(quietResult); } - else - { - WriteObject(pingReport); - } - } - - private void WritePingHeader(string resolvedTargetName, string targetAddress) - { - _testConnectionProgressBarActivity = StringUtil.Format( - TestConnectionResources.MTUSizeDetectStart, - resolvedTargetName, - targetAddress, - BufferSize); - - WriteInformation(_testConnectionProgressBarActivity, s_PSHostTag); - - var record = new ProgressRecord( - s_ProgressId, - _testConnectionProgressBarActivity, - ProgressRecordSpace); - WriteProgress(record); } private void WritePingProgress(PingReply reply) { - string msg; if (reply.Status != IPStatus.Success) { - msg = TestConnectionResources.PingTimeOut; + WriteVerbose(TestConnectionResources.PingTimeOut); } else { - msg = StringUtil.Format( + WriteVerbose(StringUtil.Format( TestConnectionResources.PingReply, reply.Address.ToString(), reply.Buffer.Length, reply.RoundtripTime, - reply.Options?.Ttl); + reply.Options?.Ttl)); } - - WriteInformation(msg, s_PSHostTag); - - ProgressRecord record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); - WriteProgress(record); - } - - private void WritePingFooter() - { - WriteInformation(TestConnectionResources.PingComplete, s_PSHostTag); - - ProgressRecord record = new ProgressRecord( - s_ProgressId, - _testConnectionProgressBarActivity, - ProgressRecordSpace) - { - RecordType = ProgressRecordType.Completed - }; - WriteProgress(record); } #endregion PingTest @@ -789,7 +706,7 @@ private bool InitProcessPing( return false; } - if (IPv6 || IPv4) + if (IPv6.IsPresent || IPv4.IsPresent) { var addressFamily = IPv6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork; @@ -933,6 +850,14 @@ internal TraceStatus( /// public class PingStatus { + /// + /// Creates a new instance of the PingStatus class. + /// This constructor permits setting the MtuSize. + /// + /// The source machine name or IP of the ping. + /// The destination machine name of the ping. + /// The maximum transmission unit size determined. + /// The response from the ping attempt. internal PingStatus(string source, string destination, int mtuSize, PingReply reply) : this(source, destination, reply) { From cab8764ad4d0910b0c44a4c63e3cfda5813a4649 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:00:47 -0400 Subject: [PATCH 12/63] :recycle: remove unused PSHost tag --- .../commands/management/TestConnectionCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index a64fc800bac..2f1550ced37 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -421,7 +421,6 @@ private void ProcessTraceroute(string targetNameOrAddress) } private string _testConnectionProgressBarActivity; - private static readonly string[] s_PSHostTag = new string[] { "PSHOST" }; private void WriteTraceRouteProgress(TraceStatus traceStatus) { From 7897be53f5398a0031c7e73385207e2a38ea7266 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:12:14 -0400 Subject: [PATCH 13/63] :memo: Update xml doc tags for TraceStatus --- .../management/TestConnectionCommand.cs | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 2f1550ced37..242b5854d26 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -780,6 +780,14 @@ public void Dispose() /// public class TraceStatus { + /// + /// Creates a new instance of the TraceStatus class. + /// + /// The hop number of this trace hop. + /// The PingStatus replies received from this trace hop. + /// The source computer name or IP address of the traceroute. + /// The target destination of the traceroute. + /// The target IPAddress of the overall traceroute. internal TraceStatus( int hop, IList replies, @@ -801,45 +809,43 @@ internal TraceStatus( } /// - /// Number of current hop (router). + /// Gets the number of the current hop / router. /// public int Hop { get; } /// - /// The source address of the trace route command. + /// Gets the source address of the traceroute command. /// - /// public string Source { get; } /// - /// The latency values of each ping to the current hop point. + /// Gets the latency values of each ping to the current hop point. /// public long[] Latency { get; } /// - /// List of ping replies from the current hop point. + /// Gets the ping replies from the current hop point. /// public PingStatus[] Replies { get; } /// - /// The hostname of the current hop point. + /// Gets the hostname of the current hop point. /// /// public string HopName { get => Replies[0].Destination; } /// - /// The IP address of the current hop point. + /// Gets the IP address of the current hop point. /// public IPAddress HopAddress { get => Replies[0].Address; } /// - /// The final destination hostname of the trace. + /// Gets the final destination hostname of the trace. /// - /// public string Destination { get; } /// - /// The final destination IP address of the trace. + /// Gets the final destination IP address of the trace. /// public IPAddress DestinationAddress { get; } } From 30ca329f85848e5c1ceed2b4f0da00188ef183b8 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:21:00 -0400 Subject: [PATCH 14/63] :construction: Simplify param sets & clean --- .../management/TestConnectionCommand.cs | 133 +++++------------- 1 file changed, 38 insertions(+), 95 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 242b5854d26..43264cf94ee 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -17,17 +17,15 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "Test-Connection" cmdlet. /// - [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPingCount, + [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPing, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] - [OutputType(typeof(PingStatus), ParameterSetName = new[] { ParameterSetPingCount })] - [OutputType(typeof(PingReply), ParameterSetName = new[] { ParameterSetPingContinues, ParameterSetDetectionOfMTUSize })] - [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPingCount, ParameterSetPingContinues, ParameterSetConnectionByTCPPort })] - [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] + [OutputType(typeof(PingStatus), ParameterSetName = new[] { ParameterSetPing, ParameterSetDetectionOfMTUSize })] [OutputType(typeof(TraceStatus), ParameterSetName = new[] { ParameterSetTraceRoute })] + [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPing, ParameterSetConnectionByTCPPort })] + [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] public class TestConnectionCommand : PSCmdlet, IDisposable { - private const string ParameterSetPingCount = "PingCount"; - private const string ParameterSetPingContinues = "PingContinues"; + private const string ParameterSetPing = "PingCount"; private const string ParameterSetTraceRoute = "TraceRoute"; private const string ParameterSetConnectionByTCPPort = "ConnectionByTCPPort"; private const string ParameterSetDetectionOfMTUSize = "DetectionOfMTUSize"; @@ -38,15 +36,13 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Do ping test. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] public SwitchParameter Ping { get; set; } = true; /// /// Force using IPv4 protocol. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -55,8 +51,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Force using IPv6 protocol. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -65,8 +60,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Do reverse DNS lookup to get names for IP addresses. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -77,8 +71,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default is Local Host. /// Remoting is not yet implemented internally in the cmdlet. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] public string Source { get; } = Dns.GetHostName(); @@ -89,8 +82,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// they decrement the Time-to-Live (TTL) value found in the packet header. /// The default (from Windows) is 128 hops. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [ValidateRange(0, sMaxHops)] [Alias("Ttl", "TimeToLive", "Hops")] @@ -100,7 +92,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Count of attempts. /// The default (from Windows) is 4 times. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] + [Parameter(ParameterSetName = ParameterSetPing)] [ValidateRange(ValidateRangeKind.Positive)] public int Count { get; set; } = 4; @@ -108,8 +100,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Delay between attempts. /// The default (from Windows) is 1 second. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [ValidateRange(ValidateRangeKind.Positive)] public int Delay { get; set; } = 1; @@ -118,8 +109,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default (from Windows) is 32 bites. /// Max value is 65500 (limit from Windows API). /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] [Alias("Size", "Bytes", "BS")] [ValidateRange(0, 65500)] public int BufferSize { get; set; } = DefaultSendBufferSize; @@ -128,15 +118,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Don't fragment ICMP packages. /// Currently CoreFX not supports this on Unix. /// - [Parameter(ParameterSetName = ParameterSetPingCount)] - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] public SwitchParameter DontFragment { get; set; } /// /// Continue ping until user press Ctrl-C /// or Int.MaxValue threshold reached. /// - [Parameter(ParameterSetName = ParameterSetPingContinues)] + [Parameter(ParameterSetName = ParameterSetPing)] public SwitchParameter Continues { get; set; } /// @@ -198,11 +187,9 @@ protected override void BeginProcessing() { base.BeginProcessing(); - switch (ParameterSetName) + if (ParameterSetName == ParameterSetPing && Continues.IsPresent) { - case ParameterSetPingContinues: - Count = int.MaxValue; - break; + Count = int.MaxValue; } } @@ -215,8 +202,7 @@ protected override void ProcessRecord() { switch (ParameterSetName) { - case ParameterSetPingCount: - case ParameterSetPingContinues: + case ParameterSetPing: ProcessPing(targetName); break; case ParameterSetDetectionOfMTUSize: @@ -237,13 +223,11 @@ protected override void ProcessRecord() private void ProcessConnectionByTCPPort(string targetNameOrAddress) { - if (!InitProcessPing(targetNameOrAddress, out string resolvedTargetName, out IPAddress targetAddress)) + if (!InitProcessPing(targetNameOrAddress, out _, out IPAddress targetAddress)) { return; } - WriteConnectionTestHeader(resolvedTargetName, targetAddress.ToString()); - var client = new TcpClient(); try @@ -253,8 +237,6 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) for (var i = 1; i <= TimeoutSeconds; i++) { - WriteConnectionTestProgress(targetNameOrAddress, targetString, i); - Task timeoutTask = Task.Delay(millisecondsDelay: 1000); Task.WhenAny(connectionTask, timeoutTask).Result.Wait(); @@ -279,42 +261,11 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) finally { client.Close(); - WriteConnectionTestFooter(); } WriteObject(false); } - private void WriteConnectionTestHeader(string resolvedTargetName, string targetAddress) - { - _testConnectionProgressBarActivity = StringUtil.Format( - TestConnectionResources.ConnectionTestStart, - resolvedTargetName, - targetAddress); - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace); - WriteProgress(record); - } - - private void WriteConnectionTestProgress(string targetNameOrAddress, string targetAddress, int timeout) - { - var msg = StringUtil.Format( - TestConnectionResources.ConnectionTestDescription, - targetNameOrAddress, - targetAddress, - timeout); - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, msg); - WriteProgress(record); - } - - private void WriteConnectionTestFooter() - { - var record = new ProgressRecord(s_ProgressId, _testConnectionProgressBarActivity, ProgressRecordSpace) - { - RecordType = ProgressRecordType.Completed - }; - WriteProgress(record); - } - #endregion ConnectionTest #region TracerouteTest @@ -402,7 +353,7 @@ private void ProcessTraceroute(string targetNameOrAddress) { var traceStatus = new TraceStatus(currentHop, replies, Source, targetNameOrAddress, targetAddress); WriteObject(traceStatus); - WriteTraceRouteProgress(traceStatus); + WriteVerboseTraceEntry(traceStatus); } currentHop++; @@ -422,9 +373,8 @@ private void ProcessTraceroute(string targetNameOrAddress) private string _testConnectionProgressBarActivity; - private void WriteTraceRouteProgress(TraceStatus traceStatus) + private void WriteVerboseTraceEntry(TraceStatus traceStatus) { - string msg; if (traceStatus.Replies[2].Status == IPStatus.TtlExpired || traceStatus.Replies[2].Status == IPStatus.Success) { @@ -436,20 +386,18 @@ private void WriteTraceRouteProgress(TraceStatus traceStatus) var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut ? "*" : traceStatus.Replies[1].Latency.ToString(); - msg = StringUtil.Format( + WriteVerbose(StringUtil.Format( TestConnectionResources.TraceRouteReply, traceStatus.Hop, roundtripTime0, roundtripTime1, traceStatus.Replies[2].Latency.ToString(), - routerName, routerAddress); + routerName, routerAddress)); } else { - msg = StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceStatus.Hop); + WriteVerbose(StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceStatus.Hop)); } - - WriteVerbose(msg); } #endregion TracerouteTest @@ -621,7 +569,19 @@ private void ProcessPing(string targetNameOrAddress) WriteObject(new PingStatus(Source, resolvedTargetName, reply)); } - WritePingProgress(reply); + if (reply.Status != IPStatus.Success) + { + WriteVerbose(TestConnectionResources.PingTimeOut); + } + else + { + WriteVerbose(StringUtil.Format( + TestConnectionResources.PingReply, + reply.Address.ToString(), + reply.Buffer.Length, + reply.RoundtripTime, + reply.Options?.Ttl)); + } } // Delay between ping but not after last ping. @@ -642,23 +602,6 @@ private void ProcessPing(string targetNameOrAddress) } } - private void WritePingProgress(PingReply reply) - { - if (reply.Status != IPStatus.Success) - { - WriteVerbose(TestConnectionResources.PingTimeOut); - } - else - { - WriteVerbose(StringUtil.Format( - TestConnectionResources.PingReply, - reply.Address.ToString(), - reply.Buffer.Length, - reply.RoundtripTime, - reply.Options?.Ttl)); - } - } - #endregion PingTest private bool InitProcessPing( From 31fbdda56590d1fd4eb517f8b2c9b5d23700591d Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:26:23 -0400 Subject: [PATCH 15/63] :wrench: Fix up parameter sets --- .../management/TestConnectionCommand.cs | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 43264cf94ee..c1c8ad3d407 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -19,13 +19,16 @@ namespace Microsoft.PowerShell.Commands /// [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPing, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] - [OutputType(typeof(PingStatus), ParameterSetName = new[] { ParameterSetPing, ParameterSetDetectionOfMTUSize })] + [OutputType(typeof(PingStatus), + ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetDetectionOfMTUSize })] [OutputType(typeof(TraceStatus), ParameterSetName = new[] { ParameterSetTraceRoute })] - [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPing, ParameterSetConnectionByTCPPort })] + [OutputType(typeof(bool), + ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetConnectionByTCPPort })] [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] public class TestConnectionCommand : PSCmdlet, IDisposable { private const string ParameterSetPing = "PingCount"; + private const string ParameterSetPingContinue = "PingContinue"; private const string ParameterSetTraceRoute = "TraceRoute"; private const string ParameterSetConnectionByTCPPort = "ConnectionByTCPPort"; private const string ParameterSetDetectionOfMTUSize = "DetectionOfMTUSize"; @@ -37,12 +40,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Do ping test. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] public SwitchParameter Ping { get; set; } = true; /// /// Force using IPv4 protocol. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -52,6 +57,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Force using IPv6 protocol. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -61,6 +67,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Do reverse DNS lookup to get names for IP addresses. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] @@ -72,6 +79,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Remoting is not yet implemented internally in the cmdlet. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] public string Source { get; } = Dns.GetHostName(); @@ -83,6 +91,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default (from Windows) is 128 hops. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Parameter(ParameterSetName = ParameterSetTraceRoute)] [ValidateRange(0, sMaxHops)] [Alias("Ttl", "TimeToLive", "Hops")] @@ -101,6 +110,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default (from Windows) is 1 second. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [ValidateRange(ValidateRangeKind.Positive)] public int Delay { get; set; } = 1; @@ -110,6 +120,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Max value is 65500 (limit from Windows API). /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] [Alias("Size", "Bytes", "BS")] [ValidateRange(0, 65500)] public int BufferSize { get; set; } = DefaultSendBufferSize; @@ -119,13 +130,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Currently CoreFX not supports this on Unix. /// [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] public SwitchParameter DontFragment { get; set; } /// /// Continue ping until user press Ctrl-C /// or Int.MaxValue threshold reached. /// - [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = ParameterSetPingContinue)] public SwitchParameter Continues { get; set; } /// @@ -187,7 +199,7 @@ protected override void BeginProcessing() { base.BeginProcessing(); - if (ParameterSetName == ParameterSetPing && Continues.IsPresent) + if (ParameterSetName == ParameterSetPingContinue) { Count = int.MaxValue; } @@ -203,6 +215,7 @@ protected override void ProcessRecord() switch (ParameterSetName) { case ParameterSetPing: + case ParameterSetPingContinue: ProcessPing(targetName); break; case ParameterSetDetectionOfMTUSize: From 3be2450deebef040d58a44619369f77ca9576d7d Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 00:27:42 -0400 Subject: [PATCH 16/63] :construction: Remove unnecessary method call --- .../commands/management/TestConnectionCommand.cs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index c1c8ad3d407..9df5c6fe9ed 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -197,8 +197,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// protected override void BeginProcessing() { - base.BeginProcessing(); - if (ParameterSetName == ParameterSetPingContinue) { Count = int.MaxValue; @@ -235,7 +233,6 @@ protected override void ProcessRecord() private void ProcessConnectionByTCPPort(string targetNameOrAddress) { - if (!InitProcessPing(targetNameOrAddress, out _, out IPAddress targetAddress)) { return; From 44d84f306c1ac7cef9b6af794c240c06aa8c6eee Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 12:56:53 -0400 Subject: [PATCH 17/63] :recycle: Remove unused fields --- .../commands/management/TestConnectionCommand.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 9df5c6fe9ed..d6c66329001 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -381,8 +381,6 @@ private void ProcessTraceroute(string targetNameOrAddress) } } - private string _testConnectionProgressBarActivity; - private void WriteVerboseTraceEntry(TraceStatus traceStatus) { if (traceStatus.Replies[2].Status == IPStatus.TtlExpired @@ -934,12 +932,6 @@ public IPStatus Status private const int DefaultSendBufferSize = 32; private static byte[] s_DefaultSendBuffer = null; - // Random value for WriteProgress Activity Id. - private static readonly int s_ProgressId = 174593053; - - // Empty message string for Progress Bar. - private const string ProgressRecordSpace = " "; - private const string TestConnectionExceptionId = "TestConnectionException"; } } From e06a54452bc56ee8cfd5311c062c9ce0aa178311 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 13:01:01 -0400 Subject: [PATCH 18/63] :wrench: Fix invalid cast --- .../commands/management/TestConnectionCommand.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index d6c66329001..c67acfe941f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -741,13 +741,13 @@ public class TraceStatus /// The target IPAddress of the overall traceroute. internal TraceStatus( int hop, - IList replies, + List replies, string source, string destination, IPAddress destinationAddress) { Hop = hop; - Replies = (PingStatus[])replies; + Replies = replies.ToArray(); Source = source; Destination = destination; DestinationAddress = destinationAddress; From 8e59c05d537a7ada5c2e8ff47015b14bed4765f6 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 22:41:10 -0400 Subject: [PATCH 19/63] :wrench: have hostname take proper value in trace --- .../commands/management/TestConnectionCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index c67acfe941f..f9815b39752 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -327,7 +327,7 @@ private void ProcessTraceroute(string targetNameOrAddress) replies.Add(new PingStatus( Source, - hostname ?? resolvedTargetName, + hostname, reply, pingOptions, timer.ElapsedMilliseconds, From 527c7aacec73ed7a98b459cf191e08b57f053196 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 23:40:55 -0400 Subject: [PATCH 20/63] :sparkles: assess status for trace specifically --- .../management/TestConnectionCommand.cs | 66 +++++++++++++------ 1 file changed, 45 insertions(+), 21 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f9815b39752..1dfe76037da 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -751,14 +751,37 @@ internal TraceStatus( Source = source; Destination = destination; DestinationAddress = destinationAddress; - + Status = IPStatus.Unknown; Latency = new long[replies.Count]; + for (int index = 0; index < replies.Count; index++) { Latency[index] = replies[index].Latency; + + // Only one ping needs to successfully get back a Success or TtlExpired status for this trace hop + // to be considered successful. + if (Status != IPStatus.Success) + { + Status = GetTraceStatus(Status, replies[index].Status); + } } } + private IPStatus GetTraceStatus(IPStatus currentStatus, IPStatus newStatus) + { + if (currentStatus == IPStatus.Success) + { + return currentStatus; + } + + if (newStatus == IPStatus.Success || newStatus == IPStatus.TtlExpired) + { + return IPStatus.Success; + } + + return newStatus; + } + /// /// Gets the number of the current hop / router. /// @@ -799,6 +822,13 @@ internal TraceStatus( /// Gets the final destination IP address of the trace. /// public IPAddress DestinationAddress { get; } + + /// + /// Gets the status of the traceroute hop. + /// It is considered successful if the individual pings report either Success or TtlExpired. + /// + /// + public IPStatus Status { get; } } /// @@ -870,25 +900,14 @@ internal PingStatus(string source, string destination, PingReply reply) private readonly PingOptions _options; /// - /// Gets the reply object from this ping. - /// - public PingReply Reply { get; } - - /// - /// Gets the returned status of the ping. + /// Gets the source from which the ping was sent. /// - public IPStatus Status - { - // Treat "TtlExpired" as "Success" when this is a traceroute hop, as that is the expected outcome. - get => _options != null && Reply.Status == IPStatus.TtlExpired - ? IPStatus.Success - : Reply.Status; - } + public string Source { get; } /// - /// Gets the source from which the ping was sent. + /// Gets the destination which was pinged. /// - public string Source { get; } + public string Destination { get; } /// /// Gets the target address of the ping. @@ -896,22 +915,27 @@ public IPStatus Status /// public IPAddress Address { get => Reply.Address; } - /// - /// Gets the destination which was pinged. - /// - public string Destination { get; } - /// /// Gets the roundtrip time of the ping in milliseconds. /// /// public long Latency { get => _latency >= 0 ? _latency : Reply.RoundtripTime; } + /// + /// Gets the returned status of the ping. + /// + public IPStatus Status { get => Reply.Status; } + /// /// Gets the size in bytes of the buffer data sent in the ping. /// public int BufferSize { get => _bufferSize >= 0 ? _bufferSize : Reply.Buffer.Length; } + /// + /// Gets the reply object from this ping. + /// + public PingReply Reply { get; } + /// /// Gets the options used when sending the ping. /// From 10173065a038b7fc1821a939d84a87e0819fa483 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Thu, 20 Jun 2019 23:42:28 -0400 Subject: [PATCH 21/63] :recycle: Shuffle properties around a bit --- .../commands/management/TestConnectionCommand.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 1dfe76037da..2db119b52e5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -797,11 +797,6 @@ private IPStatus GetTraceStatus(IPStatus currentStatus, IPStatus newStatus) /// public long[] Latency { get; } - /// - /// Gets the ping replies from the current hop point. - /// - public PingStatus[] Replies { get; } - /// /// Gets the hostname of the current hop point. /// @@ -829,6 +824,11 @@ private IPStatus GetTraceStatus(IPStatus currentStatus, IPStatus newStatus) /// /// public IPStatus Status { get; } + + /// + /// Gets the ping replies from the current hop point. + /// + public PingStatus[] Replies { get; } } /// From 1b72fef34f0ce98cec9022fe0065e5a4f5b97475 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 00:24:51 -0400 Subject: [PATCH 22/63] :art: add initial formatview for PingStatus --- .../PowerShellCore_format_ps1xml.cs | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 38d4fe125ce..ca64eb7526b 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -255,6 +255,48 @@ internal static IEnumerable GetFormatData() yield return new ExtendedTypeDefinition( "Microsoft.PowerShell.MarkdownRender.PSMarkdownOptionInfo", ViewsOf_Microsoft_PowerShell_MarkdownRender_MarkdownOptionInfo()); + + yield return new ExtendedTypeDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", + ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus()); + + yield return new ExtendedTypeDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", + ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TraceStatus()); + } + + private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus() + { + yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", + TableControl.Create() + .AddHeader(Alignment.Left, label: "Source") + .AddHeader(Alignment.Left, label: "Destination") + .AddHeader(Alignment.Left, label: "Address") + .AddHeader(Alignment.Right, label: "Latency\n(ms)") + .AddHeader(Alignment.Right, label: "BufferSize\n(B)") + .AddHeader(Alignment.Left, label: "Status") + .AddHeader(Alignment.Right, label: "MtuSize\n(B)") + .StartRowDefinition(wrap: true) + .AddPropertyColumn("Source") + .AddPropertyColumn("Destination") + .AddPropertyColumn("Address") + .AddPropertyColumn("Latency") + .AddPropertyColumn("BufferSize") + .AddPropertyColumn("Status") + .AddPropertyColumn("MtuSize") + .EndRowDefinition() + .EndTable()); + } + + private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TraceStatus() + { + yield return new FormatViewDefinition("TraceStatus", + TableControl.Create() + .AddHeader(Alignment.Left, label: "Timestamp", width: 26) + .StartRowDefinition(wrap: true) + .AddPropertyColumn("Timestamp") + .EndRowDefinition() + .EndTable()); } private static IEnumerable ViewsOf_System_RuntimeType() From ada3228c63946868f432748fe94c6467250f825b Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 00:32:28 -0400 Subject: [PATCH 23/63] :art: Add grouping to PingStatus format --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index ca64eb7526b..60a230970ce 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -285,6 +285,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddPropertyColumn("Status") .AddPropertyColumn("MtuSize") .EndRowDefinition() + .GroupByProperty("Destination") .EndTable()); } From bfe0073a07caa78badbb9b4def19760fcd7644ec Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 00:35:13 -0400 Subject: [PATCH 24/63] :art: remove grouped prop from columns --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 60a230970ce..0ab59d60bee 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -278,7 +278,6 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddHeader(Alignment.Right, label: "MtuSize\n(B)") .StartRowDefinition(wrap: true) .AddPropertyColumn("Source") - .AddPropertyColumn("Destination") .AddPropertyColumn("Address") .AddPropertyColumn("Latency") .AddPropertyColumn("BufferSize") From b66699817e0ef96bcae6c001bc43efebf3ffbd9a Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 00:37:29 -0400 Subject: [PATCH 25/63] :art: Remove stray header --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 0ab59d60bee..738e79e88a1 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -270,7 +270,6 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", TableControl.Create() .AddHeader(Alignment.Left, label: "Source") - .AddHeader(Alignment.Left, label: "Destination") .AddHeader(Alignment.Left, label: "Address") .AddHeader(Alignment.Right, label: "Latency\n(ms)") .AddHeader(Alignment.Right, label: "BufferSize\n(B)") From ee9327245753140eb22ceee496397c40b9ebc49d Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 12:45:16 -0400 Subject: [PATCH 26/63] :recycle: Refactor TraceStatus to singletons --- .../management/TestConnectionCommand.cs | 207 +++++++++--------- 1 file changed, 102 insertions(+), 105 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 2db119b52e5..7b98047d02e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -726,111 +726,6 @@ public void Dispose() _sender?.Dispose(); } - /// - /// The class contains an information about a trace route attempt. - /// - public class TraceStatus - { - /// - /// Creates a new instance of the TraceStatus class. - /// - /// The hop number of this trace hop. - /// The PingStatus replies received from this trace hop. - /// The source computer name or IP address of the traceroute. - /// The target destination of the traceroute. - /// The target IPAddress of the overall traceroute. - internal TraceStatus( - int hop, - List replies, - string source, - string destination, - IPAddress destinationAddress) - { - Hop = hop; - Replies = replies.ToArray(); - Source = source; - Destination = destination; - DestinationAddress = destinationAddress; - Status = IPStatus.Unknown; - Latency = new long[replies.Count]; - - for (int index = 0; index < replies.Count; index++) - { - Latency[index] = replies[index].Latency; - - // Only one ping needs to successfully get back a Success or TtlExpired status for this trace hop - // to be considered successful. - if (Status != IPStatus.Success) - { - Status = GetTraceStatus(Status, replies[index].Status); - } - } - } - - private IPStatus GetTraceStatus(IPStatus currentStatus, IPStatus newStatus) - { - if (currentStatus == IPStatus.Success) - { - return currentStatus; - } - - if (newStatus == IPStatus.Success || newStatus == IPStatus.TtlExpired) - { - return IPStatus.Success; - } - - return newStatus; - } - - /// - /// Gets the number of the current hop / router. - /// - public int Hop { get; } - - /// - /// Gets the source address of the traceroute command. - /// - public string Source { get; } - - /// - /// Gets the latency values of each ping to the current hop point. - /// - public long[] Latency { get; } - - /// - /// Gets the hostname of the current hop point. - /// - /// - public string HopName { get => Replies[0].Destination; } - - /// - /// Gets the IP address of the current hop point. - /// - public IPAddress HopAddress { get => Replies[0].Address; } - - /// - /// Gets the final destination hostname of the trace. - /// - public string Destination { get; } - - /// - /// Gets the final destination IP address of the trace. - /// - public IPAddress DestinationAddress { get; } - - /// - /// Gets the status of the traceroute hop. - /// It is considered successful if the individual pings report either Success or TtlExpired. - /// - /// - public IPStatus Status { get; } - - /// - /// Gets the ping replies from the current hop point. - /// - public PingStatus[] Replies { get; } - } - /// /// The class contains information about the source, the destination and ping results. /// @@ -940,6 +835,108 @@ internal PingStatus(string source, string destination, PingReply reply) /// Gets the options used when sending the ping. /// public PingOptions Options { get => _options ?? Reply.Options; } + } + + /// + /// The class contains an information about a trace route attempt. + /// + public class TraceStatus + { + /// + /// Creates a new instance of the TraceStatus class. + /// + /// The hop number of this trace hop. + /// The PingStatus response from this trace hop. + /// The source computer name or IP address of the traceroute. + /// The target destination of the traceroute. + /// The target IPAddress of the overall traceroute. + internal TraceStatus( + int hop, + PingStatus status, + string source, + string destination, + IPAddress destinationAddress) + { + Hop = hop; + Status = status; + Source = source; + Destination = destination; + DestinationAddress = destinationAddress; + } + + private readonly PingStatus _status; + + /// + /// Gets the number of the current hop / router. + /// + public int Hop { get; } + + /// + /// Gets the source address of the traceroute command. + /// + public string Source { get; } + + /// + /// Gets the latency values of each ping to the current hop point. + /// + public long Latency { get => _status.Latency; } + + /// + /// Gets the hostname of the current hop point. + /// + /// + public string HopName { get => _status.Destination; } + + /// + /// Gets the IP address of the current hop point. + /// + public IPAddress HopAddress { get => _status.Address; } + + /// + /// Gets the final destination hostname of the trace. + /// + public string Destination { get; } + + /// + /// Gets the final destination IP address of the trace. + /// + public IPAddress DestinationAddress { get; } + + /// + /// Gets the status of the traceroute hop. + /// It is considered successful if the individual pings report either Success or TtlExpired. + /// + public IPStatus Status { get => _status.Status; } + + /// + /// Gets the raw PingReply object received from the ping to this hop point. + /// + public PingReply Reply { get => _status.Reply; } + } + + /// + /// The class contains information about the source, the destination and ping results. + /// + public class PingMtuStatus : PingStatus + { + /// + /// Creates a new instance of the PingStatus class. + /// This constructor permits setting the MtuSize. + /// + /// The source machine name or IP of the ping. + /// The destination machine name of the ping. + /// The maximum transmission unit size determined. + /// The response from the ping attempt. + internal PingMtuStatus(string source, string destination, int mtuSize, PingReply reply) + : base(source, destination, reply) + { + if (mtuSize <= 0) + { + throw new PSArgumentException(nameof(mtuSize)); + } + + MtuSize = mtuSize; + } /// /// Gets the maximum transmission unit size on the network path between the source and destination. From 307cb699dd9e32753359787380d18f45ae2cea0b Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 12:45:31 -0400 Subject: [PATCH 27/63] :art: Add PingMtuSize formatview --- .../PowerShellCore_format_ps1xml.cs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 738e79e88a1..42e6bce5572 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -260,6 +260,10 @@ internal static IEnumerable GetFormatData() "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus()); + yield return new ExtendedTypeDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus", + ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingMtuStatus()); + yield return new ExtendedTypeDefinition( "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TraceStatus()); @@ -268,6 +272,26 @@ internal static IEnumerable GetFormatData() private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus() { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", + TableControl.Create() + .AddHeader(Alignment.Left, label: "Source") + .AddHeader(Alignment.Left, label: "Address") + .AddHeader(Alignment.Right, label: "Latency\n(ms)") + .AddHeader(Alignment.Right, label: "BufferSize\n(B)") + .AddHeader(Alignment.Left, label: "Status") + .StartRowDefinition(wrap: true) + .AddPropertyColumn("Source") + .AddPropertyColumn("Address") + .AddPropertyColumn("Latency") + .AddPropertyColumn("BufferSize") + .AddPropertyColumn("Status") + .EndRowDefinition() + .GroupByProperty("Destination") + .EndTable()); + } + + private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingMtuStatus() + { + yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus", TableControl.Create() .AddHeader(Alignment.Left, label: "Source") .AddHeader(Alignment.Left, label: "Address") From e1dbbd9d48a2f54d286d41de2dd4638ff8005007 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 13:07:23 -0400 Subject: [PATCH 28/63] :art: :recycle: Update TraceStatus formatter --- .../management/TestConnectionCommand.cs | 26 +++++++++---------- .../PowerShellCore_format_ps1xml.cs | 15 ++++++++--- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 7b98047d02e..1f00749c4ae 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -872,9 +872,15 @@ internal TraceStatus( public int Hop { get; } /// - /// Gets the source address of the traceroute command. + /// Gets the hostname of the current hop point. /// - public string Source { get; } + /// + public string HopName { get => _status.Destination; } + + /// + /// Gets the IP address of the current hop point. + /// + public IPAddress HopAddress { get => _status.Address; } /// /// Gets the latency values of each ping to the current hop point. @@ -882,15 +888,15 @@ internal TraceStatus( public long Latency { get => _status.Latency; } /// - /// Gets the hostname of the current hop point. + /// Gets the status of the traceroute hop. + /// It is considered successful if the individual pings report either Success or TtlExpired. /// - /// - public string HopName { get => _status.Destination; } + public IPStatus Status { get => _status.Status; } /// - /// Gets the IP address of the current hop point. + /// Gets the source address of the traceroute command. /// - public IPAddress HopAddress { get => _status.Address; } + public string Source { get; } /// /// Gets the final destination hostname of the trace. @@ -902,12 +908,6 @@ internal TraceStatus( /// public IPAddress DestinationAddress { get; } - /// - /// Gets the status of the traceroute hop. - /// It is considered successful if the individual pings report either Success or TtlExpired. - /// - public IPStatus Status { get => _status.Status; } - /// /// Gets the raw PingReply object received from the ping to this hop point. /// diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 42e6bce5572..9e562710728 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -313,12 +313,21 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TraceStatus() { - yield return new FormatViewDefinition("TraceStatus", + yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", TableControl.Create() - .AddHeader(Alignment.Left, label: "Timestamp", width: 26) + .AddHeader(Alignment.Left, label: "HopName") + .AddHeader(Alignment.Right, label: "Latency\n(ms)") + .AddHeader(Alignment.Left, label: "Status") + .AddHeader(Alignment.Left, label: "Source") + .AddHeader(Alignment.Left, label: "Destination") .StartRowDefinition(wrap: true) - .AddPropertyColumn("Timestamp") + .AddPropertyColumn("HopName") + .AddPropertyColumn("Latency") + .AddPropertyColumn("Status") + .AddPropertyColumn("Source") + .AddPropertyColumn("Destination") .EndRowDefinition() + .GroupByProperty("Hop") .EndTable()); } From 1921ddffb567b26baf1a7cb158409d3bdbe9c142 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 13:13:54 -0400 Subject: [PATCH 29/63] :recycle: finish flattening trace output --- .../management/TestConnectionCommand.cs | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 1f00749c4ae..12d84318849 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -299,7 +299,6 @@ private void ProcessTraceroute(string targetNameOrAddress) string hostname = null; var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); - var replies = new List(DefaultTraceRoutePingCount); var timer = new Stopwatch(); PingReply reply; @@ -308,6 +307,7 @@ private void ProcessTraceroute(string targetNameOrAddress) { reply = null; pingOptions.Ttl = currentHop; + timer.Reset(); // We don't allow -Count parameter for -TraceRoute. // If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string. @@ -325,14 +325,21 @@ private void ProcessTraceroute(string targetNameOrAddress) hostname = Dns.GetHostEntry(reply.Address).HostName; } - replies.Add(new PingStatus( + var status = new TraceStatus( + currentHop, + new PingStatus( + Source, + hostname, + reply, + pingOptions, + timer.ElapsedMilliseconds, + buffer.Length), Source, - hostname, - reply, - pingOptions, - timer.ElapsedMilliseconds, - buffer.Length)); - timer.Reset(); + targetNameOrAddress, + targetAddress); + + WriteOutput(status); + WriteVerboseTraceEntry(status); } catch (PingException ex) { @@ -359,13 +366,6 @@ private void ProcessTraceroute(string targetNameOrAddress) Thread.Sleep(200); } - if (!Quiet.IsPresent) - { - var traceStatus = new TraceStatus(currentHop, replies, Source, targetNameOrAddress, targetAddress); - WriteObject(traceStatus); - WriteVerboseTraceEntry(traceStatus); - } - currentHop++; replies.Clear(); } From 43d2ffee60d3bfa523d6a111588afd4fd154ffde Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 13:44:12 -0400 Subject: [PATCH 30/63] :construction: Tidy up classes --- .../management/TestConnectionCommand.cs | 184 ++++++++---------- 1 file changed, 82 insertions(+), 102 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 12d84318849..9844f967ea5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -367,7 +367,6 @@ private void ProcessTraceroute(string targetNameOrAddress) } currentHop++; - replies.Clear(); } while (reply != null && currentHop <= MaxHops @@ -383,23 +382,23 @@ private void ProcessTraceroute(string targetNameOrAddress) private void WriteVerboseTraceEntry(TraceStatus traceStatus) { - if (traceStatus.Replies[2].Status == IPStatus.TtlExpired - || traceStatus.Replies[2].Status == IPStatus.Success) + if (traceStatus.Reply.Status == IPStatus.TtlExpired + || traceStatus.Reply.Status == IPStatus.Success) { var routerAddress = traceStatus.HopAddress.ToString(); var routerName = traceStatus.HopName; - var roundtripTime0 = traceStatus.Replies[0].Status == IPStatus.TimedOut + var roundtripTime0 = traceStatus.Reply.Status == IPStatus.TimedOut ? "*" - : traceStatus.Replies[0].Latency.ToString(); - var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut + : traceStatus.Reply.Latency.ToString(); + /*var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut ? "*" - : traceStatus.Replies[1].Latency.ToString(); + : traceStatus.Replies[1].Latency.ToString();*/ WriteVerbose(StringUtil.Format( TestConnectionResources.TraceRouteReply, traceStatus.Hop, roundtripTime0, - roundtripTime1, - traceStatus.Replies[2].Latency.ToString(), + /* roundtripTime1 */ null, + /* traceStatus.Replies[2].Latency.ToString() */ null, routerName, routerAddress)); } else @@ -727,29 +726,87 @@ public void Dispose() } /// - /// The class contains information about the source, the destination and ping results. + /// The class contains an information about a trace route attempt. /// - public class PingStatus + public class TraceStatus { /// - /// Creates a new instance of the PingStatus class. - /// This constructor permits setting the MtuSize. + /// Creates a new instance of the TraceStatus class. /// - /// The source machine name or IP of the ping. - /// The destination machine name of the ping. - /// The maximum transmission unit size determined. - /// The response from the ping attempt. - internal PingStatus(string source, string destination, int mtuSize, PingReply reply) - : this(source, destination, reply) + /// The hop number of this trace hop. + /// The PingStatus response from this trace hop. + /// The source computer name or IP address of the traceroute. + /// The target destination of the traceroute. + /// The target IPAddress of the overall traceroute. + internal TraceStatus( + int hop, + PingStatus status, + string source, + string destination, + IPAddress destinationAddress) { - if (mtuSize <= 0) - { - throw new PSArgumentException(nameof(mtuSize)); - } - - MtuSize = mtuSize; + Hop = hop; + Status = status; + Source = source; + Destination = destination; + DestinationAddress = destinationAddress; } + private readonly PingStatus _status; + + /// + /// Gets the number of the current hop / router. + /// + public int Hop { get; } + + /// + /// Gets the hostname of the current hop point. + /// + /// + public string HopName { get => _status.Destination; } + + /// + /// Gets the IP address of the current hop point. + /// + public IPAddress HopAddress { get => _status.Address; } + + /// + /// Gets the latency values of each ping to the current hop point. + /// + public long Latency { get => _status.Latency; } + + /// + /// Gets the status of the traceroute hop. + /// It is considered successful if the individual pings report either Success or TtlExpired. + /// + public IPStatus Status { get => _status.Status; } + + /// + /// Gets the source address of the traceroute command. + /// + public string Source { get; } + + /// + /// Gets the final destination hostname of the trace. + /// + public string Destination { get; } + + /// + /// Gets the final destination IP address of the trace. + /// + public IPAddress DestinationAddress { get; } + + /// + /// Gets the raw PingReply object received from the ping to this hop point. + /// + public PingReply Reply { get => _status.Reply; } + } + + /// + /// The class contains information about the source, the destination and ping results. + /// + public class PingStatus + { /// /// Creates a new instance of the PingStatus class. /// This constructor allows manually specifying the initial values for the cases where the PingReply @@ -837,83 +894,6 @@ internal PingStatus(string source, string destination, PingReply reply) public PingOptions Options { get => _options ?? Reply.Options; } } - /// - /// The class contains an information about a trace route attempt. - /// - public class TraceStatus - { - /// - /// Creates a new instance of the TraceStatus class. - /// - /// The hop number of this trace hop. - /// The PingStatus response from this trace hop. - /// The source computer name or IP address of the traceroute. - /// The target destination of the traceroute. - /// The target IPAddress of the overall traceroute. - internal TraceStatus( - int hop, - PingStatus status, - string source, - string destination, - IPAddress destinationAddress) - { - Hop = hop; - Status = status; - Source = source; - Destination = destination; - DestinationAddress = destinationAddress; - } - - private readonly PingStatus _status; - - /// - /// Gets the number of the current hop / router. - /// - public int Hop { get; } - - /// - /// Gets the hostname of the current hop point. - /// - /// - public string HopName { get => _status.Destination; } - - /// - /// Gets the IP address of the current hop point. - /// - public IPAddress HopAddress { get => _status.Address; } - - /// - /// Gets the latency values of each ping to the current hop point. - /// - public long Latency { get => _status.Latency; } - - /// - /// Gets the status of the traceroute hop. - /// It is considered successful if the individual pings report either Success or TtlExpired. - /// - public IPStatus Status { get => _status.Status; } - - /// - /// Gets the source address of the traceroute command. - /// - public string Source { get; } - - /// - /// Gets the final destination hostname of the trace. - /// - public string Destination { get; } - - /// - /// Gets the final destination IP address of the trace. - /// - public IPAddress DestinationAddress { get; } - - /// - /// Gets the raw PingReply object received from the ping to this hop point. - /// - public PingReply Reply { get => _status.Reply; } - } - /// /// The class contains information about the source, the destination and ping results. /// From 57bbbf28f87dff09d3792aa07d1c0e5cee93e1bf Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 13:49:03 -0400 Subject: [PATCH 31/63] :wrench: More tidying up of new classes --- .../commands/management/TestConnectionCommand.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 9844f967ea5..14cc1d03535 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -338,7 +338,7 @@ private void ProcessTraceroute(string targetNameOrAddress) targetNameOrAddress, targetAddress); - WriteOutput(status); + WriteObject(status); WriteVerboseTraceEntry(status); } catch (PingException ex) @@ -389,7 +389,7 @@ private void WriteVerboseTraceEntry(TraceStatus traceStatus) var routerName = traceStatus.HopName; var roundtripTime0 = traceStatus.Reply.Status == IPStatus.TimedOut ? "*" - : traceStatus.Reply.Latency.ToString(); + : traceStatus.Latency.ToString(); /*var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut ? "*" : traceStatus.Replies[1].Latency.ToString();*/ @@ -504,7 +504,7 @@ private void ProcessMTUSize(string targetNameOrAddress) } else { - WriteObject(new PingStatus(Source, targetNameOrAddress, CurrentMTUSize, replyResult)); + WriteObject(new PingMtuStatus(Source, targetNameOrAddress, CurrentMTUSize, replyResult)); } } @@ -745,8 +745,8 @@ internal TraceStatus( string destination, IPAddress destinationAddress) { + _status = status; Hop = hop; - Status = status; Source = source; Destination = destination; DestinationAddress = destinationAddress; From 4a60e445b115c75bf3d14fce5ec8b301ffdb7517 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 14:09:25 -0400 Subject: [PATCH 32/63] :wrench: Fix status for tracert --- .../commands/management/TestConnectionCommand.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 14cc1d03535..95aab9e91d7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -307,7 +307,6 @@ private void ProcessTraceroute(string targetNameOrAddress) { reply = null; pingOptions.Ttl = currentHop; - timer.Reset(); // We don't allow -Count parameter for -TraceRoute. // If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string. @@ -340,6 +339,7 @@ private void ProcessTraceroute(string targetNameOrAddress) WriteObject(status); WriteVerboseTraceEntry(status); + timer.Reset(); } catch (PingException ex) { @@ -779,7 +779,12 @@ internal TraceStatus( /// Gets the status of the traceroute hop. /// It is considered successful if the individual pings report either Success or TtlExpired. /// - public IPStatus Status { get => _status.Status; } + public IPStatus Status + { + get => _status.Status == IPStatus.TtlExpired + ? IPStatus.Success + : _status.Status; + } /// /// Gets the source address of the traceroute command. From 3ab7e55163a93bc9ea6afafd0d13cf1d1b324c57 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 16:38:25 -0400 Subject: [PATCH 33/63] :wrench: Ignore hostname resolve exception --- .../commands/management/TestConnectionCommand.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 95aab9e91d7..8bcda08f376 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -321,7 +321,14 @@ private void ProcessTraceroute(string targetNameOrAddress) if (ResolveDestination && (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired)) { - hostname = Dns.GetHostEntry(reply.Address).HostName; + try + { + hostname = Dns.GetHostEntry(reply.Address).HostName; + } + catch + { + // Swallow hostname resolve exceptions and continue with trace + } } var status = new TraceStatus( From 0dedec4dcd350886feb2c953b34d22db0db2ab37 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 19:52:47 -0400 Subject: [PATCH 34/63] :recycle: use local Ping() objects --- .../management/TestConnectionCommand.cs | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 8bcda08f376..79f0d0d3fa5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -25,7 +25,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetConnectionByTCPPort })] [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] - public class TestConnectionCommand : PSCmdlet, IDisposable + public class TestConnectionCommand : PSCmdlet { private const string ParameterSetPing = "PingCount"; private const string ParameterSetPingContinue = "PingContinue"; @@ -190,8 +190,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #endregion Parameters - private readonly Ping _sender = new Ping(); - /// /// Init the cmdlet. /// @@ -302,6 +300,7 @@ private void ProcessTraceroute(string targetNameOrAddress) var timer = new Stopwatch(); PingReply reply; + using var sender = new Ping(); do { @@ -315,7 +314,7 @@ private void ProcessTraceroute(string targetNameOrAddress) try { timer.Start(); - reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = sender.Send(targetAddress, timeout, buffer, pingOptions); timer.Stop(); if (ResolveDestination @@ -341,7 +340,7 @@ private void ProcessTraceroute(string targetNameOrAddress) timer.ElapsedMilliseconds, buffer.Length), Source, - targetNameOrAddress, + hostname ?? targetNameOrAddress, targetAddress); WriteObject(status); @@ -364,10 +363,6 @@ private void ProcessTraceroute(string targetNameOrAddress) continue; } - catch - { - // Ignore host resolve exceptions. - } // We use short delay because it is impossible DoS with trace route. Thread.Sleep(200); @@ -393,7 +388,7 @@ private void WriteVerboseTraceEntry(TraceStatus traceStatus) || traceStatus.Reply.Status == IPStatus.Success) { var routerAddress = traceStatus.HopAddress.ToString(); - var routerName = traceStatus.HopName; + var routerName = traceStatus.Hostname; var roundtripTime0 = traceStatus.Reply.Status == IPStatus.TimedOut ? "*" : traceStatus.Latency.ToString(); @@ -431,6 +426,7 @@ private void ProcessMTUSize(string targetNameOrAddress) int CurrentMTUSize = 1473; int LowMTUSize = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 1280 : 68; int timeout = TimeoutSeconds * 1000; + using var sender = new Ping(); try { @@ -447,7 +443,7 @@ private void ProcessMTUSize(string targetNameOrAddress) CurrentMTUSize, HighMTUSize)); - reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = sender.Send(targetAddress, timeout, buffer, pingOptions); // Cautious! Algorithm is sensitive to changing boundary values. if (reply.Status == IPStatus.PacketTooBig) @@ -542,12 +538,13 @@ private void ProcessPing(string targetNameOrAddress) var pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); int timeout = TimeoutSeconds * 1000; int delay = Delay * 1000; + using var sender = new Ping(); for (int i = 1; i <= Count; i++) { try { - reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = sender.Send(targetAddress, timeout, buffer, pingOptions); } catch (PingException ex) { @@ -643,7 +640,7 @@ private bool InitProcessPing( if (ResolveDestination) { resolvedTargetName = hostEntry.HostName; - hostEntry = Dns.GetHostEntry(hostEntry.HostName); + //hostEntry = Dns.GetHostEntry(hostEntry.HostName); } } catch (Exception ex) @@ -724,14 +721,6 @@ private byte[] GetSendBuffer(int bufferSize) return sendBuffer; } - /// - /// IDisposable implementation. - /// - public void Dispose() - { - _sender?.Dispose(); - } - /// /// The class contains an information about a trace route attempt. /// @@ -770,7 +759,7 @@ internal TraceStatus( /// Gets the hostname of the current hop point. /// /// - public string HopName { get => _status.Destination; } + public string Hostname { get => _status.Destination; } /// /// Gets the IP address of the current hop point. From b83d90648cdc3e9c4e763194dec1ee395f3abe08 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 19:53:35 -0400 Subject: [PATCH 35/63] :art: update formatview --- .../PowerShellCore_format_ps1xml.cs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 9e562710728..44f3ddfafa2 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -315,19 +315,21 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", TableControl.Create() - .AddHeader(Alignment.Left, label: "HopName") + .AddHeader(Alignment.Right, label: "Hop") + .AddHeader(Alignment.Left, label: "Hostname") .AddHeader(Alignment.Right, label: "Latency\n(ms)") .AddHeader(Alignment.Left, label: "Status") .AddHeader(Alignment.Left, label: "Source") - .AddHeader(Alignment.Left, label: "Destination") - .StartRowDefinition(wrap: true) - .AddPropertyColumn("HopName") + .AddHeader(Alignment.Left, label: "DestinationAddress") + .StartRowDefinition() + .AddPropertyColumn("Hop") + .AddPropertyColumn("Hostname") .AddPropertyColumn("Latency") .AddPropertyColumn("Status") .AddPropertyColumn("Source") - .AddPropertyColumn("Destination") + .AddPropertyColumn("DestinationAddress") .EndRowDefinition() - .GroupByProperty("Hop") + .GroupByProperty("Destination") .EndTable()); } From 583d232d4f3606c5b957c20decfb9bf72b8923d7 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 20:37:15 -0400 Subject: [PATCH 36/63] :wrench: fix hostname resolution for trace --- .../commands/management/TestConnectionCommand.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 79f0d0d3fa5..8673c169eaa 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -294,7 +294,7 @@ private void ProcessTraceroute(string targetNameOrAddress) int currentHop = 1; int timeout = TimeoutSeconds * 1000; - string hostname = null; + string hostname; var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); @@ -305,6 +305,7 @@ private void ProcessTraceroute(string targetNameOrAddress) do { reply = null; + hostname = null; pingOptions.Ttl = currentHop; // We don't allow -Count parameter for -TraceRoute. @@ -317,7 +318,8 @@ private void ProcessTraceroute(string targetNameOrAddress) reply = sender.Send(targetAddress, timeout, buffer, pingOptions); timer.Stop(); - if (ResolveDestination + if (hostname == null + && ResolveDestination && (reply.Status == IPStatus.Success || reply.Status == IPStatus.TtlExpired)) { try @@ -340,7 +342,7 @@ private void ProcessTraceroute(string targetNameOrAddress) timer.ElapsedMilliseconds, buffer.Length), Source, - hostname ?? targetNameOrAddress, + resolvedTargetName ?? targetNameOrAddress, targetAddress); WriteObject(status); From 74c5d3dea3816e6bbdd2304c4a72e85cc76ab144 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 23:09:15 -0400 Subject: [PATCH 37/63] :recycle: use SendAsync to ping This allows us to permit pipeline stops mid-ping, and also avoids the nasty native exceptions from sync Send --- .../management/TestConnectionCommand.cs | 71 ++++++++++++++++--- 1 file changed, 62 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 8673c169eaa..f60f462b679 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -25,7 +25,7 @@ namespace Microsoft.PowerShell.Commands [OutputType(typeof(bool), ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetConnectionByTCPPort })] [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] - public class TestConnectionCommand : PSCmdlet + public class TestConnectionCommand : PSCmdlet, IDisposable { private const string ParameterSetPing = "PingCount"; private const string ParameterSetPingContinue = "PingContinue"; @@ -34,6 +34,10 @@ public class TestConnectionCommand : PSCmdlet private const string ParameterSetDetectionOfMTUSize = "DetectionOfMTUSize"; private const int sMaxHops = 128; + private readonly Ping _sender = new Ping(); + private readonly ManualResetEventSlim _pingComplete = new ManualResetEventSlim(); + private PingCompletedEventArgs _pingCompleteArgs; + #region Parameters /// @@ -195,6 +199,8 @@ public class TestConnectionCommand : PSCmdlet /// protected override void BeginProcessing() { + _sender.PingCompleted += OnPingCompleted; + if (ParameterSetName == ParameterSetPingContinue) { Count = int.MaxValue; @@ -227,6 +233,14 @@ protected override void ProcessRecord() } } + /// + /// StopProcessing implementation so we can handle Ctrl+C properly. + /// + protected override void StopProcessing() + { + _sender?.SendAsyncCancel(); + } + #region ConnectionTest private void ProcessConnectionByTCPPort(string targetNameOrAddress) @@ -300,7 +314,6 @@ private void ProcessTraceroute(string targetNameOrAddress) var timer = new Stopwatch(); PingReply reply; - using var sender = new Ping(); do { @@ -314,9 +327,7 @@ private void ProcessTraceroute(string targetNameOrAddress) { try { - timer.Start(); - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); - timer.Stop(); + reply = DoPing(targetAddress, timeout, buffer, pingOptions, timer); if (hostname == null && ResolveDestination @@ -428,7 +439,6 @@ private void ProcessMTUSize(string targetNameOrAddress) int CurrentMTUSize = 1473; int LowMTUSize = targetAddress.AddressFamily == AddressFamily.InterNetworkV6 ? 1280 : 68; int timeout = TimeoutSeconds * 1000; - using var sender = new Ping(); try { @@ -445,7 +455,7 @@ private void ProcessMTUSize(string targetNameOrAddress) CurrentMTUSize, HighMTUSize)); - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = DoPing(targetAddress, timeout, buffer, pingOptions); // Cautious! Algorithm is sensitive to changing boundary values. if (reply.Status == IPStatus.PacketTooBig) @@ -540,13 +550,12 @@ private void ProcessPing(string targetNameOrAddress) var pingOptions = new PingOptions(MaxHops, DontFragment.IsPresent); int timeout = TimeoutSeconds * 1000; int delay = Delay * 1000; - using var sender = new Ping(); for (int i = 1; i <= Count; i++) { try { - reply = sender.Send(targetAddress, timeout, buffer, pingOptions); + reply = DoPing(targetAddress, timeout, buffer, pingOptions); } catch (PingException ex) { @@ -723,6 +732,50 @@ private byte[] GetSendBuffer(int bufferSize) return sendBuffer; } + private PingReply DoPing( + IPAddress targetAddress, + int timeout, + byte[] buffer, + PingOptions pingOptions, + Stopwatch timer = null) + { + timer?.Start(); + _sender.SendAsync(targetAddress, timeout, buffer, pingOptions, this); + _pingComplete.Wait(); + timer?.Stop(); + // Pause to let _sender's async flags to be reset properly so the next SendAsync call doesn't fail. + Thread.Sleep(1); + _pingComplete.Reset(); + + if (_pingCompleteArgs.Cancelled) + { + // The only cancellation we have implemented is on pipeline stops. + throw new PipelineStoppedException(); + } + + if (_pingCompleteArgs.Error != null) + { + throw new PingException(_pingCompleteArgs.Error.Message, _pingCompleteArgs.Error); + } + + return _pingCompleteArgs.Reply; + } + + private static void OnPingCompleted(object sender, PingCompletedEventArgs e) + { + ((TestConnectionCommand)e.UserState)._pingCompleteArgs = e; + ((TestConnectionCommand)e.UserState)._pingComplete.Set(); + } + + /// + /// IDisposable implementation. + /// + public void Dispose() + { + _sender?.Dispose(); + _pingComplete?.Dispose(); + } + /// /// The class contains an information about a trace route attempt. /// From 19b781e39cc0b5638d7147478d9281d09cd03390 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 23:46:14 -0400 Subject: [PATCH 38/63] :art: update formatters with widths --- .../PowerShellCore_format_ps1xml.cs | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 44f3ddfafa2..40c91f34e4b 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -273,12 +273,12 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", TableControl.Create() - .AddHeader(Alignment.Left, label: "Source") - .AddHeader(Alignment.Left, label: "Address") - .AddHeader(Alignment.Right, label: "Latency\n(ms)") - .AddHeader(Alignment.Right, label: "BufferSize\n(B)") - .AddHeader(Alignment.Left, label: "Status") - .StartRowDefinition(wrap: true) + .AddHeader(Alignment.Left, label: "Source", width: 16) + .AddHeader(Alignment.Left, label: "Address", width: 15) + .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) + .AddHeader(Alignment.Right, label: "BufferSize(B)", width: 10) + .AddHeader(Alignment.Center, label: "Status", width: 16) + .StartRowDefinition() .AddPropertyColumn("Source") .AddPropertyColumn("Address") .AddPropertyColumn("Latency") @@ -293,13 +293,13 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus", TableControl.Create() - .AddHeader(Alignment.Left, label: "Source") - .AddHeader(Alignment.Left, label: "Address") - .AddHeader(Alignment.Right, label: "Latency\n(ms)") - .AddHeader(Alignment.Right, label: "BufferSize\n(B)") - .AddHeader(Alignment.Left, label: "Status") - .AddHeader(Alignment.Right, label: "MtuSize\n(B)") - .StartRowDefinition(wrap: true) + .AddHeader(Alignment.Left, label: "Source", width: 16) + .AddHeader(Alignment.Left, label: "Address", width: 15) + .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) + .AddHeader(Alignment.Right, label: "BufferSize(B)", width: 10) + .AddHeader(Alignment.Center, label: "Status", width: 16) + .AddHeader(Alignment.Right, label: "MtuSize(B)", width: 7) + .StartRowDefinition() .AddPropertyColumn("Source") .AddPropertyColumn("Address") .AddPropertyColumn("Latency") @@ -315,12 +315,12 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", TableControl.Create() - .AddHeader(Alignment.Right, label: "Hop") - .AddHeader(Alignment.Left, label: "Hostname") - .AddHeader(Alignment.Right, label: "Latency\n(ms)") - .AddHeader(Alignment.Left, label: "Status") - .AddHeader(Alignment.Left, label: "Source") - .AddHeader(Alignment.Left, label: "DestinationAddress") + .AddHeader(Alignment.Right, label: "Hop", width: 3) + .AddHeader(Alignment.Left, label: "Hostname", width: 30) + .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) + .AddHeader(Alignment.Center, label: "Status", width: 16) + .AddHeader(Alignment.Left, label: "Source", width: 16) + .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) .StartRowDefinition() .AddPropertyColumn("Hop") .AddPropertyColumn("Hostname") From 1729b24b8d96f68adbfb131cc0647744e2248982 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Fri, 21 Jun 2019 23:48:24 -0400 Subject: [PATCH 39/63] :recycle: :art: rename property DestinationAddress -> TargetAddress --- .../commands/management/TestConnectionCommand.cs | 8 ++++---- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f60f462b679..47f0f1342bb 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -799,8 +799,8 @@ internal TraceStatus( _status = status; Hop = hop; Source = source; - Destination = destination; - DestinationAddress = destinationAddress; + Target = destination; + TargetAddress = destinationAddress; } private readonly PingStatus _status; @@ -845,12 +845,12 @@ public IPStatus Status /// /// Gets the final destination hostname of the trace. /// - public string Destination { get; } + public string Target { get; } /// /// Gets the final destination IP address of the trace. /// - public IPAddress DestinationAddress { get; } + public IPAddress TargetAddress { get; } /// /// Gets the raw PingReply object received from the ping to this hop point. diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 40c91f34e4b..e92ea686a1e 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -320,16 +320,16 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Center, label: "Status", width: 16) .AddHeader(Alignment.Left, label: "Source", width: 16) - .AddHeader(Alignment.Left, label: "DestinationAddress", width: 18) + .AddHeader(Alignment.Left, label: "TargetAddress", width: 18) .StartRowDefinition() .AddPropertyColumn("Hop") .AddPropertyColumn("Hostname") .AddPropertyColumn("Latency") .AddPropertyColumn("Status") .AddPropertyColumn("Source") - .AddPropertyColumn("DestinationAddress") + .AddPropertyColumn("TargetAddress") .EndRowDefinition() - .GroupByProperty("Destination") + .GroupByProperty("Target") .EndTable()); } From c5def48be7eb8e860e75232835b81a4ec9a7d1cf Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Sat, 22 Jun 2019 00:06:43 -0400 Subject: [PATCH 40/63] :art: minimize total width for traceroute format --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index e92ea686a1e..52f8b3d7f41 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -316,11 +316,11 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", TableControl.Create() .AddHeader(Alignment.Right, label: "Hop", width: 3) - .AddHeader(Alignment.Left, label: "Hostname", width: 30) + .AddHeader(Alignment.Left, label: "Hostname", width: 22) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Center, label: "Status", width: 16) - .AddHeader(Alignment.Left, label: "Source", width: 16) - .AddHeader(Alignment.Left, label: "TargetAddress", width: 18) + .AddHeader(Alignment.Left, label: "Source", width: 12) + .AddHeader(Alignment.Left, label: "TargetAddress", width: 15) .StartRowDefinition() .AddPropertyColumn("Hop") .AddPropertyColumn("Hostname") From 3877c99a6417c486b475098a1e2d860eb49358ba Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Thu, 27 Jun 2019 12:42:21 -0400 Subject: [PATCH 41/63] :sparkles: Expose Options property on TraceStatus --- .../commands/management/TestConnectionCommand.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 47f0f1342bb..3216f8e1141 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -856,6 +856,11 @@ public IPStatus Status /// Gets the raw PingReply object received from the ping to this hop point. /// public PingReply Reply { get => _status.Reply; } + + /// + /// Retrieves the PingOptions used to send the ping to the trace hop. + /// + public PingOptions Options { get => _status.Options; } } /// From b44a1d2ce6a052cf12c2d25969c4ee56e5e96752 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 12:52:14 -0400 Subject: [PATCH 42/63] :sparkles: add new Ping sequence number property --- .../management/TestConnectionCommand.cs | 24 ++++++++++++------- .../PowerShellCore_format_ps1xml.cs | 4 +++- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 3216f8e1141..ae433c2fd82 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -323,7 +323,7 @@ private void ProcessTraceroute(string targetNameOrAddress) // We don't allow -Count parameter for -TraceRoute. // If we change 'DefaultTraceRoutePingCount' we should change 'ConsoleTraceRouteReply' resource string. - for (int i = 1; i <= DefaultTraceRoutePingCount; i++) + for (uint i = 1; i <= DefaultTraceRoutePingCount; i++) { try { @@ -351,7 +351,8 @@ private void ProcessTraceroute(string targetNameOrAddress) reply, pingOptions, timer.ElapsedMilliseconds, - buffer.Length), + buffer.Length, + i), Source, resolvedTargetName ?? targetNameOrAddress, targetAddress); @@ -551,7 +552,7 @@ private void ProcessPing(string targetNameOrAddress) int timeout = TimeoutSeconds * 1000; int delay = Delay * 1000; - for (int i = 1; i <= Count; i++) + for (uint i = 1; i <= Count; i++) { try { @@ -588,7 +589,7 @@ private void ProcessPing(string targetNameOrAddress) } else { - WriteObject(new PingStatus(Source, resolvedTargetName, reply)); + WriteObject(new PingStatus(Source, resolvedTargetName, reply, i)); } if (reply.Status != IPStatus.Success) @@ -886,8 +887,9 @@ internal PingStatus( PingReply reply, PingOptions options, long latency, - int bufferSize) - : this(source, destination, reply) + int bufferSize, + uint pingNum) + : this(source, destination, reply, pingNum) { _options = options; _latency = latency; @@ -900,8 +902,9 @@ internal PingStatus( /// The source machine name or IP of the ping. /// The destination machine name of the ping. /// The response from the ping attempt. - internal PingStatus(string source, string destination, PingReply reply) + internal PingStatus(string source, string destination, PingReply reply, uint pingNum) { + Ping = pingNum; Reply = reply; Source = source; Destination = destination ?? reply.Address.ToString(); @@ -912,6 +915,11 @@ internal PingStatus(string source, string destination, PingReply reply) private readonly long _latency = -1; private readonly PingOptions _options; + /// + /// Gets the sequence number of this ping in the sequence of pings to the + /// + public uint Ping { get; } + /// /// Gets the source from which the ping was sent. /// @@ -969,7 +977,7 @@ public class PingMtuStatus : PingStatus /// The maximum transmission unit size determined. /// The response from the ping attempt. internal PingMtuStatus(string source, string destination, int mtuSize, PingReply reply) - : base(source, destination, reply) + : base(source, destination, reply, 1) { if (mtuSize <= 0) { diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 52f8b3d7f41..43b9dce0f22 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -273,12 +273,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co { yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", TableControl.Create() + .AddHeader(Alignment.Right, label: "Ping", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) .AddHeader(Alignment.Left, label: "Address", width: 15) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Right, label: "BufferSize(B)", width: 10) .AddHeader(Alignment.Center, label: "Status", width: 16) .StartRowDefinition() + .AddPropertyColumn("Ping") .AddPropertyColumn("Source") .AddPropertyColumn("Address") .AddPropertyColumn("Latency") @@ -431,7 +433,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co } elseif ($_.Duration.TotalMinutes -ge 1) { $formatString = ""m\:ss\.fff"" - } + } else { $formatString = ""s\.fff"" } From 0d83434e22e5e2798ea0dbe02d04928c8f8fd000 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 12:54:48 -0400 Subject: [PATCH 43/63] :memo: Add missing XML doc comments --- .../commands/management/TestConnectionCommand.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index ae433c2fd82..2efe8d60217 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -880,7 +880,8 @@ public class PingStatus /// The response from the ping attempt. /// The PingOptions specified when the ping was sent. /// The manually measured latency of the ping attempt. - /// The buffer size + /// The buffer size. + /// The sequence number in the sequence of pings to the hop point. internal PingStatus( string source, string destination, @@ -902,6 +903,7 @@ internal PingStatus( /// The source machine name or IP of the ping. /// The destination machine name of the ping. /// The response from the ping attempt. + /// The sequence number of the ping in the sequence of pings to the target. internal PingStatus(string source, string destination, PingReply reply, uint pingNum) { Ping = pingNum; From abf52556f35cfb9a92faa1579d977df6f1fef091 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 12:57:58 -0400 Subject: [PATCH 44/63] :sparkles: expand ping to tracert --- .../commands/management/TestConnectionCommand.cs | 5 +++++ .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 2 ++ 2 files changed, 7 insertions(+) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 2efe8d60217..97b22588dca 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -817,6 +817,11 @@ internal TraceStatus( /// public string Hostname { get => _status.Destination; } + /// + /// Gets the sequence number of the ping in the sequence of pings to the hop point. + /// + public uint Ping { get => _status.Ping; } + /// /// Gets the IP address of the current hop point. /// diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 43b9dce0f22..070e62c8d0a 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -319,6 +319,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co TableControl.Create() .AddHeader(Alignment.Right, label: "Hop", width: 3) .AddHeader(Alignment.Left, label: "Hostname", width: 22) + .AddHeader(Alignment.Right, label: "Ping", width: 4) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) .AddHeader(Alignment.Center, label: "Status", width: 16) .AddHeader(Alignment.Left, label: "Source", width: 12) @@ -326,6 +327,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .StartRowDefinition() .AddPropertyColumn("Hop") .AddPropertyColumn("Hostname") + .AddPropertyColumn("Ping") .AddPropertyColumn("Latency") .AddPropertyColumn("Status") .AddPropertyColumn("Source") From 3bf2f2469b8cd6988481b0ebc1354c6b8fbf5003 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 14:56:39 -0400 Subject: [PATCH 45/63] :memo: Tidy up verbose messages Remove redundant verbose messages and remove unneeded resx --- .../commands/management/TestConnectionCommand.cs | 15 --------------- .../resources/TestConnectionResources.resx | 15 --------------- 2 files changed, 30 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 97b22588dca..f8d9133545f 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -358,7 +358,6 @@ private void ProcessTraceroute(string targetNameOrAddress) targetAddress); WriteObject(status); - WriteVerboseTraceEntry(status); timer.Reset(); } catch (PingException ex) @@ -591,20 +590,6 @@ private void ProcessPing(string targetNameOrAddress) { WriteObject(new PingStatus(Source, resolvedTargetName, reply, i)); } - - if (reply.Status != IPStatus.Success) - { - WriteVerbose(TestConnectionResources.PingTimeOut); - } - else - { - WriteVerbose(StringUtil.Format( - TestConnectionResources.PingReply, - reply.Address.ToString(), - reply.Buffer.Length, - reply.RoundtripTime, - reply.Options?.Ttl)); - } } // Delay between ping but not after last ping. diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx index 794680166cc..edaac0dff4b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx @@ -120,21 +120,12 @@ Tracing route to {0} [{1}] over a maximum of {2} hops: - - {0,3} {1} ms {2} ms {3} ms {4} [{5}] - - - {0,3} * ms * ms * ms Request timed out. - Trace complete. Trying to connect to {0} [{1}]: - - Target: {0} [{1}]. Seconds: {2} - Pinging {0} [{1}] with {2} bytes of data: @@ -153,12 +144,6 @@ Pinging {0} [{1}] with {2} bytes of data: - - Request timed out. - - - Reply from {0}: bytes={1} time={2}ms TTL={3} - Ping complete. From a5fc9df744cd5cfb14064bfc3b827628cfd38229 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 14:57:45 -0400 Subject: [PATCH 46/63] :art: Update formatter to show classic * In the case of timeouts old tools use * to represent time --- .../PowerShellCore_format_ps1xml.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 070e62c8d0a..6c2ff7f1c86 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -304,7 +304,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .StartRowDefinition() .AddPropertyColumn("Source") .AddPropertyColumn("Address") - .AddPropertyColumn("Latency") + .AddScriptBlockColumn(@" + if ($_.Status -eq 'TimedOut') { + '*' + } + else { + $_.Latency + } + ") .AddPropertyColumn("BufferSize") .AddPropertyColumn("Status") .AddPropertyColumn("MtuSize") @@ -328,7 +335,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddPropertyColumn("Hop") .AddPropertyColumn("Hostname") .AddPropertyColumn("Ping") - .AddPropertyColumn("Latency") + .AddScriptBlockColumn(@" + if ($_.Status -eq 'TimedOut') { + '*' + } + else { + $_.Latency + } + ") .AddPropertyColumn("Status") .AddPropertyColumn("Source") .AddPropertyColumn("TargetAddress") From ea12e13e9c5eafd22c550c98e4102077f9d45810 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 14:58:33 -0400 Subject: [PATCH 47/63] :recycle: Remove unused method --- .../management/TestConnectionCommand.cs | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index f8d9133545f..e6c900bcf5e 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -395,33 +395,6 @@ private void ProcessTraceroute(string targetNameOrAddress) } } - private void WriteVerboseTraceEntry(TraceStatus traceStatus) - { - if (traceStatus.Reply.Status == IPStatus.TtlExpired - || traceStatus.Reply.Status == IPStatus.Success) - { - var routerAddress = traceStatus.HopAddress.ToString(); - var routerName = traceStatus.Hostname; - var roundtripTime0 = traceStatus.Reply.Status == IPStatus.TimedOut - ? "*" - : traceStatus.Latency.ToString(); - /*var roundtripTime1 = traceStatus.Replies[1].Status == IPStatus.TimedOut - ? "*" - : traceStatus.Replies[1].Latency.ToString();*/ - WriteVerbose(StringUtil.Format( - TestConnectionResources.TraceRouteReply, - traceStatus.Hop, - roundtripTime0, - /* roundtripTime1 */ null, - /* traceStatus.Replies[2].Latency.ToString() */ null, - routerName, routerAddress)); - } - else - { - WriteVerbose(StringUtil.Format(TestConnectionResources.TraceRouteTimeOut, traceStatus.Hop)); - } - } - #endregion TracerouteTest #region MTUSizeTest From 63da17bf9a9bd7d7e36df34349cd3abcf8180d9b Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 15:01:33 -0400 Subject: [PATCH 48/63] :art: Update main PingStatus formats --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 6c2ff7f1c86..b998e7b22bd 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -283,7 +283,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddPropertyColumn("Ping") .AddPropertyColumn("Source") .AddPropertyColumn("Address") - .AddPropertyColumn("Latency") + .AddScriptBlockColumn(@" + if ($_.Status -eq 'TimedOut') { + '*' + } + else { + $_.Latency + } + ") .AddPropertyColumn("BufferSize") .AddPropertyColumn("Status") .EndRowDefinition() From 84c1691b27891d7afc909c7430dda51764a762c3 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 16:15:05 -0400 Subject: [PATCH 49/63] :art: Update hostname display on timeout --- .../commands/management/TestConnectionCommand.cs | 7 ++++++- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e6c900bcf5e..d7880145401 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -773,7 +773,12 @@ internal TraceStatus( /// Gets the hostname of the current hop point. /// /// - public string Hostname { get => _status.Destination; } + public string Hostname + { + get => _status.Destination != "0.0.0.0" + ? _status.Destination + : null; + } /// /// Gets the sequence number of the ping in the sequence of pings to the hop point. diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index b998e7b22bd..a954416ca1c 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -340,7 +340,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .AddHeader(Alignment.Left, label: "TargetAddress", width: 15) .StartRowDefinition() .AddPropertyColumn("Hop") - .AddPropertyColumn("Hostname") + .AddScriptBlockColumn(@" + if ($_.Hostname) { + $_.HostName + } + else { + '*.*.*.*' + } + ") .AddPropertyColumn("Ping") .AddScriptBlockColumn(@" if ($_.Status -eq 'TimedOut') { From 37e6e2aa8b4afdf9f0f664c84b7fc7e21167f9f1 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 16:34:06 -0400 Subject: [PATCH 50/63] :sparkles: Update -Continues parameter -> -Repeat Ensure it emits the new PingStatus type --- .../management/TestConnectionCommand.cs | 22 +++++++------------ 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index d7880145401..d6b5e02416b 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -142,7 +142,8 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// or Int.MaxValue threshold reached. /// [Parameter(ParameterSetName = ParameterSetPingContinue)] - public SwitchParameter Continues { get; set; } + [Alias("Continues")] + public SwitchParameter Repeat { get; set; } /// /// Set short output kind ('bool' for Ping, 'int' for MTU size ...). @@ -507,7 +508,7 @@ private void ProcessPing(string targetNameOrAddress) return; } - if (!Continues.IsPresent) + if (!Repeat.IsPresent) { WriteVerbose(StringUtil.Format( TestConnectionResources.MTUSizeDetectStart, @@ -548,21 +549,14 @@ private void ProcessPing(string targetNameOrAddress) continue; } - if (Continues.IsPresent) + if (Quiet.IsPresent) { - WriteObject(reply); + // Return 'true' only if all pings have completed successfully. + quietResult &= reply.Status == IPStatus.Success; } else { - if (Quiet.IsPresent) - { - // Return 'true' only if all pings have completed successfully. - quietResult &= reply.Status == IPStatus.Success; - } - else - { - WriteObject(new PingStatus(Source, resolvedTargetName, reply, i)); - } + WriteObject(new PingStatus(Source, resolvedTargetName, reply, i)); } // Delay between ping but not after last ping. @@ -572,7 +566,7 @@ private void ProcessPing(string targetNameOrAddress) } } - if (!Continues.IsPresent) + if (!Repeat.IsPresent) { WriteVerbose(TestConnectionResources.PingComplete); } From 2053d9683717f24588510cc03755edf19470c18a Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 16:34:32 -0400 Subject: [PATCH 51/63] :art: Update default formatting when missing data --- .../management/TestConnectionCommand.cs | 4 +--- .../PowerShellCore_format_ps1xml.cs | 20 ++++++++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index d6b5e02416b..05a88f6e492 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -897,13 +897,11 @@ internal PingStatus(string source, string destination, PingReply reply, uint pin /// /// Gets the target address of the ping. /// - /// - public IPAddress Address { get => Reply.Address; } + public IPAddress Address { get => Reply.Status == IPStatus.Success ? Reply.Address : null; } /// /// Gets the roundtrip time of the ping in milliseconds. /// - /// public long Latency { get => _latency >= 0 ? _latency : Reply.RoundtripTime; } /// diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index a954416ca1c..405346ce9dc 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -282,7 +282,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co .StartRowDefinition() .AddPropertyColumn("Ping") .AddPropertyColumn("Source") - .AddPropertyColumn("Address") + .AddScriptBlockColumn(@" + if ($_.Address) { + $_.Address + } + else { + '*' + } + ") .AddScriptBlockColumn(@" if ($_.Status -eq 'TimedOut') { '*' @@ -291,7 +298,14 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co $_.Latency } ") - .AddPropertyColumn("BufferSize") + .AddScriptBlockColumn(@" + if ($_.BufferSize -gt 0) { + $_.BufferSize + } + else { + '*' + } + ") .AddPropertyColumn("Status") .EndRowDefinition() .GroupByProperty("Destination") @@ -345,7 +359,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co $_.HostName } else { - '*.*.*.*' + '*' } ") .AddPropertyColumn("Ping") From b4d7c9f7832aa95b9d75441be71563b8ac213395 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 18:43:05 -0400 Subject: [PATCH 52/63] :recycle: Trim MtuSize implementation --- .../commands/management/TestConnectionCommand.cs | 13 +++---------- .../PowerShellCore_format_ps1xml.cs | 1 - 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 05a88f6e492..df1572bed25 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -493,7 +493,7 @@ private void ProcessMTUSize(string targetNameOrAddress) } else { - WriteObject(new PingMtuStatus(Source, targetNameOrAddress, CurrentMTUSize, replyResult)); + WriteObject(new PingMtuStatus(Source, targetNameOrAddress, replyResult)); } } @@ -936,23 +936,16 @@ public class PingMtuStatus : PingStatus /// /// The source machine name or IP of the ping. /// The destination machine name of the ping. - /// The maximum transmission unit size determined. /// The response from the ping attempt. - internal PingMtuStatus(string source, string destination, int mtuSize, PingReply reply) + internal PingMtuStatus(string source, string destination, PingReply reply) : base(source, destination, reply, 1) { - if (mtuSize <= 0) - { - throw new PSArgumentException(nameof(mtuSize)); - } - - MtuSize = mtuSize; } /// /// Gets the maximum transmission unit size on the network path between the source and destination. /// - public int MtuSize { get; } = -1; + public int MtuSize { get => BufferSize; } } // Count of pings sent per each trace route hop. diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 405346ce9dc..47337c09d87 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -333,7 +333,6 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co $_.Latency } ") - .AddPropertyColumn("BufferSize") .AddPropertyColumn("Status") .AddPropertyColumn("MtuSize") .EndRowDefinition() From ba6d7f1d05b76629af2147fa481198a52a7084ef Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 19:11:28 -0400 Subject: [PATCH 53/63] :recycle: Use less convoluted parametersetnames --- .../management/TestConnectionCommand.cs | 102 +++++++++--------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index df1572bed25..dab4b15cbe7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -17,21 +17,21 @@ namespace Microsoft.PowerShell.Commands /// /// The implementation of the "Test-Connection" cmdlet. /// - [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = ParameterSetPing, + [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = PingSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] [OutputType(typeof(PingStatus), - ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetDetectionOfMTUSize })] - [OutputType(typeof(TraceStatus), ParameterSetName = new[] { ParameterSetTraceRoute })] + ParameterSetName = new[] { PingSet, RepeatPingSet, MtuSizeDetectSet })] + [OutputType(typeof(TraceStatus), ParameterSetName = new[] { TraceRouteSet })] [OutputType(typeof(bool), - ParameterSetName = new[] { ParameterSetPing, ParameterSetPingContinue, ParameterSetConnectionByTCPPort })] - [OutputType(typeof(int), ParameterSetName = new[] { ParameterSetDetectionOfMTUSize })] + ParameterSetName = new[] { PingSet, RepeatPingSet, TcpPortSet })] + [OutputType(typeof(int), ParameterSetName = new[] { MtuSizeDetectSet })] public class TestConnectionCommand : PSCmdlet, IDisposable { - private const string ParameterSetPing = "PingCount"; - private const string ParameterSetPingContinue = "PingContinue"; - private const string ParameterSetTraceRoute = "TraceRoute"; - private const string ParameterSetConnectionByTCPPort = "ConnectionByTCPPort"; - private const string ParameterSetDetectionOfMTUSize = "DetectionOfMTUSize"; + private const string PingSet = "Ping"; + private const string RepeatPingSet = "RepeatPing"; + private const string TraceRouteSet = "TraceRoute"; + private const string TcpPortSet = "TcpPort"; + private const string MtuSizeDetectSet = "MtuSizeDetect"; private const int sMaxHops = 128; private readonly Ping _sender = new Ping(); @@ -43,38 +43,38 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Do ping test. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] public SwitchParameter Ping { get; set; } = true; /// /// Force using IPv4 protocol. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] - [Parameter(ParameterSetName = ParameterSetTraceRoute)] - [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] - [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(ParameterSetName = TraceRouteSet)] + [Parameter(ParameterSetName = MtuSizeDetectSet)] + [Parameter(ParameterSetName = TcpPortSet)] public SwitchParameter IPv4 { get; set; } /// /// Force using IPv6 protocol. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] - [Parameter(ParameterSetName = ParameterSetTraceRoute)] - [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] - [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(ParameterSetName = TraceRouteSet)] + [Parameter(ParameterSetName = MtuSizeDetectSet)] + [Parameter(ParameterSetName = TcpPortSet)] public SwitchParameter IPv6 { get; set; } /// /// Do reverse DNS lookup to get names for IP addresses. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] - [Parameter(ParameterSetName = ParameterSetTraceRoute)] - [Parameter(ParameterSetName = ParameterSetDetectionOfMTUSize)] - [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(ParameterSetName = TraceRouteSet)] + [Parameter(ParameterSetName = MtuSizeDetectSet)] + [Parameter(ParameterSetName = TcpPortSet)] public SwitchParameter ResolveDestination { get; set; } /// @@ -82,10 +82,10 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default is Local Host. /// Remoting is not yet implemented internally in the cmdlet. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] - [Parameter(ParameterSetName = ParameterSetTraceRoute)] - [Parameter(ParameterSetName = ParameterSetConnectionByTCPPort)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(ParameterSetName = TraceRouteSet)] + [Parameter(ParameterSetName = TcpPortSet)] public string Source { get; } = Dns.GetHostName(); /// @@ -94,9 +94,9 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// they decrement the Time-to-Live (TTL) value found in the packet header. /// The default (from Windows) is 128 hops. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] - [Parameter(ParameterSetName = ParameterSetTraceRoute)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(ParameterSetName = TraceRouteSet)] [ValidateRange(0, sMaxHops)] [Alias("Ttl", "TimeToLive", "Hops")] public int MaxHops { get; set; } = sMaxHops; @@ -105,7 +105,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Count of attempts. /// The default (from Windows) is 4 times. /// - [Parameter(ParameterSetName = ParameterSetPing)] + [Parameter(ParameterSetName = PingSet)] [ValidateRange(ValidateRangeKind.Positive)] public int Count { get; set; } = 4; @@ -113,8 +113,8 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Delay between attempts. /// The default (from Windows) is 1 second. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] [ValidateRange(ValidateRangeKind.Positive)] public int Delay { get; set; } = 1; @@ -123,8 +123,8 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// The default (from Windows) is 32 bites. /// Max value is 65500 (limit from Windows API). /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] [Alias("Size", "Bytes", "BS")] [ValidateRange(0, 65500)] public int BufferSize { get; set; } = DefaultSendBufferSize; @@ -133,15 +133,15 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Don't fragment ICMP packages. /// Currently CoreFX not supports this on Unix. /// - [Parameter(ParameterSetName = ParameterSetPing)] - [Parameter(ParameterSetName = ParameterSetPingContinue)] + [Parameter(ParameterSetName = PingSet)] + [Parameter(ParameterSetName = RepeatPingSet)] public SwitchParameter DontFragment { get; set; } /// /// Continue ping until user press Ctrl-C /// or Int.MaxValue threshold reached. /// - [Parameter(ParameterSetName = ParameterSetPingContinue)] + [Parameter(ParameterSetName = RepeatPingSet)] [Alias("Continues")] public SwitchParameter Repeat { get; set; } @@ -177,20 +177,20 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Detect MTU size. /// - [Parameter(Mandatory = true, ParameterSetName = ParameterSetDetectionOfMTUSize)] + [Parameter(Mandatory = true, ParameterSetName = MtuSizeDetectSet)] public SwitchParameter MTUSizeDetect { get; set; } /// /// Do traceroute test. /// - [Parameter(Mandatory = true, ParameterSetName = ParameterSetTraceRoute)] + [Parameter(Mandatory = true, ParameterSetName = TraceRouteSet)] public SwitchParameter Traceroute { get; set; } /// /// Do tcp connection test. /// [ValidateRange(0, 65535)] - [Parameter(Mandatory = true, ParameterSetName = ParameterSetConnectionByTCPPort)] + [Parameter(Mandatory = true, ParameterSetName = TcpPortSet)] public int TCPPort { get; set; } #endregion Parameters @@ -202,7 +202,7 @@ protected override void BeginProcessing() { _sender.PingCompleted += OnPingCompleted; - if (ParameterSetName == ParameterSetPingContinue) + if (ParameterSetName == RepeatPingSet) { Count = int.MaxValue; } @@ -217,17 +217,17 @@ protected override void ProcessRecord() { switch (ParameterSetName) { - case ParameterSetPing: - case ParameterSetPingContinue: + case PingSet: + case RepeatPingSet: ProcessPing(targetName); break; - case ParameterSetDetectionOfMTUSize: + case MtuSizeDetectSet: ProcessMTUSize(targetName); break; - case ParameterSetTraceRoute: + case TraceRouteSet: ProcessTraceroute(targetName); break; - case ParameterSetConnectionByTCPPort: + case TcpPortSet: ProcessConnectionByTCPPort(targetName); break; } From 07540c0be14d1019b015dc0bd57daf4714dd54d3 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 19:12:30 -0400 Subject: [PATCH 54/63] :recycle: :art: Improve code style, update formats --- .../commands/management/TestConnectionCommand.cs | 8 +++++--- .../resources/TestConnectionResources.resx | 7 ++----- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 10 ++++++---- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index dab4b15cbe7..e5a492ed7d4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -418,13 +418,15 @@ private void ProcessMTUSize(string targetNameOrAddress) { var pingOptions = new PingOptions(MaxHops, true); int retry = 1; + int pingCount = 0; while (LowMTUSize < (HighMTUSize - 1)) { + pingCount++; byte[] buffer = GetSendBuffer(CurrentMTUSize); - WriteDebug(StringUtil.Format( - "LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2}", + WriteVerbose(StringUtil.Format( + TestConnectionResources.MtuSizeDetectDebug, LowMTUSize, CurrentMTUSize, HighMTUSize)); @@ -511,7 +513,7 @@ private void ProcessPing(string targetNameOrAddress) if (!Repeat.IsPresent) { WriteVerbose(StringUtil.Format( - TestConnectionResources.MTUSizeDetectStart, + TestConnectionResources.PingStart, resolvedTargetName, targetAddress, BufferSize)); diff --git a/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx b/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx index edaac0dff4b..6dc729dc250 100644 --- a/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx +++ b/src/Microsoft.PowerShell.Commands.Management/resources/TestConnectionResources.resx @@ -126,11 +126,8 @@ Trying to connect to {0} [{1}]: - - Pinging {0} [{1}] with {2} bytes of data: - - - MTU size: {0}. Attempt: {1} + + LowMTUSize: {0}, CurrentMTUSize: {1}, HighMTUSize: {2} Testing connection to computer '{0}' failed: {1} diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 47337c09d87..2354b64c6af 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -271,7 +271,8 @@ internal static IEnumerable GetFormatData() private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingStatus() { - yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", + yield return new FormatViewDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus", TableControl.Create() .AddHeader(Alignment.Right, label: "Ping", width: 4) .AddHeader(Alignment.Left, label: "Source", width: 16) @@ -314,12 +315,12 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_PingMtuStatus() { - yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus", + yield return new FormatViewDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus", TableControl.Create() .AddHeader(Alignment.Left, label: "Source", width: 16) .AddHeader(Alignment.Left, label: "Address", width: 15) .AddHeader(Alignment.Right, label: "Latency(ms)", width: 7) - .AddHeader(Alignment.Right, label: "BufferSize(B)", width: 10) .AddHeader(Alignment.Center, label: "Status", width: 16) .AddHeader(Alignment.Right, label: "MtuSize(B)", width: 7) .StartRowDefinition() @@ -342,7 +343,8 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co private static IEnumerable ViewsOf_Microsoft_PowerShell_Commands_TestConnectionCommand_TraceStatus() { - yield return new FormatViewDefinition("Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", + yield return new FormatViewDefinition( + "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus", TableControl.Create() .AddHeader(Alignment.Right, label: "Hop", width: 3) .AddHeader(Alignment.Left, label: "Hostname", width: 22) From abcd05bf836681745d7167eb517c102d3e87776e Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 19:23:08 -0400 Subject: [PATCH 55/63] :white_check_mark: Remove unneeded pref variables --- .../Test-Connection.Tests.ps1 | 95 +++++++++---------- 1 file changed, 44 insertions(+), 51 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index a4a2c14433f..1dba40523d9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -5,11 +5,6 @@ Import-Module HelpersCommon Describe "Test-Connection" -tags "CI" { BeforeAll { - $oldInformationPreference = $InformationPreference - $oldProgressPreference = $ProgressPreference - $InformationPreference = "Ignore" - $ProgressPreference = "SilentlyContinue" - $hostName = [System.Net.Dns]::GetHostName() $targetName = "localhost" $targetAddress = "127.0.0.1" @@ -22,33 +17,28 @@ Describe "Test-Connection" -tags "CI" { # this resolves to an actual IP rather than 127.0.0.1 # this can also include both IPv4 and IPv6, so select InterNetwork rather than InterNetworkV6 $realAddress = [System.Net.Dns]::GetHostEntry($hostName).AddressList | - Where-Object {$_.AddressFamily -eq "InterNetwork"} | + Where-Object { $_.AddressFamily -eq "InterNetwork" } | Select-Object -First 1 | - Foreach-Object {$_.IPAddressToString} + ForEach-Object { $_.IPAddressToString } # under some environments, we can't round trip this and retrieve the real name from the address # in this case we will simply use the hostname $jobContinues = Start-Job { Test-Connection $using:targetAddress -Continues } } - AfterAll { - $InformationPreference = $oldInformationPreference - $ProgressPreference = $oldProgressPreference - } - Context "Ping" { It "Default parameter set is 'Ping'" { $result = Test-Connection $targetName $replies = $result.Replies - $result.Count | Should -Be 1 - $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingReport" - $result[0].Source | Should -BeExactly $hostName + $result.Count | Should -Be 1 + $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingReport" + $result[0].Source | Should -BeExactly $hostName $result[0].Destination | Should -BeExactly $targetName - $replies.Count | Should -Be 4 - $replies[0] | Should -BeOfType "System.Net.NetworkInformation.PingReply" - $replies[0].Address | Should -BeExactly $targetAddressIPv6 - $replies[0].Status | Should -BeExactly "Success" + $replies.Count | Should -Be 4 + $replies[0] | Should -BeOfType "System.Net.NetworkInformation.PingReply" + $replies[0].Address | Should -BeExactly $targetAddressIPv6 + $replies[0].Status | Should -BeExactly "Success" # TODO: Here and below we skip the check on Unix because .Net Core issue if ($isWindows) { $replies[0].Buffer.Count | Should -Be 32 @@ -79,7 +69,8 @@ Describe "Test-Connection" -tags "CI" { # Error code = 11001 - Host not found. if (!$isWindows) { $Error[0].Exception.InnerException.ErrorCode | Should -Be -131073 - } else { + } + else { $Error[0].Exception.InnerException.ErrorCode | Should -Be 11001 } } @@ -88,8 +79,8 @@ Describe "Test-Connection" -tags "CI" { It "Force IPv4 with implicit PingOptions" { $result = Test-Connection $hostName -Count 1 -IPv4 - $result.Replies[0].Address | Should -BeExactly $realAddress - $result.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 + $result.Replies[0].Address | Should -BeExactly $realAddress + $result.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 if ($isWindows) { $result.Replies[0].Options.DontFragment | Should -BeFalse } @@ -103,19 +94,20 @@ Describe "Test-Connection" -tags "CI" { # it's more about breaking out of the loop $result2 = Test-Connection 8.8.8.8 -Count 1 -IPv4 -MaxHops 1 -DontFragment - $result1.Replies[0].Address | Should -BeExactly $realAddress + $result1.Replies[0].Address | Should -BeExactly $realAddress # .Net Core (.Net Framework) returns Options based on default PingOptions() constructor (Ttl=128, DontFragment = false). # After .Net Core fix we should have 'DontFragment | Should -Be $true' here. - $result1.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 + $result1.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 if (!$isWindows) { $result1.Replies[0].Options.DontFragment | Should -BeNullOrEmpty # depending on the network configuration any of the following should be returned - $result2.Replies[0].Status | Should -BeIn "TtlExpired","TimedOut","Success" - } else { + $result2.Replies[0].Status | Should -BeIn "TtlExpired", "TimedOut", "Success" + } + else { $result1.Replies[0].Options.DontFragment | Should -BeFalse # We expect 'TtlExpired' but if a router don't reply we get `TimedOut` # AzPipelines returns $null - $result2.Replies[0].Status | Should -BeIn "TtlExpired","TimedOut",$null + $result2.Replies[0].Status | Should -BeIn "TtlExpired", "TimedOut", $null } } @@ -128,23 +120,23 @@ Describe "Test-Connection" -tags "CI" { } It "MaxHops Should -Be greater 0" { - { Test-Connection $targetName -MaxHops 0 } | Should -Throw -ErrorId "System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -MaxHops 0 } | Should -Throw -ErrorId "System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.TestConnectionCommand" { Test-Connection $targetName -MaxHops -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "Count Should -Be greater 0" { - { Test-Connection $targetName -Count 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -Count 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" { Test-Connection $targetName -Count -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "Delay Should -Be greater 0" { - { Test-Connection $targetName -Delay 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -Delay 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" { Test-Connection $targetName -Delay -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "Delay works" { - $result1 = measure-command {Test-Connection localhost -Count 2} - $result2 = measure-command {Test-Connection localhost -Delay 4 -Count 2} + $result1 = Measure-Command { Test-Connection localhost -Count 2 } + $result2 = Measure-Command { Test-Connection localhost -Delay 4 -Count 2 } $result1.TotalSeconds | Should -BeGreaterThan 1 $result1.TotalSeconds | Should -BeLessThan 3 @@ -152,8 +144,8 @@ Describe "Test-Connection" -tags "CI" { } It "BufferSize Should -Be between 0 and 65500" { - { Test-Connection $targetName -BufferSize 0 } | Should Not Throw - { Test-Connection $targetName -BufferSize -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -BufferSize 0 } | Should Not Throw + { Test-Connection $targetName -BufferSize -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" { Test-Connection $targetName -BufferSize 65501 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } @@ -170,7 +162,7 @@ Describe "Test-Connection" -tags "CI" { $resolvedName = [System.Net.DNS]::GetHostEntry($targetAddress).HostName $result.Destination | Should -BeExactly $resolvedName - $result.Replies[0].Address | Should -BeExactly $targetAddress + $result.Replies[0].Address | Should -BeExactly $targetAddress } It "ResolveDestination for name" { @@ -182,7 +174,7 @@ Describe "Test-Connection" -tags "CI" { $resolvedAddress = ([System.Net.DNS]::GetHostAddresses($resolvedName)[0] -split "%")[0] $result.Destination | Should -BeExactly $resolvedName - $result.Replies[0].Address | Should -BeExactly $resolvedAddress + $result.Replies[0].Address | Should -BeExactly $resolvedAddress } It "TimeOut works" { @@ -196,14 +188,14 @@ Describe "Test-Connection" -tags "CI" { $result = Receive-Job $jobContinues Remove-Job $jobContinues -Force - $result.Count | Should -BeGreaterThan 4 - $result[0].Address | Should -BeExactly $targetAddress - $result[0].Status | Should -BeExactly "Success" + $result.Count | Should -BeGreaterThan 4 + $result[0].Address | Should -BeExactly $targetAddress + $result[0].Status | Should -BeExactly "Success" if ($isWindows) { $result[0].Buffer.Count | Should -Be 32 } } -} + } # TODO: We skip the MTUSizeDetect tests on Unix because we expect 'TtlExpired' but get 'TimeOut' internally from .Net Core Context "MTUSizeDetect" { @@ -232,22 +224,23 @@ Describe "Test-Connection" -tags "CI" { # Check target host reply. $pingReplies = $replies[-1].PingReplies - $result.Count | Should -Be 1 - $result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteResult" - $result.Source | Should -BeExactly $hostName + $result.Count | Should -Be 1 + $result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteResult" + $result.Source | Should -BeExactly $hostName $result.DestinationAddress | Should -BeExactly $realAddress - $result.DestinationHost | Should -BeExactly $hostName + $result.DestinationHost | Should -BeExactly $hostName - $replies.Count | Should -BeGreaterThan 0 - $replies[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteReply" - $replies[0].Hop | Should -Be 1 + $replies.Count | Should -BeGreaterThan 0 + $replies[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteReply" + $replies[0].Hop | Should -Be 1 - $pingReplies.Count | Should -Be 3 - $pingReplies[0].Address | Should -BeExactly $realAddress - $pingReplies[0].Status | Should -BeExactly "Success" + $pingReplies.Count | Should -Be 3 + $pingReplies[0].Address | Should -BeExactly $realAddress + $pingReplies[0].Status | Should -BeExactly "Success" if (!$isWindows) { $pingReplies[0].Buffer.Count | Should -Match '^0$|^32$' - } else { + } + else { $pingReplies[0].Buffer.Count | Should -Be 32 } } From 7dce1e94d149c1f8183276954a369641641c4440 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 20:01:48 -0400 Subject: [PATCH 56/63] :white_check_mark: Update tests --- .../Test-Connection.Tests.ps1 | 134 +++++++++--------- 1 file changed, 67 insertions(+), 67 deletions(-) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index 1dba40523d9..be28f17b3b9 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -22,26 +22,26 @@ Describe "Test-Connection" -tags "CI" { ForEach-Object { $_.IPAddressToString } # under some environments, we can't round trip this and retrieve the real name from the address # in this case we will simply use the hostname - $jobContinues = Start-Job { Test-Connection $using:targetAddress -Continues } + $jobContinues = Start-Job { Test-Connection $using:targetAddress -Repeat } } Context "Ping" { It "Default parameter set is 'Ping'" { $result = Test-Connection $targetName - $replies = $result.Replies - $result.Count | Should -Be 1 - $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingReport" + $result.Count | Should -Be 4 + $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus" + $result[0].Ping | Should -Be 1 $result[0].Source | Should -BeExactly $hostName $result[0].Destination | Should -BeExactly $targetName - - $replies.Count | Should -Be 4 - $replies[0] | Should -BeOfType "System.Net.NetworkInformation.PingReply" - $replies[0].Address | Should -BeExactly $targetAddressIPv6 - $replies[0].Status | Should -BeExactly "Success" + $result[0].Address | Should -BeExactly $targetAddressIPv6 + $result[0].Status | Should -BeExactly "Success" + $result[0].Latency | Should -BeOfType "long" + $result[0].Reply | Should -BeOfType "System.Net.NetworkInformation.PingReply" + $result[0].Options | Should -BeOfType "System.Net.NetworkInformation.PingOptions" # TODO: Here and below we skip the check on Unix because .Net Core issue if ($isWindows) { - $replies[0].Buffer.Count | Should -Be 32 + $replies[0].BufferSize | Should -Be 32 } } @@ -50,8 +50,8 @@ Describe "Test-Connection" -tags "CI" { $result1 = Test-Connection -Ping $targetName -Count 1 $result2 = Test-Connection $targetName -Count 2 - $result1.Replies.Count | Should -Be 1 - $result2.Replies.Count | Should -Be 2 + $result1.Count | Should -Be 1 + $result2.Count | Should -Be 2 } It "Quiet works" { @@ -65,7 +65,8 @@ Describe "Test-Connection" -tags "CI" { It "Ping fake host" { - { $result = Test-Connection "fakeHost" -Count 1 -Quiet -ErrorAction Stop } | Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand" + { $result = Test-Connection "fakeHost" -Count 1 -Quiet -ErrorAction Stop } | + Should -Throw -ErrorId "TestConnectionException,Microsoft.PowerShell.Commands.TestConnectionCommand" # Error code = 11001 - Host not found. if (!$isWindows) { $Error[0].Exception.InnerException.ErrorCode | Should -Be -131073 @@ -79,10 +80,10 @@ Describe "Test-Connection" -tags "CI" { It "Force IPv4 with implicit PingOptions" { $result = Test-Connection $hostName -Count 1 -IPv4 - $result.Replies[0].Address | Should -BeExactly $realAddress - $result.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 + $result[0].Address | Should -BeExactly $realAddress + $result[0].Options.Ttl | Should -BeLessOrEqual 128 if ($isWindows) { - $result.Replies[0].Options.DontFragment | Should -BeFalse + $result[0].Options.DontFragment | Should -BeFalse } } @@ -94,34 +95,36 @@ Describe "Test-Connection" -tags "CI" { # it's more about breaking out of the loop $result2 = Test-Connection 8.8.8.8 -Count 1 -IPv4 -MaxHops 1 -DontFragment - $result1.Replies[0].Address | Should -BeExactly $realAddress + $result1[0].Address | Should -BeExactly $realAddress # .Net Core (.Net Framework) returns Options based on default PingOptions() constructor (Ttl=128, DontFragment = false). # After .Net Core fix we should have 'DontFragment | Should -Be $true' here. - $result1.Replies[0].Options.Ttl | Should -BeLessOrEqual 128 + $result1[0].Options.Ttl | Should -BeLessOrEqual 128 if (!$isWindows) { - $result1.Replies[0].Options.DontFragment | Should -BeNullOrEmpty - # depending on the network configuration any of the following should be returned - $result2.Replies[0].Status | Should -BeIn "TtlExpired", "TimedOut", "Success" + $result1[0].Options.DontFragment | Should -BeNullOrEmpty + # Depending on the network configuration any of the following should be returned + $result2[0].Status | Should -BeIn "TtlExpired", "TimedOut", "Success" } else { - $result1.Replies[0].Options.DontFragment | Should -BeFalse + $result1[0].Options.DontFragment | Should -BeFalse # We expect 'TtlExpired' but if a router don't reply we get `TimedOut` # AzPipelines returns $null - $result2.Replies[0].Status | Should -BeIn "TtlExpired", "TimedOut", $null + $result2[0].Status | Should -BeIn "TtlExpired", "TimedOut", $null } } It "Force IPv6" -Pending { $result = Test-Connection $targetName -Count 1 -IPv6 - $result.Replies[0].Address | Should -BeExactly $targetAddressIPv6 + $result[0].Address | Should -BeExactly $targetAddressIPv6 # We should check Null not Empty! - $result.Replies[0].Options | Should -Be $null + $result[0].Options | Should -Be $null } It "MaxHops Should -Be greater 0" { - { Test-Connection $targetName -MaxHops 0 } | Should -Throw -ErrorId "System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.TestConnectionCommand" - { Test-Connection $targetName -MaxHops -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -MaxHops 0 } | + Should -Throw -ErrorId "System.ArgumentOutOfRangeException,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -MaxHops -1 } | + Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "Count Should -Be greater 0" { @@ -130,8 +133,10 @@ Describe "Test-Connection" -tags "CI" { } It "Delay Should -Be greater 0" { - { Test-Connection $targetName -Delay 0 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" - { Test-Connection $targetName -Delay -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -Delay 0 } | + Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -Delay -1 } | + Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "Delay works" { @@ -144,16 +149,18 @@ Describe "Test-Connection" -tags "CI" { } It "BufferSize Should -Be between 0 and 65500" { - { Test-Connection $targetName -BufferSize 0 } | Should Not Throw - { Test-Connection $targetName -BufferSize -1 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" - { Test-Connection $targetName -BufferSize 65501 } | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -BufferSize 0 } | Should -Not Throw + { Test-Connection $targetName -BufferSize -1 } | + Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" + { Test-Connection $targetName -BufferSize 65501 } | + Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.TestConnectionCommand" } It "BufferSize works" -Pending:(!$IsWindows) { $result = Test-Connection $targetName -Count 1 -BufferSize 2 if ($isWindows) { - $result.Replies[0].Buffer.Count | Should -Be 2 + $result.BufferSize | Should -Be 2 } } @@ -162,7 +169,7 @@ Describe "Test-Connection" -tags "CI" { $resolvedName = [System.Net.DNS]::GetHostEntry($targetAddress).HostName $result.Destination | Should -BeExactly $resolvedName - $result.Replies[0].Address | Should -BeExactly $targetAddress + $result.Address | Should -BeExactly $targetAddress } It "ResolveDestination for name" { @@ -174,16 +181,18 @@ Describe "Test-Connection" -tags "CI" { $resolvedAddress = ([System.Net.DNS]::GetHostAddresses($resolvedName)[0] -split "%")[0] $result.Destination | Should -BeExactly $resolvedName - $result.Replies[0].Address | Should -BeExactly $resolvedAddress + $result.Address | Should -BeExactly $resolvedAddress } It "TimeOut works" { - (Measure-Command { Test-Connection $UnreachableAddress -Count 1 -TimeOut 1 }).TotalSeconds | Should -BeLessThan 3 - (Measure-Command { Test-Connection $UnreachableAddress -Count 1 -TimeOut 4 }).TotalSeconds | Should -BeGreaterThan 3 + (Measure-Command { Test-Connection $UnreachableAddress -Count 1 -TimeOut 1 }).TotalSeconds | + Should -BeLessThan 3 + (Measure-Command { Test-Connection $UnreachableAddress -Count 1 -TimeOut 4 }).TotalSeconds | + Should -BeGreaterThan 3 } - It "Continues works" { - # By default we do 4 ping so for '-Continues' we expect to get >4 results. + It "Repeat works" { + # By default we do 4 ping so for '-Repeat' we expect to get >4 results. # Also we should wait >4 seconds before check results but previous tests already did the pause. $result = Receive-Job $jobContinues Remove-Job $jobContinues -Force @@ -192,24 +201,24 @@ Describe "Test-Connection" -tags "CI" { $result[0].Address | Should -BeExactly $targetAddress $result[0].Status | Should -BeExactly "Success" if ($isWindows) { - $result[0].Buffer.Count | Should -Be 32 + $result[0].BufferSize | Should -Be 32 } } } - # TODO: We skip the MTUSizeDetect tests on Unix because we expect 'TtlExpired' but get 'TimeOut' internally from .Net Core + # TODO: We skip the MTUSizeDetect tests on Unix because we expect 'PacketTooBig' but get 'TimeOut' internally from .Net Core Context "MTUSizeDetect" { It "MTUSizeDetect works" -pending:($IsMacOS) { - $result = Test-Connection $hostName -MTUSizeDetect + $result = Test-Connection $hostName -DetectMtuSize - $result | Should -BeOfType "System.Net.NetworkInformation.PingReply" + $result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+PingMtuStatus" $result.Destination | Should -BeExactly $hostName $result.Status | Should -BeExactly "Success" - $result.MTUSize | Should -BeGreaterThan 0 + $result.MtuSize | Should -BeGreaterThan 0 } It "Quiet works" -pending:($IsMacOS) { - $result = Test-Connection $hostName -MTUSizeDetect -Quiet + $result = Test-Connection $hostName -DetectMtuSize -Quiet $result | Should -BeOfType "Int32" $result | Should -BeGreaterThan 0 @@ -220,33 +229,24 @@ Describe "Test-Connection" -tags "CI" { It "TraceRoute works" { # real address is an ipv4 address, so force IPv4 $result = Test-Connection $hostName -TraceRoute -IPv4 - $replies = $result.Replies - # Check target host reply. - $pingReplies = $replies[-1].PingReplies - - $result.Count | Should -Be 1 - $result | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteResult" - $result.Source | Should -BeExactly $hostName - $result.DestinationAddress | Should -BeExactly $realAddress - $result.DestinationHost | Should -BeExactly $hostName - - $replies.Count | Should -BeGreaterThan 0 - $replies[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceRouteReply" - $replies[0].Hop | Should -Be 1 - - $pingReplies.Count | Should -Be 3 - $pingReplies[0].Address | Should -BeExactly $realAddress - $pingReplies[0].Status | Should -BeExactly "Success" + + $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus" + $result[0].Source | Should -BeExactly $hostName + $result[0].DestinationAddress | Should -BeExactly $realAddress + $result[0].DestinationHost | Should -BeExactly $hostName + $result[0].Hop | Should -Be 1 + $result[0].HopAddress | Should -BeExactly $realAddress + $result[0].Status | Should -BeExactly "Success" if (!$isWindows) { - $pingReplies[0].Buffer.Count | Should -Match '^0$|^32$' + $result[0].Reply.Buffer.Count | Should -Match '^0$|^32$' } else { - $pingReplies[0].Buffer.Count | Should -Be 32 + $result[0].Reply.Buffer.Count | Should -Be 32 } } It "Quiet works" { - $result = Test-Connection $hostName -TraceRoute -Quiet 6> $null + $result = Test-Connection $hostName -TraceRoute -Quiet $result | Should -BeTrue } @@ -261,10 +261,10 @@ Describe "Connection" -Tag "CI", "RequireAdminOnWindows" { } It "Test connection to local host port 80" { - Test-Connection '127.0.0.1' -TCPPort $WebListener.HttpPort | Should -BeTrue + Test-Connection '127.0.0.1' -TcpPort $WebListener.HttpPort | Should -BeTrue } It "Test connection to unreachable host port 80" { - Test-Connection $UnreachableAddress -TCPPort 80 -TimeOut 1 | Should -BeFalse + Test-Connection $UnreachableAddress -TcpPort 80 -TimeOut 1 | Should -BeFalse } } From 8d7cefac367784a71f30010d9324e2d37f084608 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 20:02:45 -0400 Subject: [PATCH 57/63] :art: Update parameter naming --- .../commands/management/TestConnectionCommand.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index e5a492ed7d4..b73a802ed47 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -178,7 +178,8 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Detect MTU size. /// [Parameter(Mandatory = true, ParameterSetName = MtuSizeDetectSet)] - public SwitchParameter MTUSizeDetect { get; set; } + [Alias("MtuSizeDetect")] + public SwitchParameter DetectMtuSize { get; set; } /// /// Do traceroute test. @@ -191,7 +192,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// [ValidateRange(0, 65535)] [Parameter(Mandatory = true, ParameterSetName = TcpPortSet)] - public int TCPPort { get; set; } + public int TcpPort { get; set; } #endregion Parameters @@ -255,7 +256,7 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) try { - Task connectionTask = client.ConnectAsync(targetAddress, TCPPort); + Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); var targetString = targetAddress.ToString(); for (var i = 1; i <= TimeoutSeconds; i++) From 184f79f0a933be04d8dedec988c3b6d89b92b4d3 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 21:23:58 -0400 Subject: [PATCH 58/63] :wrench: Adjust parameter sets --- .../management/TestConnectionCommand.cs | 48 +++++++++---------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index b73a802ed47..4c820df2ed4 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -19,11 +19,10 @@ namespace Microsoft.PowerShell.Commands /// [Cmdlet(VerbsDiagnostic.Test, "Connection", DefaultParameterSetName = PingSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135266")] - [OutputType(typeof(PingStatus), - ParameterSetName = new[] { PingSet, RepeatPingSet, MtuSizeDetectSet })] + [OutputType(typeof(PingStatus), ParameterSetName = new[] { PingSet, RepeatPingSet })] [OutputType(typeof(TraceStatus), ParameterSetName = new[] { TraceRouteSet })] - [OutputType(typeof(bool), - ParameterSetName = new[] { PingSet, RepeatPingSet, TcpPortSet })] + [OutputType(typeof(PingMtuStatus), ParameterSetName = new[] { MtuSizeDetectSet })] + [OutputType(typeof(bool), ParameterSetName = new[] { PingSet, RepeatPingSet, TcpPortSet })] [OutputType(typeof(int), ParameterSetName = new[] { MtuSizeDetectSet })] public class TestConnectionCommand : PSCmdlet, IDisposable { @@ -32,7 +31,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable private const string TraceRouteSet = "TraceRoute"; private const string TcpPortSet = "TcpPort"; private const string MtuSizeDetectSet = "MtuSizeDetect"; - private const int sMaxHops = 128; + private const int DefaultMaxHops = 128; private readonly Ping _sender = new Ping(); private readonly ManualResetEventSlim _pingComplete = new ManualResetEventSlim(); @@ -41,14 +40,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable #region Parameters /// - /// Do ping test. + /// Gets or sets the Ping parameter. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] public SwitchParameter Ping { get; set; } = true; /// - /// Force using IPv4 protocol. + /// Gets or sets whether to force use of the IPv4 protocol. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] @@ -58,7 +57,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public SwitchParameter IPv4 { get; set; } /// - /// Force using IPv6 protocol. + /// Gets or sets whether to force use of the IPv6 protocol. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] @@ -68,7 +67,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public SwitchParameter IPv6 { get; set; } /// - /// Do reverse DNS lookup to get names for IP addresses. + /// Gets or sets whether to do reverse DNS lookup to get names for IP addresses. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] @@ -78,7 +77,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public SwitchParameter ResolveDestination { get; set; } /// - /// Source from which to do a test (ping, trace route, ...). + /// Gets or sets the source hostname from which to perform a test (ping, trace route, ...). /// The default is Local Host. /// Remoting is not yet implemented internally in the cmdlet. /// @@ -89,20 +88,20 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public string Source { get; } = Dns.GetHostName(); /// - /// The number of times the Ping data packets can be forwarded by routers. - /// As gateways and routers transmit packets through a network, - /// they decrement the Time-to-Live (TTL) value found in the packet header. + /// Gets or sets the number of times the Ping data packets can be forwarded by routers. + /// As gateways and routers transmit packets through a network, they decrement the Time-to-Live (TTL) value + /// found in the packet header. /// The default (from Windows) is 128 hops. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] [Parameter(ParameterSetName = TraceRouteSet)] - [ValidateRange(0, sMaxHops)] + [ValidateRange(0, DefaultMaxHops)] [Alias("Ttl", "TimeToLive", "Hops")] - public int MaxHops { get; set; } = sMaxHops; + public int MaxHops { get; set; } = DefaultMaxHops; /// - /// Count of attempts. + /// Gets or sets the number of pings to attempt. /// The default (from Windows) is 4 times. /// [Parameter(ParameterSetName = PingSet)] @@ -110,7 +109,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public int Count { get; set; } = 4; /// - /// Delay between attempts. + /// Gets or sets the delay between ping attempts. /// The default (from Windows) is 1 second. /// [Parameter(ParameterSetName = PingSet)] @@ -119,7 +118,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public int Delay { get; set; } = 1; /// - /// Buffer size to send. + /// Gets or sets the size of the buffer to send. /// The default (from Windows) is 32 bites. /// Max value is 65500 (limit from Windows API). /// @@ -130,7 +129,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public int BufferSize { get; set; } = DefaultSendBufferSize; /// - /// Don't fragment ICMP packages. + /// Gets or sets whether to prevent fragmentation of ICMP packages. /// Currently CoreFX not supports this on Unix. /// [Parameter(ParameterSetName = PingSet)] @@ -138,19 +137,20 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public SwitchParameter DontFragment { get; set; } /// - /// Continue ping until user press Ctrl-C - /// or Int.MaxValue threshold reached. + /// Gets or sets whether to ping the target endlessly, until the user presses Ctrl+C or the maximum threshold of + /// Int.MaxValue pings is reached. /// - [Parameter(ParameterSetName = RepeatPingSet)] + [Parameter(Mandatory = true, ParameterSetName = RepeatPingSet)] [Alias("Continues")] public SwitchParameter Repeat { get; set; } /// - /// Set short output kind ('bool' for Ping, 'int' for MTU size ...). + /// Gets or sets whether to return simplified output. /// Default is to return typed result object(s). + /// When used with standard -Ping parameter set or -TraceRoute, a simple $true/$false value /// [Parameter()] - public SwitchParameter Quiet; + public SwitchParameter Quiet { get; set; } /// /// Time-out value in seconds. From c42d82cb1ad2d211dc1232033a74a8d1e2d5275f Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 21:34:24 -0400 Subject: [PATCH 59/63] :memo: Update doc tags --- .../management/TestConnectionCommand.cs | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 4c820df2ed4..883a6d1c2f9 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -78,14 +78,14 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Gets or sets the source hostname from which to perform a test (ping, trace route, ...). - /// The default is Local Host. + /// The default is localhost. /// Remoting is not yet implemented internally in the cmdlet. /// [Parameter(ParameterSetName = PingSet)] [Parameter(ParameterSetName = RepeatPingSet)] [Parameter(ParameterSetName = TraceRouteSet)] [Parameter(ParameterSetName = TcpPortSet)] - public string Source { get; } = Dns.GetHostName(); + public string Source { get; set; } = Dns.GetHostName(); /// /// Gets or sets the number of times the Ping data packets can be forwarded by routers. @@ -147,7 +147,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Gets or sets whether to return simplified output. /// Default is to return typed result object(s). - /// When used with standard -Ping parameter set or -TraceRoute, a simple $true/$false value + /// When used with standard -Ping parameter set or -TraceRoute, a simple $true/$false value is returned. /// [Parameter()] public SwitchParameter Quiet { get; set; } @@ -175,20 +175,20 @@ public class TestConnectionCommand : PSCmdlet, IDisposable public string[] TargetName { get; set; } /// - /// Detect MTU size. + /// Gets or sets whether to detect MTU size. /// [Parameter(Mandatory = true, ParameterSetName = MtuSizeDetectSet)] [Alias("MtuSizeDetect")] public SwitchParameter DetectMtuSize { get; set; } /// - /// Do traceroute test. + /// Gets or sets whether to perform a traceroute test. /// [Parameter(Mandatory = true, ParameterSetName = TraceRouteSet)] public SwitchParameter Traceroute { get; set; } /// - /// Do tcp connection test. + /// Gets or sets whether to do a tcp connection test on the specified port. /// [ValidateRange(0, 65535)] [Parameter(Mandatory = true, ParameterSetName = TcpPortSet)] @@ -607,7 +607,6 @@ private bool InitProcessPing( if (ResolveDestination) { resolvedTargetName = hostEntry.HostName; - //hostEntry = Dns.GetHostEntry(hostEntry.HostName); } } catch (Exception ex) @@ -699,6 +698,7 @@ private PingReply DoPing( _sender.SendAsync(targetAddress, timeout, buffer, pingOptions, this); _pingComplete.Wait(); timer?.Stop(); + // Pause to let _sender's async flags to be reset properly so the next SendAsync call doesn't fail. Thread.Sleep(1); _pingComplete.Reset(); @@ -738,7 +738,7 @@ public void Dispose() public class TraceStatus { /// - /// Creates a new instance of the TraceStatus class. + /// Initializes a new instance of the class. /// /// The hop number of this trace hop. /// The PingStatus response from this trace hop. @@ -824,7 +824,7 @@ public IPStatus Status public PingReply Reply { get => _status.Reply; } /// - /// Retrieves the PingOptions used to send the ping to the trace hop. + /// Gets the PingOptions used to send the ping to the trace hop. /// public PingOptions Options { get => _status.Options; } } @@ -835,7 +835,7 @@ public IPStatus Status public class PingStatus { /// - /// Creates a new instance of the PingStatus class. + /// Initializes a new instance of the class. /// This constructor allows manually specifying the initial values for the cases where the PingReply /// object may be missing some information, specifically in the instances where PingReply objects are /// utilised to perform a traceroute. @@ -863,7 +863,7 @@ internal PingStatus( } /// - /// Creates a new instance of the PingStatus class. + /// Initializes a new instance of the class. /// /// The source machine name or IP of the ping. /// The destination machine name of the ping. @@ -934,8 +934,7 @@ internal PingStatus(string source, string destination, PingReply reply, uint pin public class PingMtuStatus : PingStatus { /// - /// Creates a new instance of the PingStatus class. - /// This constructor permits setting the MtuSize. + /// Initializes a new instance of the class. /// /// The source machine name or IP of the ping. /// The destination machine name of the ping. From a002c90ca23ecf8c98824b82dfd82eaaf300eae2 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 21:40:30 -0400 Subject: [PATCH 60/63] :recycle: remove unused variable --- .../commands/management/TestConnectionCommand.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 883a6d1c2f9..26b3d9d112d 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -257,7 +257,6 @@ private void ProcessConnectionByTCPPort(string targetNameOrAddress) try { Task connectionTask = client.ConnectAsync(targetAddress, TcpPort); - var targetString = targetAddress.ToString(); for (var i = 1; i <= TimeoutSeconds; i++) { From 3d723da4e070ea8808705b68e04b79efcf67c088 Mon Sep 17 00:00:00 2001 From: vexx32 <32407840+vexx32@users.noreply.github.com> Date: Tue, 2 Jul 2019 22:11:46 -0400 Subject: [PATCH 61/63] :white_check_mark: Update tests & formatting --- .../DefaultFormatters/PowerShellCore_format_ps1xml.cs | 9 +-------- .../Test-Connection.Tests.ps1 | 4 ++-- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs index 2354b64c6af..bedae83eefc 100644 --- a/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs +++ b/src/System.Management.Automation/FormatAndOutput/DefaultFormatters/PowerShellCore_format_ps1xml.cs @@ -299,14 +299,7 @@ private static IEnumerable ViewsOf_Microsoft_PowerShell_Co $_.Latency } ") - .AddScriptBlockColumn(@" - if ($_.BufferSize -gt 0) { - $_.BufferSize - } - else { - '*' - } - ") + .AddPropertyColumn("BufferSize") .AddPropertyColumn("Status") .EndRowDefinition() .GroupByProperty("Destination") diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 index be28f17b3b9..9b592061f64 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Test-Connection.Tests.ps1 @@ -232,8 +232,8 @@ Describe "Test-Connection" -tags "CI" { $result[0] | Should -BeOfType "Microsoft.PowerShell.Commands.TestConnectionCommand+TraceStatus" $result[0].Source | Should -BeExactly $hostName - $result[0].DestinationAddress | Should -BeExactly $realAddress - $result[0].DestinationHost | Should -BeExactly $hostName + $result[0].TargetAddress | Should -BeExactly $realAddress + $result[0].Target | Should -BeExactly $hostName $result[0].Hop | Should -Be 1 $result[0].HopAddress | Should -BeExactly $realAddress $result[0].Status | Should -BeExactly "Success" From 33a7cf8cd168477ebc3e9815403f2a4b65afc327 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 23 Aug 2019 12:46:16 -0400 Subject: [PATCH 62/63] :recycle: Remove async ping framework Bug with native exceptions was fixed in .NET Core 3-preview7 Should be safe to use synchronous ping. --- .../management/TestConnectionCommand.cs | 61 +++---------------- 1 file changed, 7 insertions(+), 54 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 26b3d9d112d..37f11b04112 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -34,8 +34,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable private const int DefaultMaxHops = 128; private readonly Ping _sender = new Ping(); - private readonly ManualResetEventSlim _pingComplete = new ManualResetEventSlim(); - private PingCompletedEventArgs _pingCompleteArgs; #region Parameters @@ -201,7 +199,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// protected override void BeginProcessing() { - _sender.PingCompleted += OnPingCompleted; + //_sender.PingCompleted += OnPingCompleted; if (ParameterSetName == RepeatPingSet) { @@ -235,14 +233,6 @@ protected override void ProcessRecord() } } - /// - /// StopProcessing implementation so we can handle Ctrl+C properly. - /// - protected override void StopProcessing() - { - _sender?.SendAsyncCancel(); - } - #region ConnectionTest private void ProcessConnectionByTCPPort(string targetNameOrAddress) @@ -313,7 +303,7 @@ private void ProcessTraceroute(string targetNameOrAddress) var pingOptions = new PingOptions(currentHop, DontFragment.IsPresent); - var timer = new Stopwatch(); + //var timer = new Stopwatch(); PingReply reply; do @@ -328,7 +318,7 @@ private void ProcessTraceroute(string targetNameOrAddress) { try { - reply = DoPing(targetAddress, timeout, buffer, pingOptions, timer); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); if (hostname == null && ResolveDestination @@ -351,7 +341,7 @@ private void ProcessTraceroute(string targetNameOrAddress) hostname, reply, pingOptions, - timer.ElapsedMilliseconds, + reply.RoundtripTime, buffer.Length, i), Source, @@ -359,7 +349,7 @@ private void ProcessTraceroute(string targetNameOrAddress) targetAddress); WriteObject(status); - timer.Reset(); + //timer.Reset(); } catch (PingException ex) { @@ -431,7 +421,7 @@ private void ProcessMTUSize(string targetNameOrAddress) CurrentMTUSize, HighMTUSize)); - reply = DoPing(targetAddress, timeout, buffer, pingOptions); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); // Cautious! Algorithm is sensitive to changing boundary values. if (reply.Status == IPStatus.PacketTooBig) @@ -531,7 +521,7 @@ private void ProcessPing(string targetNameOrAddress) { try { - reply = DoPing(targetAddress, timeout, buffer, pingOptions); + reply = _sender.Send(targetAddress, timeout, buffer, pingOptions); } catch (PingException ex) { @@ -686,49 +676,12 @@ private byte[] GetSendBuffer(int bufferSize) return sendBuffer; } - private PingReply DoPing( - IPAddress targetAddress, - int timeout, - byte[] buffer, - PingOptions pingOptions, - Stopwatch timer = null) - { - timer?.Start(); - _sender.SendAsync(targetAddress, timeout, buffer, pingOptions, this); - _pingComplete.Wait(); - timer?.Stop(); - - // Pause to let _sender's async flags to be reset properly so the next SendAsync call doesn't fail. - Thread.Sleep(1); - _pingComplete.Reset(); - - if (_pingCompleteArgs.Cancelled) - { - // The only cancellation we have implemented is on pipeline stops. - throw new PipelineStoppedException(); - } - - if (_pingCompleteArgs.Error != null) - { - throw new PingException(_pingCompleteArgs.Error.Message, _pingCompleteArgs.Error); - } - - return _pingCompleteArgs.Reply; - } - - private static void OnPingCompleted(object sender, PingCompletedEventArgs e) - { - ((TestConnectionCommand)e.UserState)._pingCompleteArgs = e; - ((TestConnectionCommand)e.UserState)._pingComplete.Set(); - } - /// /// IDisposable implementation. /// public void Dispose() { _sender?.Dispose(); - _pingComplete?.Dispose(); } /// From 236b981a3a1eba2574f2ac3e89b950a28c6657b1 Mon Sep 17 00:00:00 2001 From: Joel <32407840+vexx32@users.noreply.github.com> Date: Fri, 23 Aug 2019 14:14:41 -0400 Subject: [PATCH 63/63] :recycle: simplify attribute declarations --- .../management/TestConnectionCommand.cs | 24 ++++--------------- 1 file changed, 5 insertions(+), 19 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs index 37f11b04112..043cf0432f7 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/TestConnectionCommand.cs @@ -47,31 +47,19 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// /// Gets or sets whether to force use of the IPv4 protocol. /// - [Parameter(ParameterSetName = PingSet)] - [Parameter(ParameterSetName = RepeatPingSet)] - [Parameter(ParameterSetName = TraceRouteSet)] - [Parameter(ParameterSetName = MtuSizeDetectSet)] - [Parameter(ParameterSetName = TcpPortSet)] + [Parameter] public SwitchParameter IPv4 { get; set; } /// /// Gets or sets whether to force use of the IPv6 protocol. /// - [Parameter(ParameterSetName = PingSet)] - [Parameter(ParameterSetName = RepeatPingSet)] - [Parameter(ParameterSetName = TraceRouteSet)] - [Parameter(ParameterSetName = MtuSizeDetectSet)] - [Parameter(ParameterSetName = TcpPortSet)] + [Parameter] public SwitchParameter IPv6 { get; set; } /// /// Gets or sets whether to do reverse DNS lookup to get names for IP addresses. /// - [Parameter(ParameterSetName = PingSet)] - [Parameter(ParameterSetName = RepeatPingSet)] - [Parameter(ParameterSetName = TraceRouteSet)] - [Parameter(ParameterSetName = MtuSizeDetectSet)] - [Parameter(ParameterSetName = TcpPortSet)] + [Parameter] public SwitchParameter ResolveDestination { get; set; } /// @@ -147,7 +135,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// Default is to return typed result object(s). /// When used with standard -Ping parameter set or -TraceRoute, a simple $true/$false value is returned. /// - [Parameter()] + [Parameter] public SwitchParameter Quiet { get; set; } /// @@ -156,7 +144,7 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// It is not the cmdlet timeout! It is a timeout for waiting one ping response. /// The default (from Windows) is 5 second. /// - [Parameter()] + [Parameter] [ValidateRange(ValidateRangeKind.Positive)] public int TimeoutSeconds { get; set; } = 5; @@ -199,8 +187,6 @@ public class TestConnectionCommand : PSCmdlet, IDisposable /// protected override void BeginProcessing() { - //_sender.PingCompleted += OnPingCompleted; - if (ParameterSetName == RepeatPingSet) { Count = int.MaxValue;