retry logic when script returns an error

Good Afternoon
If anyone can help me clean up the below script, as of right now i have the script doing the below
1) force killing a service called bbcomm and bbgrouter
2) starting up the bbgrouter service 
3) telnet port 4321

but as you can see all im doing for the telnet is just pasting the command 6 times in a row, Is there a way that powershell can run the command again if the telnet connection is not up and have it retry 6 times.

Thank you for your time, I greatly appreciate anyone's help.


$pcnames = Read-Host -Prompt 'What is the name of the machine that is having issues with bbgrouter. Use a comma when entering more computer names.'
$pcnames -split ',' | % { Invoke-Command $_ -ScriptBlock { Get-Process bbgrouter,bbcomm | Kill -F }} ;
Get-Service -Name bbgrouter -ComputerName $pcnames | Set-Service -Status Running

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Start-Sleep -s 3

$servers = $pcnames
$portToCheck = '4321'

foreach ($server in $servers) {

If ( Test-Connection $server -Count 1 -Quiet) {

try {
$null = New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck
$props = @{
Server = $server
PortOpen = 'Yes'
}
}

catch {
$props = @{
Server = $server
PortOpen = 'No'
}
}
}

Else {

$props = @{
Server = $server
PortOpen = 'Server did not respond to ping'
}
}

New-Object PsObject -Property $props

}

Read-Host -Prompt "Press Enter to exit"

  • I see that you have posted this twice, I assume to hit a wider audience.
    I have responded to both of your posts similarly.

    Remember there are always more elegant ways to achieve given use case, the below is just an idea for you to try.

    That is a lot of code, for your use case.
    Test defined port 
    On fail to connect, max retry 6 times

    $servers = @("$env:COMPUTERNAME",'L201T')
    $RetryCounter = 6


    foreach ($server in $servers)
    {
        If (( Test-NetConnection -ComputerName $server -Port '3389' ).PingSucceeded)
        {
            "$server is avaialble and responding on the defined port"
        }
        Else
        {
            While ($RetryCounter -ne 0)
            {
                $RetryCounter -= 1
                Write-Warning -Message "$server is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: $RetryCounter"

                If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
                {
                    "$server is available and responding on the defined port"
                }
                Else
                {
                    Write-Warning -Message "$server is still not available or not responding on the defined port"
                }
            }
        }
    }



    But, since you are on a legacy OS and PowerShell version, you'll not have the
    necessary modules for the Test-NetConnection cmdlet, Hence the reason for your
    entry...

    New-Object System.Net.Sockets.TCPClient -ArgumentList $server,$portToCheck

    But nothing stops you from fixing that for Win7 PowerShell usability.

    You don't have to install modules to use them.

    You can do this by using Implicit PowerShell remoting to proxy cmdlets from a
    higher OS version and hopefully by extension, higher PowerShell version, then
    executing in using the proxied set of cmdlets, via you downlevel client....
    Or take an approach like this...

    Now, note this is quick and dirty, but shoudl give you the direction you
    require to satisfy your use case.

    So, digest and modify as fits your needs.

     

    Function Test-CustomNetConnection($ComputerName, $Port)
    {
        $ErrorActionPreference = 'SilentlyContinue'
        New-Object System.Net.Sockets.TCPClient -ArgumentList $ComputerName,$Port
        $ErrorActionPreference = 'Continue'
    }

    $servers = @("$env:COMPUTERNAME",'L201T')
    $RetryCounter = 6


    foreach ($server in $servers)
    {
        If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
        {
            Write-Host "$server is available and responding on the defined port" -ForegroundColor Green
        }
        Else
        {
            While ($RetryCounter -ne 0)
            {
                $RetryCounter -= 1
                Write-Warning -Message "$server is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: $RetryCounter"

                If (( Test-CustomNetConnection -ComputerName $server -Port '3389'))
                {
                    "$server is avaialble and responding on the defined port"
                    Write-Host "$server is now available and responding on the defined port" -ForegroundColor Green
                }
                Else
                {
                    Write-Host "`t`t`t`tStill no response from server or defined port." -ForegroundColor Gray
                }
            }
        }
    }

     

    # results

    LP70 is available and responding on the defined port
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 5
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 4
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 3
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 2
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 1
        Still no response from server or defined port.
    WARNING: L201T is not available or not responding on the defined port
                    Retrying the connection attempt. Retrying attempt countdown: 0
        Still no response from server or defined port.