如何通过脚本手动触发Windows NCSI的IPv4/IPv6连通性检查?
手动触发Windows NCSI立即检查IPv4/IPv6连通性
现有查询方法
你已经可以通过以下PowerShell命令获取NCSI判定的IPv4/IPv6连通状态:
powershell -NonInteractive -NoProfile -Command '(Get-NetConnectionProfile).IPv4Connectivity; (Get-NetConnectionProfile).IPv6Connectivity'
当两个版本均连通时,返回结果为:
Internet Internet
手动触发NCSI即时检查的脚本方案
由于系统默认NCSI检查频率较低,可通过以下脚本化方法强制触发即时检查,无需重启网络组件:
方法1:调用系统原生API触发刷新
通过PowerShell调用nsi.dll中的函数,直接触发网络状态刷新,进而让NCSI重新检查连通性:
Add-Type @" using System; using System.Runtime.InteropServices; public class NCSIHelper { [DllImport("nsi.dll", SetLastError = true)] public static extern uint NSI_RefreshNetworkList(); } "@ # 触发刷新 [NCSIHelper]::NSI_RefreshNetworkList()
方法2:更新网络连接配置文件触发检查
通过修改并保存当前网络连接配置文件的微小属性,间接触发NCSI重新检测:
Get-NetConnectionProfile | ForEach-Object { # 仅修改描述字段(无实际影响,仅触发更新) $currentDesc = $_.Description $_.Description = $currentDesc $_ | Set-NetConnectionProfile -ErrorAction SilentlyContinue }
方法3:重启Network List Service(轻量级服务重启)
如果上述方法无效,可重启负责网络列表管理的服务,该操作不会中断现有网络连接:
Restart-Service -Name netprofm -Force
验证效果
执行触发命令后,立即运行你原本的查询命令,即可获取最新的连通性状态:
powershell -NonInteractive -NoProfile -Command '(Get-NetConnectionProfile).IPv4Connectivity; (Get-NetConnectionProfile).IPv6Connectivity'
内容的提问来源于stack exchange,提问作者Philippe Cerfon
相关产品推荐
相关产品推荐

