none
Do until $True RRS feed

  • Question

  • Hi,

    I found this Function on the Web that checks the RPC port on a server to see if it is UP.

    What I would like to do is run this over and over until it is UP and then quit or until I hit control C.

    I have tried a Do Until Loop but the function only runs once. Here is the function:

    function Test-Port{
    Param([string]$srv,$port=135,$timeout=1000,[switch]$verbose)
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    if(!$wait)
    {
    $tcpclient.Close()
    if($verbose){Write-Host "Connection Timeout"}
    Return $false
    }
    else
    {
    $error.Clear()
    $tcpclient.EndConnect($iar) | out-Null
    if($error[0]){if($verbose){write-host $error[0]};$failed = $true}
    $tcpclient.Close()
    }
    if($failed){Return $false
    	}else{return $true}
    }

    I think the problem is at the If(!wait) section, but I am not sure how to proceed.

    Thanks,

    OldDog

    Wednesday, April 25, 2012 8:27 PM

Answers

  • Very easy:

    function Test-Port{
    Param([string]$srv,$port=135,$timeout=1000,[switch]$verbose)
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    if(!$wait)
    {
    $tcpclient.Close()
    if($verbose){Write-Host "Connection Timeout"}
    Return $false
    }
    else
    {
    $error.Clear()
    $tcpclient.EndConnect($iar) | out-Null
    if($error[0]){if($verbose){write-host $error[0]};$failed = $true}
    $tcpclient.Close()
    }
    if($failed){Return $false
    	}else{return $true}
    }
    do { $result = Test-Port }
    until ($result)


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by OldDog1 Wednesday, April 25, 2012 8:59 PM
    Wednesday, April 25, 2012 8:33 PM

All replies

  • Very easy:

    function Test-Port{
    Param([string]$srv,$port=135,$timeout=1000,[switch]$verbose)
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    if(!$wait)
    {
    $tcpclient.Close()
    if($verbose){Write-Host "Connection Timeout"}
    Return $false
    }
    else
    {
    $error.Clear()
    $tcpclient.EndConnect($iar) | out-Null
    if($error[0]){if($verbose){write-host $error[0]};$failed = $true}
    $tcpclient.Close()
    }
    if($failed){Return $false
    	}else{return $true}
    }
    do { $result = Test-Port }
    until ($result)


    Grant Ward, a.k.a. Bigteddy

    What's new in Powershell 3.0 (Technet Wiki)

    • Marked as answer by OldDog1 Wednesday, April 25, 2012 8:59 PM
    Wednesday, April 25, 2012 8:33 PM
  • Thanks, that was easy. I modified it a bit and saved it as a script.

    function Test-Port{
    Param ([string]$srv,$port=135,$timeout=1000,[switch]$verbose)
    $ErrorActionPreference = "SilentlyContinue"
    $tcpclient = new-Object system.Net.Sockets.TcpClient
    $iar = $tcpclient.BeginConnect($srv,$port,$null,$null)
    $wait = $iar.AsyncWaitHandle.WaitOne($timeout,$false)
    if (!$wait) {
    $tcpclient.Close()
    if ($verbose){Write-Host "Connection Timeout"}
    Return $false
    } else {
    $error.Clear()
    $tcpclient.EndConnect($iar) | out-Null
    if ($error[0]){if($verbose){write-host $error[0]};$failed = $true}
    $tcpclient.Close()
    }
    if ($failed){Write-host "." -NoNewline 
    Return $false
    }
    else{return $true}
    }
    $srv = $args[0]
    do  { $result = Test-Port $srv -verbose }
    until  ($result)
    Write-host "$srv RPC is up"

    Wednesday, April 25, 2012 8:58 PM