Rebooting a switch is easy. Having patience enough to not frantically click the F5 button to check if the webinterface is back online is next to impossible. Here’s a quick PowerShell snippet with a solution!

isOnline?

The isOnline function uses Test-Connection every 2 seconds. The -quiet parameter is handy. It returns a $true if one of both pings are succesfull. Otherwise, it will return a $false.

Then, we log a timestamp for each time the target is switching states. A neat log for future reference!

Finally, we also display our current state in some beautiful colours. PowerShell contains a nifty little trick to update the previous line of our terminal.

Write-Host "`r"

Here you go! Have fun rebooting your networked devices.

Tip: You could also use this instead of ping -t. Sometimes I don’t like the endless stream of output in the command prompt.

function IsOnline {

    <#
.SYNOPSIS
Check if target is online or offline. Report when changing state.
.DESCRIPTION
Check if target is online or offline. Report when changing state. This will include a list of timestamps.
.EXAMPLE
IsOnline 192.168.1.123
.PARAMETER Target
    Specify target.
#>

    [CmdletBinding()]
Param(
[Parameter(Mandatory=$True, Position=1)]
        [string] $Target
    )

    while ($True) {

        $TargetPingable = Test-Connection -Quiet -count 2 $Target -ErrorAction SilentlyContinue
        $Date = Get-Date

        Switch ($TargetPingable) {
            $false  { $TargetState = "OFFLINE";         $CurrentStatusColour = "Magenta" }
            $true   { $TargetState = "ONLINE";          $CurrentStatusColour = "DarkCyan" }
            default { $TargetState = "in ERROR STATE";  $CurrentStatusColour = "Magenta" }
        }

        if ($TargetPingable -eq $TargetPingablePreviousState) {
            Write-Host -ForegroundColor $CurrentStatusColour "`r$Target is now $TargetState " -NoNewline
        }

        else {
            Write-Host -ForegroundColor DarkGray "`r$Date Targetstate: $TargetState"
            Write-Host -ForegroundColor $CurrentStatusColour "$Target is now $TargetState " -NoNewline
        }

        $TargetPingablePreviousState = $TargetPingable      
    }
}


Leave a Reply

Your email address will not be published. Required fields are marked *