PowerShell 测试连接 "On first response"
PowerShell Test-Connection "On first response"
我们公司使用Citrix,访问Citrix StoreFront的地址有两个:
- 内部-access.company.com
- 外部-access.company.com
Link 1 仅在内部网络(本地或 VPN)上时有效,link 2 仅在不在内部网络上时有效。这让用户猜测他们需要双击桌面上的哪个快捷方式。
为了解决我们最终用户的困惑,我在下面写了这个小片段。它按预期工作,但因为它依赖于 PING 来查看是否可以到达内部服务器来决定...执行速度很慢。
我想做的是在收到 PING 的响应后立即执行相关块,而不是等待所有 4 次 PING 尝试完成。这在 PowerShell 中可行吗?
所以不是“PING 4 次并且如果至少收到 1 个响应运行 块”它是“PING 4 次并继续第一反应 运行 阻止 ".
if(Test-Connection -Quiet -ComputerName "10.10.10.10" -Count 2){
$url = "http://internal-access.company.com"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
}elseif(Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 4){
$url = "https://external-access.company.com"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
}else{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Unable to connect to Citrix. Please check your network connection and call the Service Desk on +44(0)207 111 1111 if you require assistance. Thank you.",0,"No network connection detected!",0x1)
}
提前致谢,
仲裁者
您可以使用 -Count 1
测试 4 个 ping 并在 ping 正常时中断循环:
for($i = 0; $i -lt 4; $i++){
if(Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 1){
$url = "https://external-access.company.com"
$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate($url)
break
}
}
#Script continues
您可以检查测试连接的状态代码,例如:
If( (Test-Connection servername -Count 1).StatusCode -eq 0)
但我建议您在那种情况下只检查一次。将计数设为 1,例如:
Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 1
这应该是解决您的问题的好办法:
注意我正在使用 Start-Process
在默认浏览器中启动网页以便于使用。
Function Test-QuickConnection($ip,$count=4,$ttl=50){
$attempts = 0
do{
$connected = Test-Connection $ip -Quiet -Count 1 -TimeToLive ([math]::Ceiling(($ttl/$count)))
} while ((++$attempts -lt $count) -and !$connected)
return $connected
}
if (Test-QuickConnection "10.10.10.10"){
Start-Process "http://internal-access.company.com"
} elseif (Test-QuickConnection "8.8.8.8"){
Start-Process "https://external-access.company.com"
} else {
Add-Type -AssemblyName "System.Windows.Forms"
[System.Windows.Forms.MessageBox]::Show("Unable to connect to Citrix")
}
我们公司使用Citrix,访问Citrix StoreFront的地址有两个:
- 内部-access.company.com
- 外部-access.company.com
Link 1 仅在内部网络(本地或 VPN)上时有效,link 2 仅在不在内部网络上时有效。这让用户猜测他们需要双击桌面上的哪个快捷方式。
为了解决我们最终用户的困惑,我在下面写了这个小片段。它按预期工作,但因为它依赖于 PING 来查看是否可以到达内部服务器来决定...执行速度很慢。
我想做的是在收到 PING 的响应后立即执行相关块,而不是等待所有 4 次 PING 尝试完成。这在 PowerShell 中可行吗?
所以不是“PING 4 次并且如果至少收到 1 个响应运行 块”它是“PING 4 次并继续第一反应 运行 阻止 ".
if(Test-Connection -Quiet -ComputerName "10.10.10.10" -Count 2){
$url = "http://internal-access.company.com"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
}elseif(Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 4){
$url = "https://external-access.company.com"
$ie = New-Object -com internetexplorer.application;
$ie.visible = $true;
$ie.navigate($url);
}else{
$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("Unable to connect to Citrix. Please check your network connection and call the Service Desk on +44(0)207 111 1111 if you require assistance. Thank you.",0,"No network connection detected!",0x1)
}
提前致谢, 仲裁者
您可以使用 -Count 1
测试 4 个 ping 并在 ping 正常时中断循环:
for($i = 0; $i -lt 4; $i++){
if(Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 1){
$url = "https://external-access.company.com"
$ie = New-Object -com internetexplorer.application
$ie.visible = $true
$ie.navigate($url)
break
}
}
#Script continues
您可以检查测试连接的状态代码,例如:
If( (Test-Connection servername -Count 1).StatusCode -eq 0)
但我建议您在那种情况下只检查一次。将计数设为 1,例如:
Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 1
这应该是解决您的问题的好办法:
注意我正在使用 Start-Process
在默认浏览器中启动网页以便于使用。
Function Test-QuickConnection($ip,$count=4,$ttl=50){
$attempts = 0
do{
$connected = Test-Connection $ip -Quiet -Count 1 -TimeToLive ([math]::Ceiling(($ttl/$count)))
} while ((++$attempts -lt $count) -and !$connected)
return $connected
}
if (Test-QuickConnection "10.10.10.10"){
Start-Process "http://internal-access.company.com"
} elseif (Test-QuickConnection "8.8.8.8"){
Start-Process "https://external-access.company.com"
} else {
Add-Type -AssemblyName "System.Windows.Forms"
[System.Windows.Forms.MessageBox]::Show("Unable to connect to Citrix")
}