# 設定編碼以防中文亂碼 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 Clear-Host Write-Host "=========================================" -ForegroundColor Cyan Write-Host " 端點網路狀態快速診斷工具 " -ForegroundColor Cyan Write-Host "=========================================" -ForegroundColor Cyan Write-Host "" $failStage = $null $ipInfo = "" # 步驟 1: 檢查 IP 位址 (辨識是否為 169.254 APIPA 斷網狀態) Write-Host "[*] 正在檢查本機 IP 設定..." -NoNewline $ipConfig = Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex $(Get-NetConnectionProfile | Select-Object -ExpandProperty InterfaceIndex) -ErrorAction SilentlyContinue $ipAddress = $ipConfig.IPAddress if (-not $ipAddress) { $ipAddress = (Get-NetIPAddress -AddressFamily IPv4 -InterfaceIndex 12 -ErrorAction SilentlyContinue | Select-Object -ExpandProperty IPAddress) } if ($ipAddress) { Write-Host " [ 成功 ]" -ForegroundColor Green Write-Host " -> 目前 IP: $ipAddress" # 檢查是否為 Windows 自動私人 IP (APIPA) if ($ipAddress -like "169.254.*") { $failStage = "IP_APIPA (DHCP 伺服器無回應,取得無效的 169.254 網段)" } } else { Write-Host " [ 失敗 ]" -ForegroundColor Red $failStage = "IP_Missing (未取得任何 IPv4 位址)" } # 步驟 2: 檢查閘道器 (Gateway) 是否通 if (-not $failStage) { Write-Host "[*] 正在檢查區域網路閘道器 (Gateway)..." -NoNewline $gateway = (Get-NetRoute -DestinationPrefix "0.0.0.0/0" -ErrorAction SilentlyContinue | Select-Object -ExpandProperty NextHop | Select-Object -First 1) if ($gateway) { $pingGw = Test-Connection -ComputerName $gateway -Count 1 -Quiet if ($pingGw) { Write-Host " [ 成功 ] ($gateway)" -ForegroundColor Green } else { Write-Host " [ 失敗 ]" -ForegroundColor Red $failStage = "Gateway_Unreachable (有取得 IP 但無法連線至閘道器,實體線路或交換器異常)" } } else { Write-Host " [ 失敗 ]" -ForegroundColor Red $failStage = "Gateway_NotFound (找不到預設閘道器)" } } # 步驟 3: 檢查外網 IP (8.8.8.8) 是否通 if (-not $failStage) { Write-Host "[*] 正在檢查網際網路連線 (8.8.8.8)..." -NoNewline $pingExt = Test-Connection -ComputerName "8.8.8.8" -Count 1 -Quiet if ($pingExt) { Write-Host " [ 成功 ]" -ForegroundColor Green } else { Write-Host " [ 失敗 ]" -ForegroundColor Red $failStage = "External_Network_Fail (閘道器通,但無法連線至外網,可能路由器斷線或外線掉線)" } } # 步驟 4: 檢查 DNS 解析是否正常 if (-not $failStage) { Write-Host "[*] 正在檢查 DNS 解析 (google.com)..." -NoNewline try { $resolve = Resolve-DnsName -Name "google.com" -ErrorAction Stop Write-Host " [ 成功 ]" -ForegroundColor Green } catch { Write-Host " [ 失敗 ]" -ForegroundColor Red $failStage = "DNS_Fail (外網可通,但無法解析網域名稱,請檢查 DNS 設定)" } } Write-Host "" Write-Host "=========================================" -ForegroundColor Cyan if (-not $failStage) { Write-Host " 檢測結果:網路完全正常! (OK)" -ForegroundColor Green } else { Write-Host " 檢測結果:網路異常" -ForegroundColor Red Write-Host " 故障點:$failStage" -ForegroundColor Yellow } Write-Host "=========================================" -ForegroundColor Cyan Write-Host "" # 關鍵:強制畫面停留,等待人工按 Enter 才會關閉 Read-Host "請檢視上方結果,按下 Enter 鍵關閉視窗..."