Add-Type -AssemblyName PresentationFramework,  
                     PresentationCore, WindowsBase 
 
[xml]$Xaml = @" 
<Window  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:local="clr-namespace:WpfApp1" 
    Title="Testeur réseau" Height="282.927" Width="500"> 
    <Grid> 
        <Label Content="Adresse IP :" HorizontalAlignment="Left" Margin="30,20,0,0" VerticalAlignment="Top"/>
        <TextBox Name="TextBoxIP" HorizontalAlignment="Left" Height="20" Margin="130,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="90"/>
        <Label Content="Port (1-65535) :" HorizontalAlignment="Left" Margin="240,60,0,0" VerticalAlignment="Top"/>
        <TextBox Name="TextBoxPort" HorizontalAlignment="Left" Height="20" Margin="340,65,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="90"/>
        <Label Content="Ping seul :" HorizontalAlignment="Left" Margin="30,60,0,0" VerticalAlignment="Top"/> 
        <CheckBox Name="CheckBoxPing" Content="" HorizontalAlignment="Left" Margin="130,65,0,0" VerticalAlignment="Top"/> 
        <Label Content="Nombre de Pings :" HorizontalAlignment="Left" Margin="30,100,0,0" VerticalAlignment="Top"/> 
        <Slider Name="Slider" HorizontalAlignment="Left" Margin="135,100,0,0" VerticalAlignment="Top" Width="166" RenderTransformOrigin="0,0" Maximum="20" Minimum="1" LargeChange="4" SmallChange="1" TickFrequency="4" TickPlacement="Both"/>
        <TextBox Name="TextBoxSlider" IsReadOnly="True" HorizontalAlignment="Left" Height="20" Margin="365,105,0,0" TextWrapping="Wrap" Text="1" VerticalAlignment="Top" Width="32"/>
        <Button Name="ButtonTest" IsEnabled="False" Content="Test" HorizontalAlignment="Left" Margin="30,150,0,0" VerticalAlignment="Top" Width="150" Height="30"/>
        <ProgressBar Name="ProgressBar" HorizontalAlignment="Left" Height="30" Margin="200,150,0,0" VerticalAlignment="Top" Width="200"/>
        <TextBox Name="TextBoxResult" IsReadOnly="True" HorizontalAlignment="Left" Height="30" Margin="30,200,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="400"/>
    </Grid>
</Window> 
"@ 
 
$reader = New-Object System.Xml.XmlNodeReader $xaml 
$Window = [Windows.Markup.XamlReader]::Load( $reader ) 
 
#Récupération des contrôles 
$TextBoxIP = $Window.FindName('TextBoxIP') 
$TextBoxPort = $Window.FindName('TextBoxPort') 
$TextBoxSlider = $Window.FindName('TextBoxSlider') 
$CheckBoxPing = $Window.FindName('CheckBoxPing') 
$Slider = $Window.FindName('Slider') 
$ButtonTest = $Window.FindName('ButtonTest') 
$ProgressBar = $Window.FindName('ProgressBar') 
$TextBoxResult = $Window.FindName('TextBoxResult') 
 
$Slider.Add_ValueChanged( { 
   $TextBoxSlider.Text = [int]$Slider.Value  
}) 
 
$CheckBoxPing.add_Checked( { 
            $TextBoxPort.IsReadOnly = $true 
        }) 
$CheckBoxPing.add_UnChecked({ 
            $TextBoxPort.IsReadOnly = $false 
    }) 
 
# Traitement du script 
$ButtonTest.add_Click( { 
        $TextBoxResult.Text = '' 
        $Result = @() 
        if ($CheckBoxPing.IsChecked)
        { 
            $window.Cursor = [System.Windows.Input.Cursors]::Wait 
            For ($i = 1 ; $i -le $TextBoxSlider.Text; $i++)
            { 
                $ProgressBar.Value = $i / $TextBoxSlider.Text * 100 
                $Result += Test-Connection  -ComputerName $TextBoxIP.Text -Count 1 -Quiet 
            } 
            $Text = 'Réussi : {0}    |    Echec : {1}' -f   
                $Result.Where{$_ -eq $true}.Count, 
                $Result.Where{$_ -eq $false}.Count 
        } 
        elseif (([int]$TextBoxPort.Text -ge 1) -and ([int]$TextBoxPort.Text -le 65535)) 
        {
            $window.Cursor = [System.Windows.Input.Cursors]::Wait 
            For ($i = 1 ; $i -le $TextBoxSlider.Text; $i++)
            { 
                $ProgressBar.Value = $i / $TextBoxSlider.Text * 100 
                $Result += (Test-NetConnection -ComputerName $TextBoxIP.text -Port $TextBoxPort.Text).TcpTestSucceeded 
            } 
            $Text = 'Réussi : {0}    |    Echec : {1}' -f  
                $Result.Where{$_ -eq $true}.Count,  
                $Result.Where{$_ -eq $false}.Count 
        } 
        else
        { 
            $Text = "Erreur lors de la vérification du Port. La valeur doit être comprise entre 1 et 65535" 
        } 
        $Window.Cursor = [System.Windows.Input.Cursors]::Arrow 
        $TextBoxResult.Text = $Text 
    }) 

$TextBoxIP.add_TextChanged( { 
        $Pattern = '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$' 
        if ($TextBoxIP.Text -Match $Pattern ) { 
            $ButtonTest.IsEnabled = $true 
            $TextBoxResult.Text = '' 
        } 
        else { 
            $ButtonTest.IsEnabled = $false 
            $TextBoxResult.Text = "Erreur lors de la vérification de l'adresse IP. Vérifiez la syntaxe." 
        } 
    }) 

[void]$Window.ShowDialog()
