2025/11/12

PowerShellコマンド:Test-NetConnection 使用例

## 基本構文
```
Test-NetConnection [-ComputerName] <string> [-Port <int>] [-InformationLevel <string>] [-TraceRoute]
```

## 使用例一覧
### 1. ホストへの接続確認(Ping相当)
```
Test-NetConnection www.google.com
```

- ホスト名の名前解決と ICMP 通信確認
- PingSucceeded が True なら疎通可能

### 2. TCPポートの疎通確認(例:443)
```
Test-NetConnection -ComputerName 192.168.1.10 -Port 443
```

- 指定IPの443番ポートにTCP接続できるか確認
- TcpTestSucceeded が True ならポートが開いている

### 3. ホスト名+ポート確認(DNS解決付き)
```
Test-NetConnection -ComputerName example.com -Port 80
```

- ホスト名をDNSで解決し、HTTPポート(80)への接続確認

### 4. トレースルート付きで確認
```
Test-NetConnection www.microsoft.com -TraceRoute
```

- 通信経路(ルーターのホップ)を表示

### 5. LAN内のIPに対して443ポート確認(スクリプト)
```
$subnet = "192.168.1"
1..254 | ForEach-Object {
    $ip = "$subnet.$_"
    $result = Test-NetConnection -ComputerName $ip -Port 443 -WarningAction SilentlyContinue
    if ($result.TcpTestSucceeded) {
        Write-Host "$ip is listening on port 443"
    }
}
```

- サブネット内の全ホストに対して443ポートの疎通確認
- 成功したIPのみ表示

## 出力例
```
ComputerName     : 192.168.1.10
RemoteAddress    : 192.168.1.10
RemotePort       : 443
InterfaceAlias   : Ethernet
TcpTestSucceeded : True
```

0 件のコメント:

コメントを投稿

人気の投稿