Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = "Ping TCP"
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
$form.MaximizeBox = $false
$form.StartPosition = "CenterScreen"
$labelHostname = New-Object System.Windows.Forms.Label
$labelHostname.Location = New-Object System.Drawing.Point(10, 20)
$labelHostname.Size = New-Object System.Drawing.Size(200, 20)
$labelHostname.Text = "Hostname or IP address:"
$form.Controls.Add($labelHostname)
$textboxHostname = New-Object System.Windows.Forms.TextBox
$textboxHostname.Location = New-Object System.Drawing.Point(10, 40)
$textboxHostname.Size = New-Object System.Drawing.Size(200, 20)
$form.Controls.Add($textboxHostname)
$labelPort = New-Object System.Windows.Forms.Label
$labelPort.Location = New-Object System.Drawing.Point(10, 70)
$labelPort.Size = New-Object System.Drawing.Size(100, 20)
$labelPort.Text = "Port number:"
$form.Controls.Add($labelPort)
$textboxPort = New-Object System.Windows.Forms.TextBox
$textboxPort.Location = New-Object System.Drawing.Point(10, 90)
$textboxPort.Size = New-Object System.Drawing.Size(100, 20)
$form.Controls.Add($textboxPort)
$buttonPing = New-Object System.Windows.Forms.Button
$buttonPing.Location = New-Object System.Drawing.Point(10, 120)
$buttonPing.Size = New-Object System.Drawing.Size(80, 25)
$buttonPing.Text = "Ping"
$form.Controls.Add($buttonPing)
$resultLabel = New-Object System.Windows.Forms.Label
$resultLabel.Location = New-Object System.Drawing.Point(10, 160)
$resultLabel.Size = New-Object System.Drawing.Size(200, 20)
$form.Controls.Add($resultLabel)
$buttonPing.Add_Click({
$hostname = $textboxHostname.Text
$port = $textboxPort.Text
$tcpClient = New-Object System.Net.Sockets.TcpClient
try {
$tcpClient.Connect($hostname, $port)
}
catch {
$resultLabel.Text = "Unable to connect to $hostname on port $port"
$resultLabel.ForeColor = [System.Drawing.Color]::Red
exit
}
if ($tcpClient.Connected) {
$resultLabel.Text = "Successfully connected to $hostname on port $port"
$resultLabel.ForeColor = [System.Drawing.Color]::Green
}
$tcpClient.Close()
})
$form.ShowDialog() | Out-Null