WPK PowerShell network checker

Currently at the 26C3 and the network is pretty flaky here. At least on the first day. Out of necessity a friend implemented a simple network connectivity checker as a Windows Sidebar gadget. I started playing around a bit with WPK and decided to do the same in PowerShell with a nice WPK interface.

After a long learning process and much trial and error the following came out:

Import-Module WPK

New-Window -AllowsTransparency -WindowStyle None -Background Transparent -SizeToContent WidthAndHeight -Topmost -BitmapEffect { New-DropShadowBit­mapEffect -Opacity .5 } {
    New-StackPanel -Orientation Horizontal {
        New-Border -Width 40 -Margin ‚5,5,0,10‘ -Child {
            New-StackPanel {
                New-TextBlock ‚4‘ -FontSize 24 -Foreground White -HorizontalAlig­nment Center
                New-TextBlock -On_Loaded { Set-Variable tb4 $this } -Foreground ‚#80ffffff‘ -HorizontalAlig­nment Center
            } -On_Loaded { Set-Variable c4 $this }
        } -CornerRadius ‚20,0,0,0‘ -BorderThickness ‚2,2,0,2‘ -BorderBrush Black -On_Loaded { Set-Variable -Name v4 -Value $this }
        New-Border -Width 40 -Margin ‚0,5,10,10‘ -Child {
            New-StackPanel {
                New-TextBlock ‚6‘ -FontSize 24 -Foreground White -HorizontalAlig­nment Center
                New-TextBlock -On_Loaded { Set-Variable tb6 $this } -Foreground ‚#80ffffff‘ -HorizontalAlig­nment Center
            } -On_Loaded { Set-Variable c6 $this }
        } -CornerRadius ‚0,20,0,0‘ -BorderThickness ‚0,2,2,2‘ -BorderBrush Black -On_Loaded { Set-Variable -Name v6 -Value $this }
    }
} -On_Loaded {
    $block = {
        $net4 = 1
        try { $x = @(Test-Connection google.com -Count 1) } catch { $net4 = 0 }
        if ($x.Count -eq 0) { $net4 = 0 }
       
        $net6 = 1
        try { $x = @(Test-Connection ipv6.google.com -Count 1) } catch { $net6 = 0 }
        if ($x.Count -eq 0) { $net6 = 0 }
       
        switch ($net4) {
            1 {
                $v4.Background = ‚Green‘
                $tb4.Text = $x[0].Response­Time
                $tb4 | Move-Control -fadeIn -duration ([TimeSpan]::From­MilliSeconds(500))
            }
            0 { $v4.Background = ‚Red‘
                $tb4 | Move-Control -fadeOut -duration ([TimeSpan]::From­MilliSeconds(500))
            }
            2 { $v4.Background = ‚Gold‘ }
        }
        switch ($net6) {
            1 { $v6.Background = ‚Green‘
                $tb6.Text = $x[0].Response­Time
                $tb6 | Move-Control -fadeIn -duration ([TimeSpan]::From­MilliSeconds(500))
            }
            0 { $v6.Background = ‚Red‘
                $tb6 | Move-Control -fadeOut -duration ([TimeSpan]::From­MilliSeconds(500))
            }
            2 { $v6.Background = ‚Gold‘ }
        }
    }
    Register-PowerShellCommand -ScriptBlock $block -Run -In ‚0:0:1‘
} -On_MouseRightBut­tonUp {
    $Window.Close()
} -On_MouseLeftBut­tonDown {
    $window.DragMo­ve()
} -AsJob

To use it you have to install the PowerShellPack which includes WPK, the WPF PowerShell Toolkit.

The window looks as follows:

Network Checker

The two halves „4“ and „6“ correspond to IPv4 and IPv6 connectivity, respectively. The program tries pinging google.com and ipv6.google.com. If a half turns red this means that the ping failed. Below the IP version number is the last response time.

Kommentare

Neuen Kommentar abgeben

The content of this field is kept private and will not be shown publicly.