# # $MediaServer_hostname_or_IPv4 = "mediaserver.domain.local" $WinAppServer_hostname_or_IPv4 = "localhost" $SQLServer_hostname_or_IPv4 = "localhost" $ContainerServer_hostname_or_IPv4 = "" # Only relevant when you are going to add Web Agent to your solution $Repeat_until_user_presses_CTRL_C = $true $Seconds_to_wait_between_repeats = 15 $Network_timeout_milliseconds = 5000 # How long to wait for a network port to answer, before the script gives up and goes to the next port Function Get_IPv4_address_from_hostname_or_IP { # If $Hostname_or_IP can be resolved to an IPv4 address, then return the resolved IPv4 address. # Otherwise return the input parameter ($Hostname_or_IP), because we will then assume that the user has entered an IP4 address. Param ($Hostname_or_IP) $ErrorActionPreference = "SilentlyContinue" $Result_DNS=Resolve-DnsName -Type A $Hostname_or_IP|select -Expand IPAddress|Select-Object -First 1 $ErrorActionPreference = "Continue" if ($Result_DNS) {return($Result_DNS)} else {return($Hostname_or_IP)} } # function Function Test_all_ports_in_ObjDictionary_on_hostname_or_IP { # Test that $Hostname_or_IP listens to all ports defined in $ObjDictionary_ports_to_test # Retrieve port numbers from $ObjDictionary_ports_to_test.Key and retrieve port decriptions from $ObjDictionary_ports_to_test.Value # Write results in a format that looks nice. Use red/green color to print errors/successes Param ($ObjDictionary_ports_to_test,$Hostname_or_IP) $IPv4_address=Get_IPv4_address_from_hostname_or_IP $Hostname_or_IP write-host "Test-NetConnection"$Hostname_or_IP.toupper()"(Resolved IPv4 address: $IPv4_address) at" (get-date) ForEach ($Port_description_key_value in $ObjDictionary_ports_to_test.GetEnumerator()) { $PortNumber_Trimmed=$Port_description_key_value.key.ToString().trim() # We're going to work around the problem that test-netconnection by default use a timeout longer than we need, and that this too long timeout can't be changed in the current version of PowerShell. # Because of that, we use start-job and a wrapper that abandon the job after a certain time has passed. # In the end of the script, we stop all remaining jobs, wait for them to finish and remove them. $Background_job=start-job -scriptblock { $global:ProgressPreference = 'SilentlyContinue' # remove annoying pop up on top of text Test-NetConnection -ComputerName $args[0] -Port $args[1] -InformationLevel "Quiet" } -ArgumentList $IPv4_address,$PortNumber_Trimmed do { start-sleep -Milliseconds 50 $Background_job_duration_milliseconds=[math]::Round(((get-date)-$Background_job.PSBeginTime).TotalMilliseconds) # To avoid clutter on screen and flashing progressbars, don't display progressbar until 2500ms has passed if ($Background_job_duration_milliseconds -gt 2500 -and $Background_job_duration_milliseconds -lt $Network_timeout_milliseconds) { write-progress -Activity "Test-netconnection to $IPv4_address port $PortNumber_Trimmed" -PercentComplete (100*$Background_job_duration_milliseconds/$Network_timeout_milliseconds) } } until ($Background_job.state -eq 'completed' -or $Background_job_duration_milliseconds -ge $Network_timeout_milliseconds) Write-Progress -Activity "Test-netconnection to $IPv4_address port $PortNumber_Trimmed" -Completed $Result_test_netconnection=receive-job $Background_job If ($Result_test_netconnection) { write-host -ForegroundColor green "*"$Port_description_key_value.value "Port"$Port_description_key_value.key": SUCCESS" } Else { if ($Background_job.state -eq 'completed') { write-host -ForegroundColor red "*"$Port_description_key_value.value "Port"$Port_description_key_value.key": ERROR" } else { write-host -ForegroundColor red "*"$Port_description_key_value.value "Port"$Port_description_key_value.key": TIMEOUT ERROR ($Network_timeout_milliseconds`ms)" } } } write-host } # function # main() # Initialize dictionary objects. Use sorted dictionaries. $MediaServerPortsToTest = New-Object 'system.collections.generic.sorteddictionary[string,string]' $SQLServerPortsToTest = New-Object 'system.collections.generic.sorteddictionary[string,string]' $WinAppServerPortsToTest = New-Object 'system.collections.generic.sorteddictionary[string,string]' $ContainerServerPortsToTest = New-Object 'system.collections.generic.sorteddictionary[string,string]' # Populate dictionary objectd with port numbers and port descriptions # You can add/delete/change the list, if you need to. # --------------------------------------------------------------------------------------- $SQLServerPortsToTest.Add( ' 1433', 'MS SQL Server default instance ') $MediaServerPortsToTest.Add( ' 5038', 'Asterisk Manager Interface (AMI) ') $MediaServerPortsToTest.Add( ' 5060', 'Asterisk Session Initiation Protocol (SIP) ') $MediaServerPortsToTest.Add( ' 22', 'Secure Shell SSH ') $MediaServerPortsToTest.Add( ' 8080', 'Apache Tomcat HTTP connector ') $WinAppServerPortsToTest.Add(' 8080', 'Apache Tomcat instance 1 HTTP connector ') $WinAppServerPortsToTest.Add(' 8443', 'Apache Tomcat instance 1 HTTPS connector ') $WinAppServerPortsToTest.Add(' 8009', 'Apache Tomcat instance 1 AJP connector ') $WinAppServerPortsToTest.Add(' 8081', 'Apache Tomcat instance 2 HTTP connector ') $WinAppServerPortsToTest.Add(' 8010', 'Apache Tomcat instance 2 AJP connector ') $WinAppServerPortsToTest.Add(' 6038', 'Queue Manager service ') $WinAppServerPortsToTest.Add('35023', 'Service Manager service ') $WinAppServerPortsToTest.Add('35020', 'ZyGo service ') $WinAppServerPortsToTest.Add('35034', 'Client Manager LAN ') $WinAppServerPortsToTest.Add('35033', 'Client Manager WAN ') # test the following ports on localhost or 127.0.0.1 only: if ($WinAppServer_hostname_or_IPv4 -like '*localhost*' -or $WinAppServer_hostname_or_IPv4 -like '*127.0.0.1*' ) { $WinAppServerPortsToTest.Add('35021', 'ZyGo internal admin LOCALHOST ') $WinAppServerPortsToTest.Add(' 8006', 'Apache Tomcat instance 2 Shutdown LOCALHOST ') $WinAppServerPortsToTest.Add(' 8005', 'Apache Tomcat instance 1 Shutdown LOCALHOST ') } $ContainerServerPortsToTest.Add(' 80', 'Web Agent service (service-web-apps) ') $ContainerServerPortsToTest.Add('19999', 'Client API CasPort ') $ContainerServerPortsToTest.Add('19998', 'Client API WebSocketPort ') # End of dictionary object population section # --------------------------------------------------------------------------------------- do { $Begin_date_last_run=get-date # Test that it is possible to connect to all the ports, but ignore the test if no hostnname or IPv4 is specified for one or more servers if ($MediaServer_hostname_or_IPv4) {Test_all_ports_in_ObjDictionary_on_hostname_or_IP $MediaServerPortsToTest $MediaServer_hostname_or_IPv4} if ($SQLServer_hostname_or_IPv4 ) {Test_all_ports_in_ObjDictionary_on_hostname_or_IP $SQLServerPortsToTest $SQLServer_hostname_or_IPv4} if ($WinAppServer_hostname_or_IPv4) {Test_all_ports_in_ObjDictionary_on_hostname_or_IP $WinAppServerPortsToTest $WinAppServer_hostname_or_IPv4} if ($ContainerServer_hostname_or_IPv4) {Test_all_ports_in_ObjDictionary_on_hostname_or_IP $ContainerServerPortsToTest $ContainerServer_hostname_or_IPv4} Write-host 'Waiting for test-netconnection jobs that run in the background to finish...' -ForegroundColor Gray get-job|stop-job Write-Host 'Cleaning up background jobs...' -ForegroundColor Gray get-job|remove-job write-host if ($Repeat_until_user_presses_CTRL_C) { write-host "------------------------------------------------------------------------------------------------" write-host "These tests will repeat every"$Seconds_to_wait_between_repeats" seconds. Press CTRL+C to stop." write-host "------------------------------------------------------------------------------------------------" write-host # Wait for the remaining time so that the script can never repeat faster than $Seconds_to_wait_between_repeats $Last_run_duration_seconds=(get-date)-$Begin_date_last_run $Last_run_duration_seconds=$Last_run_duration_seconds.TotalSeconds if ($Last_run_duration_seconds -lt $Seconds_to_wait_between_repeats) # never try to wait a negative armount of seconds { start-sleep -Seconds ($Seconds_to_wait_between_repeats-$Last_run_duration_seconds) } } } until (-not $Repeat_until_user_presses_CTRL_C) # Press CTRL+C to stop # #