【问题标题】:Test-Connection output different between Windows PS 5.1 and PS Core 7.1Windows PS 5.1 和 PS Core 7.1 之间的测试连接输出不同
【发布时间】:2021-08-30 19:58:46
【问题描述】:

此代码的结果在 Windows PowerShell 5.1 和 PowerShell Core 7.1.4 之间有所不同。这是预期的吗?怎样做才能得到相同的输出?

C:>powershell -NoLogo -NoProfile -Command "Test-Connection 10.37.45.128 | Select-Object -Property Address,BufferSize,Latency,Status;$PSVersionTable.PSVersion.ToString()"

Address      BufferSize Latency Status
-------      ---------- ------- ------
10.37.45.128         32
10.37.45.128         32
10.37.45.128         32
10.37.45.128         32
5.1.17763.1852

C:>pwsh -NoLogo -NoProfile -Command "Test-Connection 10.37.45.128 | Select-Object -Property Address,BufferSize,Latency,Status;$PSVersionTable.PSVersion.ToString()"

Address      BufferSize Latency  Status
-------      ---------- -------  ------
10.37.45.128         32       0 Success
10.37.45.128         32       0 Success
10.37.45.128         32       0 Success
10.37.45.128         32       0 Success
7.1.4

更新:

正如@SantiagoSquarzon 和@js2010 所指出的,Test-Connection 在 PS Core 中返回的对象与在 Windows PS 中不同。

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus
   TypeName: Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus

C:>powershell -NoLogo -NoProfile -Command "(Test-Connection -Count 1 localhost).GetType()"

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     ManagementObject                         System.Management.ManagementBaseObject

C:>pwsh -NoLogo -NoProfile -Command "(Test-Connection -Count 1 localhost).GetType()"

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
False    False    PingStatus                               System.Object

C:>powershell -NoLogo -NoProfile -Command "Test-Connection -Count 1 localhost | Get-Member"

   TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus

Name                           MemberType     Definition
----                           ----------     ----------
PSComputerName                 AliasProperty  PSComputerName = __SERVER
Address                        Property       string Address {get;set;}
BufferSize                     Property       uint32 BufferSize {get;set;}
NoFragmentation                Property       bool NoFragmentation {get;set;}
PrimaryAddressResolutionStatus Property       uint32 PrimaryAddressResolutionStatus {get;set;}
ProtocolAddress                Property       string ProtocolAddress {get;set;}
ProtocolAddressResolved        Property       string ProtocolAddressResolved {get;set;}
RecordRoute                    Property       uint32 RecordRoute {get;set;}
ReplyInconsistency             Property       bool ReplyInconsistency {get;set;}
ReplySize                      Property       uint32 ReplySize {get;set;}
ResolveAddressNames            Property       bool ResolveAddressNames {get;set;}
ResponseTime                   Property       uint32 ResponseTime {get;set;}
ResponseTimeToLive             Property       uint32 ResponseTimeToLive {get;set;}
RouteRecord                    Property       string[] RouteRecord {get;set;}
RouteRecordResolved            Property       string[] RouteRecordResolved {get;set;}
SourceRoute                    Property       string SourceRoute {get;set;}
SourceRouteType                Property       uint32 SourceRouteType {get;set;}
StatusCode                     Property       uint32 StatusCode {get;set;}
Timeout                        Property       uint32 Timeout {get;set;}
TimeStampRecord                Property       uint32[] TimeStampRecord {get;set;}
TimeStampRecordAddress         Property       string[] TimeStampRecordAddress {get;set;}
TimeStampRecordAddressResolved Property       string[] TimeStampRecordAddressResolved {get;set;}
TimestampRoute                 Property       uint32 TimestampRoute {get;set;}
TimeToLive                     Property       uint32 TimeToLive {get;set;}
TypeofService                  Property       uint32 TypeofService {get;set;}
__CLASS                        Property       string __CLASS {get;set;}
__DERIVATION                   Property       string[] __DERIVATION {get;set;}
__DYNASTY                      Property       string __DYNASTY {get;set;}
__GENUS                        Property       int __GENUS {get;set;}
__NAMESPACE                    Property       string __NAMESPACE {get;set;}
__PATH                         Property       string __PATH {get;set;}
__PROPERTY_COUNT               Property       int __PROPERTY_COUNT {get;set;}
__RELPATH                      Property       string __RELPATH {get;set;}
__SERVER                       Property       string __SERVER {get;set;}
__SUPERCLASS                   Property       string __SUPERCLASS {get;set;}
ConvertFromDateTime            ScriptMethod   System.Object ConvertFromDateTime();
ConvertToDateTime              ScriptMethod   System.Object ConvertToDateTime();
IPV4Address                    ScriptProperty System.Object IPV4Address {get=$iphost = [System.Net.Dns]::GetHostEntry($this.addres...
IPV6Address                    ScriptProperty System.Object IPV6Address {get=$iphost = [System.Net.Dns]::GetHostEntry($this.addres...

C:>pwsh -NoLogo -NoProfile -Command "Test-Connection -Count 1 localhost | Get-Member"

   TypeName: Microsoft.PowerShell.Commands.TestConnectionCommand+PingStatus

Name           MemberType Definition
----           ---------- ----------
Equals         Method     bool Equals(System.Object obj)
GetHashCode    Method     int GetHashCode()
GetType        Method     type GetType()
ToString       Method     string ToString()
Address        Property   ipaddress Address {get;}
BufferSize     Property   int BufferSize {get;}
Destination    Property   string Destination {get;}
DisplayAddress Property   string DisplayAddress {get;}
Latency        Property   long Latency {get;}
Ping           Property   uint Ping {get;}
Reply          Property   System.Net.NetworkInformation.PingReply Reply {get;}
Source         Property   string Source {get;}
Status         Property   System.Net.NetworkInformation.IPStatus Status {get;}

【问题讨论】:

  • cmdlet 返回不同类型的对象,您需要使用计算属性在两个版本上获得相同的结果。
  • 或者您可以使用 Get-CimInstance -ClassName win32_pingstatus -Filter "Address='google.com'" 之类的东西在两个版本上获得相同的结果(未经测试)。
  • 我认为对于旧版本,您可以使用响应时间和状态码,这是一个数字。但是当主机宕机时,它就会分崩离析。命令完全不同。

标签: powershell powershell-core


【解决方案1】:

好吧,如果你能 ping 通它就可以了:

test-connection yahoo.com -count 1 | select Address, BufferSize,
  @{n='Latency'; e={$_.ResponseTime}},
  @{n='Status';  e={if($_.StatusCode -eq 0){'Success'}}}
Address   BufferSize Latency Status
-------   ---------- ------- ------
yahoo.com         32      94 Success

【讨论】:

  • 您的想法是对的,但还有一些细节需要了解。我会根据你的答案发布一个似乎在这里工作的答案。
  • 是的,此代码适用于 Windows PS 5.1,但不适用于 PS Core 7.1.4。
【解决方案2】:

此代码适用于 Windows PS 5.1 和 Core PS 7.1.4。它通过测试返回对象的成员是否为 $null 来确定它是否可以使用或是否应该使用其他成员名称。

Test-Connection localhost -Count 1 |
    Select-Object -Property Address, BufferSize,
        @{name='Latency'; e={
            if ($null -eq $_.Latency) {$_.ResponseTime}else{$_.Latency}}
        },
        @{name='Status'; e={
            if ($null -eq $_.Status) {
                if ($_.StatusCode -eq 0) {'Success'} else {'Not Success'}
            } else {
                $_.Status
            }
        }}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 2011-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-31
    相关资源
    最近更新 更多