Tramite il comando
Test-NetConnection di
PowerShell è possibile testare se una porta, di un determinato indirizzo IP, è in ascolto o meno.
Il formato del comando è il seguente:
Test-NetConnection <IP o nome> -Port <numero porta>
ad esempio:
Test-NetConnection 192.168.1.1 -Port 80
Test-NetConnection miosito.lan.local -Port 80
oppure posso fase il test tramite il tipo di protocollo (
CommonTCPPort ), ad esempio
http:
Test-NetConnection 192.168.1.1 -CommonTCPPort http
da un output simile al seguente in caso di successo:
ComputerName : 192.168.1.12
RemoteAddress : 192.168.1.12
RemotePort : 443
NameResolutionResults : 192.168.13.2
MatchingIPsecRules :
NetworkIsolationContext : Internet
IsAdmin : False
InterfaceAlias : Ethernet
SourceAddress : 192.168.1.12
NetRoute (NextHop) : 0.0.0.0
PingSucceeded : True
PingReplyDetails (RTT) : 1 ms
TcpTestSucceeded : False
o questo in caso sulla porta non sia in ascolto nessun servizio:
WARNING: Name resolution of miosito.lan.local failed
ComputerName : miosito.lan.local
RemoteAddress :
InterfaceAlias :
SourceAddress :
PingSucceeded : False
Per testare più porte in successione si può fare così:
@(80,81,443) | % { write-host "Port $_ .."; Test-NetConnection 192.168.1.1 -Port $_ }