Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

MTU

Powershell MTU Check

Add-Type -AssemblyName System.Windows.Forms

$form = New-Object System.Windows.Forms.Form
$form.Text = 'MTU Tester'
$form.Size = New-Object System.Drawing.Size(400,200)

$labelMinMTU = New-Object System.Windows.Forms.Label
$labelMinMTU.Text = 'Min MTU:'
$labelMinMTU.Location = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($labelMinMTU)

$labelMaxMTU = New-Object System.Windows.Forms.Label
$labelMaxMTU.Text = 'Max MTU:'
$labelMaxMTU.Location = New-Object System.Drawing.Point(10,40)
$form.Controls.Add($labelMaxMTU)

$labelTarget = New-Object System.Windows.Forms.Label
$labelTarget.Text = 'Target:'
$labelTarget.Location = New-Object System.Drawing.Point(10,70)
$form.Controls.Add($labelTarget)

$textMinMTU = New-Object System.Windows.Forms.TextBox
$textMinMTU.Text = '1000'
$textMinMTU.Location = New-Object System.Drawing.Point(120,10)
$textMinMTU.Width = 150
$form.Controls.Add($textMinMTU)

$textMaxMTU = New-Object System.Windows.Forms.TextBox
$textMaxMTU.Text = '12000'
$textMaxMTU.Location = New-Object System.Drawing.Point(120,40)
$textMaxMTU.Width = 150
$form.Controls.Add($textMaxMTU)

$textTarget = New-Object System.Windows.Forms.TextBox
$textTarget.Text = '45.55.116.11'
$textTarget.Location = New-Object System.Drawing.Point(120,70)
$textTarget.Width = 150
$form.Controls.Add($textTarget)

$buttonRun = New-Object System.Windows.Forms.Button
$buttonRun.Text = 'Run'
$buttonRun.Location = New-Object System.Drawing.Point(10,100)
$buttonRun.Add_Click({
    $minMTU = [int]$textMinMTU.Text
    $maxMTU = [int]$textMaxMTU.Text
    $target = $textTarget.Text

    while ($minMTU -lt $maxMTU) {
        $midMTU = [math]::Floor(($minMTU + $maxMTU) / 2)
        $pingResult = & ping -f -l $midMTU -n 1 $target | Select-String "Reply"
        if ($null -ne $pingResult) {
            $minMTU = $midMTU + 1
        } else {
            $maxMTU = $midMTU - 1
        }
    }

    [System.Windows.Forms.MessageBox]::Show("Maximum MTU size: $($maxMTU)", "Result")
})
$form.Controls.Add($buttonRun)

$form.ShowDialog()