2012-03-10 81 views

回答

4

Test-Connection默认会给你一个集合(4 System.Management.ManagementObject对象)。它在内部使用Win32_PingStatus WMI类。

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

输出:

System.Object[] 

所以,你可以这样做:

(Test-Connection server -Count 1).StatusCode 

或本:

(Test-Connection server)[0].StatusCode 

不要忘了,有些坪可能会失败,所以如果你只需检查一个它可能并不意味着没有合作nnection。

您也可以尝试直接调用Win32_PingStatus这样的:

Get-WmiObject -Class Win32_PingStatus -Filter "Address='server'" | Select-Object -Property Address,ResponseTime,StatusCode 
+0

那不幸的是,我已经更新了我的问题与更多的信息! – Sune 2012-03-14 12:27:04

+0

@Sune什么部分没有工作?获取您期望的财产价值或特定的代码?它为我返回属性,但尝试直接使用'Get-WmiObject'调用Win32_PingStatus来查看是否获得了不同的状态代码。我添加了示例代码。 – 2012-03-14 13:42:17

1

你为什么不只是尝试:

$r = Test-Connection server -Quiet 

$ r是一个布尔

+1

也许OP想检查21个可能的代码之一并采取适当的行动。 [使用Win32_PingStatus](http://msdn.microsoft.com/en-us/library/windows/desktop/aa394350(V = vs.85)的.aspx) – 2012-03-10 21:02:49

1
$r = test-connection computername -count 1 
$r.statuscode 

该作品。