简体   繁体   English

从测试连接返回StatusCode

[英]Returning StatusCode from Test-Connection

test-connection server | 测试连接服务器| select-object * 选择对象*

returns many properties, among them StatusCode. 返回许多属性,其中包括StatusCode。

How can I return only this value? 如何只返回该值? My best guess would be 我最好的猜测是

$r = test-connection server
$r.statuscode

but that doesn't work. 但这不起作用。 How do I do it? 我该怎么做?

EDIT 编辑

http://msdn.microsoft.com/en-us/library/windows/desktop/aa394350%28v=vs.85%29.aspx has more information about the statuscode I would like to receive.. No luck yet! http://msdn.microsoft.com/zh-cn/library/windows/desktop/aa394350%28v=vs.85%29.aspx包含有关我希望收到的状态码的更多信息。

Test-Connection by default will give you a collection (4 System.Management.ManagementObject objects). 默认情况下, Test-Connection将给您一个集合(4个System.Management.ManagementObject对象)。 It uses the Win32_PingStatus WMI class internally. 它在内部使用Win32_PingStatus WMI类。

(Test-Connection server).GetType().FullName

Outputs: 输出:

System.Object[]

So you can do this: 因此,您可以执行以下操作:

(Test-Connection server -Count 1).StatusCode

or this: 或这个:

(Test-Connection server)[0].StatusCode

Don't forget, some pings may fail so if you just check one it may not necessary mean there's no connection. 别忘了,某些ping操作可能会失败,因此,如果您只检查一次,则可能没有必要,这意味着没有连接。

You could also try calling Win32_PingStatus directly like this: 您也可以尝试像这样直接调用Win32_PingStatus

Get-WmiObject -Class Win32_PingStatus -Filter "Address='server'" | Select-Object -Property Address,ResponseTime,StatusCode
$r = test-connection computername -count 1
$r.statuscode

This works. 这可行。

Why don't you just try : 您为什么不尝试:

$r = Test-Connection server -Quiet

$r is a boolean $ r是布尔值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM