【发布时间】: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