PowerShell cmdlet Test-NetConnection not available
Asked Answered
W

2

13

I noticed that the cmdlet Test-NetConnection was not installed on Server 2012. Since Server 2012 comes with PowerShell version 3 so I thought it might help to update to the latest version 5.1.

I did the update but the cmdlet Test-NetConnection is still not available.

Only Test-Connection is present, but I need Test-NetConnection to test ports.

How can I get Test-NetConnection now?

PS C:\Windows\system32> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.34209
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


PS C:\Windows\system32> Get-Command Test-NetConnection
Get-Command : The term 'Test-NetConnection' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path
was included, verify that the path is correct and try again.
At line:1 char:1
+ Get-Command Test-NetConnection
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Test-NetConnection:String) [Get-Command], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand
Wellbeing answered 30/11, 2017 at 10:13 Comment(0)
C
12

The availability of many cmdlets is tied to the Windows version, not the PowerShell version. If you can't upgrade your Windows version you can't have Test-NetConnection.

You could use a commandline port scanner like nmap or scanline for port tests, or you could connect to the port(s) yourself:

function Test-Port($server, $port) {
    $client = New-Object Net.Sockets.TcpClient
    try {
        $client.Connect($server, $port)
        $true
    } catch {
        $false
    } finally {
        $client.Dispose()
    }
}
Consignment answered 30/11, 2017 at 10:31 Comment(2)
too bad. I have to stick with it for now. I find this cmdlet very convenient as no extra install is required. I very much appreciate yor function, works like a charm! Thank you!Wellbeing
is seems that the function send a SYN packet and do not wait for the ACK. So it is not as reliable as Test-NetConnection.Antiquate
N
0
$ipaddress = "serverName"
$port = "portNum"
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)
if ($connection.Connected) {
     Write-Host "Success" 
} else { 
     Write-Host "Failed" 
}
Noellanoelle answered 23/2, 2022 at 21:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.