Windows Server cung cấp nhiều công cụ cho admin khi ứng dụng hoặc máy tính không kết nối được đến cổng hoặc IP server cụ thể. Ngay lập tức nghĩ đến công cụ giải quyết DNS và vấn đề mạng khác, nhưng nếu thích dùng PowerShell, có lệnh tuyệt vời Test-NetConnection.
Khắc phục sự cố kết nối mạng qua PowerShell
Dùng cmdlet Test-NetConnection để tìm thông tin chẩn đoán cho kết nối. Hỗ trợ Ping test, TCP test, route tracing, và route selection diagnostics.
Tùy thuộc tùy chọn dùng, đầu ra bao gồm: ComputerName, RemoteAddress, SelectedSourceAddress, OutgoingInterfaceIndex, SelectedNetRoute, và hơn nữa.
Danh sách tham số hỗ trợ:
- CommonTCPPort: Chỉ định số cổng TCP dịch vụ điển hình
- ComputerName: Chỉ định tên DNS hoặc IP máy đích
- ConstrainInterface: Chỉ định ràng buộc interface cho chẩn đoán tuyến đường
- ConstrainSourceAddress: Chỉ định ràng buộc địa chỉ nguồn cho chẩn đoán tuyến đường
- DiagnoseRouting: Chỉ định chạy chẩn đoán tuyến đường để xuất thông tin chọn tuyến đường và địa chỉ nguồn cho máy đích
- Hops: Chỉ định số hop trong lệnh traceroute
- InformationLevel: Chỉ định mức thông tin Detailed hoặc Quiet
- Port: Chỉ định số cổng TCP trên máy từ xa
- TraceRoute: Chỉ định chạy Tracert kiểm tra kết nối đến máy từ xa
Sử dụng cmdlet mạnh mẽ Test-NetConnection
Test-NetConnection -ComputerName "www.contoso.com" -ConstrainInterface 5 -DiagnoseRouting -InformationLevel "Detailed" ComputerName : www.contoso.com RemoteAddress : 2600:1409:a:185::2768 ConstrainInterfaceIndex : 5 SelectedSourceAddress : 2001:4898:e0:79:75dd:64cf:d9ff:f86 OutgoingInterfaceIndex : 5 SelectedNetRoute : DestinationPrefix: ::/0 NextHop: fe80::200:5eff:fe00:202 RouteSelectionEvents : IP: Route [DestinationPrefix: ::/0 NextHop: fe80::200:5eff:fe00:202 InterfaceIndex: 4 RouteMetric: 256] is blocked for Destination: 2600:1409:a:185::2768 ConstrainInterfaceIndex: 5 ConstrainScopeZone: 1 in Compartment: 1, Reason: InterfaceConstraint. SourceAddressSelectionEvents : IP: Source address 2001:4898:e0:79:75dd:64cf:d9ff:f86 is preferred over fe80::75dd:64cf:d9ff:f86 for destination 2600:1409:a:185::2768 Rule = 2.0. IP: Source address 2001:4898:e0:79:75dd:64cf:d9ff:f86 is preferred over fe80::75dd:64cf:d9ff:f86 for destination 2600:1409:a:185::2768 Rule = 2.0. RouteDiagnosticsSucceeded : True
Danh sách lệnh mẫu
Một vài lệnh mẫu thử trên máy. Một số cần quyền admin, hãy mở PowerShell 7 hoặc 5.1 với quyền cần thiết.
Test-NetConnection youtube.com
Thông tin kết nối chi tiết hơn:
Test-NetConnection youtube.com -InformationLevel "Detailed"
Khi làm việc với dịch vụ web, cần test cổng TCP cụ thể:
Test-NetConnection youtube.com -Port 443 -InformationLevel "Detailed"
Thực hiện chẩn đoán tuyến đường đến máy từ xa:
Test-NetConnection -ComputerName itopstalk.com -DiagnoseRouting -InformationLevel Detailed
Tìm cổng mặc định của website:
Test-NetConnection -ComputerName microsoft.com -CommonTCPPort HTTP
Chạy Trace Route cho website:
Test-NetConnection -ComputerName google.com -TraceRoute
Lợi ích: dùng được trên nền tảng non-Windows nhưng cùng lệnh như Windows.
Ngoài ra, nhận thấy chạy lệnh cho một số trang như Microsoft.com không hoạt động. Ping thất bại luôn, có thể do server chặn yêu cầu ngẫu nhiên.