测试网络连接给出的结果不一致



今天遇到了一个有趣的问题。

我有一堆C#代码,通过RunspacePool创建使用Powershell。 我给它输入命令,它输出的正是Powershell在你自己运行Powershell时会输出的内容,每一行都是列表中的一个新条目。

奇怪的是,今天在程序中尝试"测试网络连接"命令时,结果不一致。

命令:

Test-NetConnection -ComputerName 'D123' -InformationLevel Detailed

D123 是同一网络上的 Windows 7 VM

下面你可以看到两个结果,似乎有50%的机会得到任何一个:

Count = 32
[0]: "ComputerName             : D123"
[1]: "RemoteAddress            : fe80::9503:8d01:d690:6c0a%14"
[2]: "PingSucceeded            : True"
[3]: "PingReplyDetails         : System.Net.NetworkInformation.PingReply"
[4]: "TcpClientSocket          :"
[5]: "TcpTestSucceeded         : False"
[6]: "RemotePort               : 0"
[7]: "TraceRoute               :"
[8]: "Detailed                 : True"
[9]: "InterfaceAlias           : Internal"
[10]: "InterfaceIndex           : 14"
[11]: "InterfaceDescription     : Intel(R) 82574L Gigabit Network Connection #2"
[12]: "NetAdapter               : MSFT_NetAdapter (CreationClassName = "MSFT_NetAdapter", DeviceID ="
[13]: ""{FDA85168-53B0-4F24-ABEB-BDC9F3A29958}", SystemCreationClassName = "CIM_NetworkPort","
[14]: "SystemName = "WIN-IAJUP1S3FL9.testdomain.com")"
[15]: "NetRoute                 : MSFT_NetRoute (InstanceID = "poB:DD9@?55"
[16]: "?55DD55"
[17]: "")"
[18]: "SourceAddress            : MSFT_NetIPAddress (Name = "poB:DDl:pBD?:lDpC:mDmp?o/"
[19]: "?55"
[20]: "?55"
[21]: "55"
[22]: "", CreationClassName = "","
[23]: "SystemCreationClassName = "", SystemName = "")"
[24]: "NameResolutionSucceeded  : True"
[25]: "BasicNameResolution      : {}"
[26]: "LLMNRNetbiosRecords      : {}"
[27]: "DNSOnlyRecords           : {}"
[28]: "AllNameResolutionResults :"
[29]: "IsAdmin                  : True"
[30]: "NetworkIsolationContext  : Private Network"
[31]: "MatchingIPsecRules       :"

Count = 9
[0]: "ComputerName             : D123"
[1]: "RemoteAddress            : fe80::9503:8d01:d690:6c0a%14"
[2]: "AllNameResolutionResults : 172.16.0.10"
[3]: "fe80::9503:8d01:d690:6c0a"
[4]: "InterfaceAlias           : Internal"
[5]: "SourceAddress            : fe80::b0f8:20b:f90c:cf3e%14"
[6]: "NetRoute (NextHop)       : ::"
[7]: "PingSucceeded            : True"
[8]: "PingReplyDetails (RTT)   : 0 ms"

第二个块是它应该返回的内容,也是命令在Powershell上运行时显示的内容。

我想知道的是为什么它返回这么多值,从它的外观来看,它返回命令中所有成员的结果,而不仅仅是操作级别的成员(如果有意义,请看这里:在 Power Shell 中使用 .NET 属性),但我完全不知道为什么。

EDIT1:输出块是从Visual Studio复制+粘贴的,因此Count = 32是Powershell传回的行数,[数字]是列表位置。

编辑2:似乎更多细节来自添加" |选择 *" 到命令,这很奇怪,因为我的代码都没有这样做,并且它在 50% 的时间内获取其他详细信息。

感谢Veefu和John的帮助,我更多地研究了我的代码。

不知道为什么这样做,但是每当我在主窗口加载事件中运行Powershell代码时,它都会影响同一Powershell命令的所有后续执行。 当移动到代码的不同部分时,它的行为符合预期。

不确定为什么会发生这种情况,但从 Powershell 输出中选择特定属性更加一致,因此对于任何查看此内容的人,我建议按照 Veefu 在评论中所说的那样做。

最新更新