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

ScreenConnect and NGINX

source:   Create TLS certificate folder sudo mkdir /etc/nginx/tls cd /etc/nginx/tls   Create server key…

How to Fix Duplicate Computer Account Names Issue in Active Directory

Automatic cleanup: # disableDuplicateComputers.ps1 # Version 0.0.1 $defaultPasswordPeriod=30 $disabledComputersReport='c:\disabledComputersReport.csv' function disableDuplicateComputers{ param( $lastLogonDaysExceeding=30, $disabledComputersReport='c:\disabledComputersReport.csv' )…

PowerShell: Create Registry Keys within Windows Localhost

# createRegKey.ps1 $regKeys=@( @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupEnabled'; value=0 } @{ hive='REGISTRY::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome'; name='ChromeCleanupReportingEnabled'; value=0 } ) function…
Other project: DragonCoin.com