0

Can ping command be used in a startup script to check connectivity to a network server ? Powershell startup script:

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

Rightnow, the $reply is blank during startup script execution but has values if I run after logged in.

Thanks.

4

1 回答 1

0

Test-Connection 应该可以正常工作,但它不返回字符串“True”,而是返回布尔值 $True。所以:

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

这在我的盒子(W2K3、Powershell 2、.NET 4.5)上运行良好。

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

于 2013-03-29T12:02:21.640 回答