【问题标题】:PowerShell Test-Connection "On first response"PowerShell 测试连接“第一次响应”
【发布时间】:2017-06-13 13:54:11
【问题描述】:

我们公司使用Citrix,访问Citrix StoreFront有两个地址:

  1. internal-access.company.com
  2. external-access.company.com

链接 1 仅在它们位于内部网络(本地或 VPN)上时有效,链接 2 仅在它们不在内部网络上时有效。这让用户猜测他们需要双击桌面上的哪个快捷方式。

为了解决我们最终用户的困惑,我在下面编写了这个小 sn-p。它按预期工作,但因为它依赖于 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)

}

提前致谢, 仲裁者

【问题讨论】:

    标签: powershell powershell-3.0 powershell-4.0


    【解决方案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")
    }
    

    【讨论】:

    • 我不知道Start-Process - 这要简单得多,谢谢。这是一个非常聪明的小功能,我刚刚对其进行了测试 - 它比我原来的速度至少快 800%!
    • @Arbiter 不用担心,很高兴听到它有帮助。
    【解决方案2】:

    您可以使用-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
    

    【讨论】:

    • 这是对我的问题的一个非常聪明、合乎逻辑的答案。创意 - 谢谢!
    【解决方案3】:

    您可以检查测试连接的状态代码,例如:

    If( (Test-Connection servername -Count 1).StatusCode -eq 0)
    

    但我建议您在这种情况下只检查一次。将计数设为 1,例如:

    Test-Connection -Quiet -ComputerName "8.8.8.8" -Count 1
    

    【讨论】:

    • 谢谢!不幸的是,我需要 -count 4 而不是 -count 1 以允许 ARP 在第一次尝试时过期。还是-count 1 ping 4 次?
    • count 4 将 ping 4 次。 count 1 表示仅 ping 1 次。
    • @Arbiter: start-process 会更快地完成操作。似乎有 1 个人给出了这个答案
    猜你喜欢
    • 1970-01-01
    • 2020-12-03
    • 2018-06-18
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多