我注意到Server 2012上没有安装cmdlet Test-NetConnection
。由于Server 2012附带PowerShell版本3,所以我认为更新到最新版本5.1可能有所帮助。
我进行了更新,但cmdlet Test-NetConnection
仍然无效。
仅存在Test-Connection
,但我需要Test-NetConnection
来测试端口。
我现在如何获得Test-NetConnection
?
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
答案 0 :(得分:2)
许多cmdlet的可用性与Windows版本相关,而不是PowerShell版本。如果您无法升级Windows版本,则无法使用Test-NetConnection
。
您可以使用命令行端口扫描程序(如nmap或scanline进行端口测试,或者您可以自己连接到端口:
function Test-Port($server, $port) {
$client = New-Object Net.Sockets.TcpClient
try {
$client.Connect($server, $port)
$true
} catch {
$false
} finally {
$client.Dispose()
}
}