2013-03-04 146 views
0

可以在启动脚本中使用ping命令来检查与网络服务器的连接吗? Powershell的启动脚本:Ping启动脚本

$Ping=new-object System.Net.NetworkInformation.Ping 
$Reply=$ping.send("MyServer01") 
if ($Reply.status() -eq "Success") 
{ 
Perform operations 
} 
Else 
{Perform other operations} 

RightNow公司,在$回复是启动脚本执行过程中的空白,但如果我运行登录后具有价值

感谢。

+0

改用'test-connection' ... – 2013-03-04 20:08:55

+0

它没有工作。如果((test-connection“MyServer01”-quiet)-eq“True”) – user2133112 2013-03-04 20:10:31

+0

不是解决方案,只是一条评论... – 2013-03-04 20:17:12

回答

0

测试连接应该工作得很好,但它不会返回字符串“True”,而是返回布尔值$ True。所以:

if (Test-Connection "MyServer01" -Quiet) { 
    Some-Command 
} else { 
    Other-Command 
} 

这对我的盒子(W2K3,Powershell 2,.NET 4.5)非常好。

PS:如果你只是运行cmd,ping MyServer01,那么它工作吗?