PowerShell cmdlet Test-NetConnection 不可用

PowerShell cmdlet Test-NetConnection not available

我注意到 cmdlet Test-NetConnection 没有安装在 Server 2012 上。由于 Server 2012 附带 PowerShell 版本 3,所以我认为更新到最新版本 5.1 可能会有所帮助。

我进行了更新,但 cmdlet Test-NetConnection 仍然不可用。

只有 Test-Connection 存在,但我需要 Test-NetConnection 来测试端口。

我现在怎样才能得到Test-NetConnection

PS C:\Windows\system32> $PSVersionTable

名称值
---- -----
PS版本 5.1.14409.1005
PS版桌面
PS兼容版本 {1.0、2.0、3.0、4.0...}
构建版本 10.0.14409.1005
CLR版本 4.0.30319.34209
WSManStack 版本 3.0
PS远程协议版本 2.3
序列化版本 1.1.0.1


PS C:\Windows\system32> 获取命令测试网络连接
Get-Command:术语 'Test-NetConnection' 未被识别为 cmdlet 的名称,
函数、脚本文件或可运行的程序。检查名称的拼写,或者路径是否
已包含,请验证路径是否正确,然后重试。
在 line:1 char:1
+ 获取命令测试网络连接
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo:ObjectNotFound:(Test-NetConnection:String)[Get-Command],CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException,Microsoft.PowerShell.Commands.GetCommandCommand

许多 cmdlet 的可用性与 Windows 版本相关,而不是 PowerShell 版本。如果你不能升级你的 Windows 版本,你就不能拥有 Test-NetConnection.

您可以使用 nmap or scanline 之类的命令行端口扫描器进行端口测试,或者您可以自己连接到端口:

function Test-Port($server, $port) {
    $client = New-Object Net.Sockets.TcpClient
    try {
        $client.Connect($server, $port)
        $true
    } catch {
        $false
    } finally {
        $client.Dispose()
    }
}
$ipaddress = "serverName"
$port = "portNum"
$connection = New-Object System.Net.Sockets.TcpClient($ipaddress, $port)
if ($connection.Connected) {
     Write-Host "Success" 
} else { 
     Write-Host "Failed" 
}