Posted On April 12, 2019

PowerShell Legacy Versions: How to Check Connection of Servers on Certain Ports?

kimconnect 0 comments
blog.KimConnect.com >> Windows >> PowerShell Legacy Versions: How to Check Connection of Servers on Certain Ports?

A more updated version is available here.

function Check-NetConnection($server, $port) {
    $session = New-Object System.Net.Sockets.TcpClient;
    $result=$false
    try {
        $session.Connect($server, $port);
        $result=$true;
    } catch {
        $result=$false;
    } finally {
        $session.Close();
       
    }
    return $result;
}
check-netconnection -server 'google.com' -port 443

PowerShell Version 4.0 is required to have access to a function named “Test-NetConnection.” Windows Server 2003, 2008, and 2012 cannot use such cmdlet. This is only available on Windows 8.1 / 2012 R2 and higher. Dread not, here is a work-around:

# Change this variable
$servers=@(
@("kimconnect.com","443"),
@("toyota.com","443"),
@("ajgiarionafonorijiafaerksdajraoe.uga","6532")
);


function Check-NetConnection($server, $port) {
$session = New-Object System.Net.Sockets.TcpClient;
try {
$session.Connect($server, $port);
$true;
} catch {
$false;
} finally {
$session.Close();
}
}

$servers | ForEach {Check-NetConnection "$_[0]" "$_[1]";}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

Pihole Error: Tried 100 Times to Connect to FTL Server

Error Message: DataTables warning: table id=all-queries - Tried 100 times to connect to FTL server,…

PowerShell: Discover Domains and Trusts in Microsoft Windows Environment

PS C:\Windows\system32> Get-ADTrust -Filter *Direction : BiDirectionalDisallowTransivity : FalseDistinguishedName : CN=kimconnectschools.org,CN=System,DC=kimconnect,DC=k12,DC=ca,DC=usForestTransitive : TrueIntraForest : FalseIsTreeParent…

Current List of Secure Browsers

PCI compliance requires that SSLv2, SSLv3 and TLS 1.0   Google Chrome – version 30 or…