Script and AppV Repository
Script and AppV Repository
Script and AppV Repository

I have created a small PowerShell form that checks if a port on a host is reachable. The goal was to challenge me regarding PowerShell forms. I used PowerShell Studio 2020 to create the form and the code. The created code has been cleaned up as it contains a lot of overhead.

You can run the script with parameters or without. You enter the details and you see the result in seconds. The 'Test'-button is available when both the hostname and port have been filled in.  

Description Picture
You can enter both the hostname and port. The 'Test'-button is available when both the hostname and port have been filled in.
Ready for showing the results.
Success: the given hostname and port are reachable.
Failure: the given hostname and port are not reachable.
You can also run the test silently. The output is shown on the screen.
The help function.

The code:

<#
.SYNOPSIS
    Check if the given port is open on the given host.

.DESCRIPTION
    Check if the given port is open on the given host.

.EXAMPLE
    Start the application. All details will be entered in the GUI 
    ."NetworkPortTester_v01.ps1"

.EXAMPLE
    Check www.google.com on port 80 with output shown in the GUI.
    ."NetworkPortTester_v01.ps1" -Hostname www.google.com -Port 80

.EXAMPLE
    Check www.google.com on port 80 with output shown on screen.
    ."NetworkPortTester_v01.ps1" -Hostname www.google.com -Port 80 -Silent

.NOTES
    Author:  Willem-Jan Vroom
    Website: https://www.vroom.cc/
    Twitter: @TheStingPilot

v0.1:
   * Initial version.
#>

[CmdletBinding(DefaultParameterSetName = 'Default')]

Param
  (
   [Parameter(HelpMessage ='Give the hostname you want to check.')]
   [Parameter(Mandatory   = $False, ParameterSetName='Default')]
   [String] $Hostname,

   [Parameter(HelpMessage ='Give the port you want to check.')]
   [Parameter(Mandatory   = $False, ParameterSetName='Default')]
   [String] $Port,

   [Parameter(HelpMessage ='Silent run.')]
   [Parameter(Mandatory   = $False, ParameterSetName='Default')]
   [Switch] $Silent
  )

# =============================================================================================================================================
# Function block
# =============================================================================================================================================

  Function Check-NetworkAndPort
   {

    <#
    .NOTES
    ========================================================================================================================
    Created with:     Windows PowerShell ISE
    Created on:       06-January-2021
    Created by:       Willem-Jan Vroom
    Organization:     
    Functionname:     Check-NetworkAndPort
    ========================================================================================================================
    .SYNOPSIS

    This function checks the given host and port. The function returns $true or $false    

    #>

    param
     (
      [String] $HostnameToCheck,
      [String] $PortToCheck
     )

    $Global:ProgressPreference = 'SilentlyContinue'
    $ReturnValue               = Test-NetConnection -ComputerName $HostnameToCheck -Port $PortToCheck -InformationLevel Quiet -WarningAction SilentlyContinue

    Return $ReturnValue
   }


# =============================================================================================================================================
# End function block
# =============================================================================================================================================

# =============================================================================================================================================
# Forms Block
# =============================================================================================================================================

  [void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
  [void][reflection.assembly]::Load('System.Drawing,       Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')

  [System.Windows.Forms.Application]::EnableVisualStyles()
  $formNetworkPortTesterV01  = New-Object 'System.Windows.Forms.Form'
  $Button_Cancel             = New-Object 'System.Windows.Forms.Button'
  $Button_Test               = New-Object 'System.Windows.Forms.Button'
  $txtBox_Result             = New-Object 'System.Windows.Forms.TextBox'
  $txtBox_Port               = New-Object 'System.Windows.Forms.TextBox'
  $txtBox_Host               = New-Object 'System.Windows.Forms.TextBox'
  $lblPort                   = New-Object 'System.Windows.Forms.Label'
  $lblHost                   = New-Object 'System.Windows.Forms.Label'
  $txtApplicationName        = New-Object 'System.Windows.Forms.TextBox'
  $InitialFormWindowState    = New-Object 'System.Windows.Forms.FormWindowState'

  $Button_Test_Click = {
	$Button_Test.Cursor        = 'AppStarting'
	$txtBox_Result.Visible     = $false
    $txtBox_Result.Text        = ''
	$Hostname                  = $txtBox_Host.Text
	$Port                      = $txtBox_Port.Text
    
	$Result                    = Check-NetworkAndPort -HostnameToCheck $Hostname -PortToCheck $Port
	$txtBox_Result.Visible     = $true
	if ($Result)
	 {
	  $txtBox_Result.Text      = 'Success'	
      $txtBox_Result.BackColor = 'Green'
	 }
	else
	 {
	  $txtBox_Result.BackColor = 'Red'
      $txtBox_Result.Text      = 'Failed'
        		
	 }
	$Button_Test.Cursor        = 'Default'
   }
	
	$Button_Cancel_Click={
		$formNetworkPortTesterV01.Close()
   }
	
  $TextChangedAction = {
   if ($txtBox_Host.Text -and $txtBox_Port.Text)
	{
	 $Button_Test.Enabled = $true
	}
	else
	{
	 $Button_Test.Enabled = $false
	}
  }
	
   $formNetworkPortTesterV01_Load={
	$Button_Test.Enabled = $false
	$txtBox_Host.Text    = $Hostname
     $txtBox_Port.Text   = $Port
	}

  $formNetworkPortTesterV01.SuspendLayout()

  $formNetworkPortTesterV01.Controls.Add($Button_Cancel)
  $formNetworkPortTesterV01.Controls.Add($Button_Test)
  $formNetworkPortTesterV01.Controls.Add($txtBox_Result)
  $formNetworkPortTesterV01.Controls.Add($txtBox_Port)
  $formNetworkPortTesterV01.Controls.Add($txtBox_Host)
  $formNetworkPortTesterV01.Controls.Add($lblPort)
  $formNetworkPortTesterV01.Controls.Add($lblHost)
  $formNetworkPortTesterV01.Controls.Add($txtApplicationName)
  $formNetworkPortTesterV01.AutoScaleDimensions          = New-Object System.Drawing.SizeF(6, 13)
  $formNetworkPortTesterV01.AutoScaleMode                = 'Font'
  $formNetworkPortTesterV01.ClientSize                   = New-Object System.Drawing.Size(459, 193)
  $formNetworkPortTesterV01.FormBorderStyle              = 'Fixed3D'
  $formNetworkPortTesterV01.Name                         = 'formNetworkPortTesterV01'
  $formNetworkPortTesterV01.Text                         = 'Network Port Tester v0.1'
  $formNetworkPortTesterV01.add_Load($formNetworkPortTesterV01_Load)

  $Button_Cancel.DialogResult                            = 'Cancel'
  $Button_Cancel.Location                                = New-Object System.Drawing.Point(330, 151)
  $Button_Cancel.Name                                    = 'Button_Cancel'
  $Button_Cancel.Size                                    = New-Object System.Drawing.Size(75, 23)
  $Button_Cancel.TabIndex                                = 4
  $Button_Cancel.Text                                    = 'Cancel'
  $Button_Cancel.UseVisualStyleBackColor                 = $True
  $Button_Cancel.add_Click($Button_Cancel_Click)

  $Button_Test.Location                                  = New-Object System.Drawing.Point(50, 151)
  $Button_Test.Name                                      = 'Button_Test'
  $Button_Test.Size                                      = New-Object System.Drawing.Size(75, 23)
  $Button_Test.TabIndex                                  = 3
  $Button_Test.Text                                      = 'Test'
  $Button_Test.UseVisualStyleBackColor                   = $True
  $Button_Test.add_Click($Button_Test_Click)

  $txtBox_Result.BackColor                               = [System.Drawing.SystemColors]::Window 
  $txtBox_Result.Location                                = New-Object System.Drawing.Point(306, 89)
  $txtBox_Result.Name                                    = 'txtBox_Result'
  $txtBox_Result.Size                                    = New-Object System.Drawing.Size(100, 20)
  $txtBox_Result.TabIndex                                = 6
  $txtBox_Result.Visible                                 = $False
  $txtBox_Result.TextAlign                               = 'Center'
  $txtBox_Result.Font = [System.Drawing.Font]::new('Microsoft Sans Serif', '8.25', [System.Drawing.FontStyle]'Bold')

  $txtBox_Port.Location                                  = New-Object System.Drawing.Point(231, 89)
  $txtBox_Port.MaxLength                                 = 5
  $txtBox_Port.Name                                      = 'txtBox_Port'
  $txtBox_Port.Size                                      = New-Object System.Drawing.Size(62, 20)
  $txtBox_Port.TabIndex                                  = 2
  $txtBox_Port.add_TextChanged($TextChangedAction)

  $txtBox_Host.Location                                  = New-Object System.Drawing.Point(50, 90)
  $txtBox_Host.MaxLength                                 = 30
  $txtBox_Host.Name                                      = 'txtBox_Host'
  $txtBox_Host.Size                                      = New-Object System.Drawing.Size(169, 20)
  $txtBox_Host.TabIndex                                  = 1
  $txtBox_Host.add_TextChanged($TextChangedAction)

  $lblPort.AutoSize                                      = $True
  $lblPort.Location                                      = New-Object System.Drawing.Point(231, 73)
  $lblPort.Name                                          = 'lblPort'
  $lblPort.Size                                          = New-Object System.Drawing.Size(26, 13)
  $lblPort.TabIndex                                      = 2
  $lblPort.Text                                          = 'Port'

  $lblHost.AutoSize                                      = $True
  $lblHost.Location                                      = New-Object System.Drawing.Point(50, 73)
  $lblHost.Name                                          = 'lblHost'
  $lblHost.Size                                          = New-Object System.Drawing.Size(29, 13)
  $lblHost.TabIndex                                      = 1
  $lblHost.Text                                          = 'Host'

  $txtApplicationName.Location                           = New-Object System.Drawing.Point(160, 12)
  $txtApplicationName.Name                               = 'txtApplicationName'
  $txtApplicationName.ReadOnly                           = $True
  $txtApplicationName.Size                               = New-Object System.Drawing.Size(133, 20)
  $txtApplicationName.TabIndex                           = 0
  $txtApplicationName.TabStop                            = $False
  $txtApplicationName.Text                               = 'Network Port Tester'
  $txtApplicationName.TextAlign                          = 'Center'
  $formNetworkPortTesterV01.ResumeLayout()

  $InitialFormWindowState                                = $formNetworkPortTesterV01.WindowState
  $formNetworkPortTesterV01.add_Load($Form_StateCorrection_Load)
  $formNetworkPortTesterV01.add_FormClosed($Form_Cleanup_FormClosed)    

# =============================================================================================================================================
# End Forms Block
# =============================================================================================================================================

# =============================================================================================================================================
# Show the form (or not if silent)
# =============================================================================================================================================
   
  if($Silent)
   {
    if($Hostname -and $Port)
     {
      Write-Host $formNetworkPortTesterV01.Text
      $Result = Check-NetworkAndPort -HostnameToCheck $Hostname -PortToCheck $Port
      $ResultText = "success"
      if(-not($Result))
       {
        $ResultText = "failed"
       }
      Write-Host "Contacting $Hostname on port $Port = $ResultText"
     }
      else
     {
      Write-Host $formNetworkPortTesterV01.Text
      Write-Host "Invalid input: hostname and/or port are not supplied.`r`nPlease run the script again."
     }
   }
    else
   {
    [void] $formNetworkPortTesterV01.ShowDialog()
   }
Add comment