Perrypackettracer / Powershell-scripts-to-use-in-an-active-directory

0 stars 0 forks source link

PowerShell script that checks the availability of remote ports on a specified server #20

Open Perrypackettracer opened 8 months ago

Perrypackettracer commented 8 months ago

Here's a PowerShell script that checks the availability of remote ports on a specified server. This can be useful for verifying network connectivity to specific services or applications.

# Define the server and port(s) to check
$Server = "example.com"
$Ports = @(80, 443, 3389)  # Specify the ports you want to check

# Loop through each port and check its availability
foreach ($Port in $Ports) {
    $Endpoint = "$Server:$Port"
    $Result = Test-NetConnection -ComputerName $Server -Port $Port

    if ($Result.TcpTestSucceeded) {
        Write-Host "Port $Port on $Server is reachable."
    } else {
        Write-Host "Port $Port on $Server is not reachable. Error: $($Result.Exception.Message)"
    }
}

Explanation:

  1. Modify the $Server variable with the name or IP address of the server you want to check.

  2. Specify the array $Ports with the list of ports you want to check for connectivity.

  3. The script uses Test-NetConnection to check the availability of each specified port on the remote server.

  4. It checks if the TCP test succeeded and displays a message accordingly.

  5. This script is handy for troubleshooting network connectivity to specific services, such as web servers (ports 80, 443) or remote desktop (port 3389).

  6. Customize the script based on the specific ports and servers you want to check.

  7. Execute the script to check the availability of the specified ports on the server. Adjust it according to your networking requirements.