简体   繁体   中英

PowerShell Test-Connection “On first response”

We use Citrix in our company and there are two addresses for accessing the Citrix StoreFront:

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

Link 1 only works when they're on the internal network (either locally or on VPN) and link 2 only works when they're not on the internal network. This leaves users to guess at what shortcut on their desktop they need to double click.

In order to resolve the confusion for our end users I have written this little snippet below. It works just as expected but because it depends on PING to see whether an internal server can be reached to decide... the execution is quite slow.

What I would like to do is execute the relevant block as soon as a response is received from a PING, rather than wait for all 4 PING attempts to complete. Is this possible in PowerShell?

So rather than " PING 4 times and if at least 1 response was received run the block " it's " PING 4 times and on first response run block ".

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)

}

Thanks in advance, Arbiter

This should be a nice solution to your problem:

notice I'm using Start-Process to launch the webpage in the default browser for ease of use.

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")
}

You can test 4 pings with -Count 1 and break the loop when ping is ok :

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

you can check the status code for the test-connection like:

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

But i would suggest you to check only once in that case . Make the count as 1 like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM