圖形界面修改IP繁瑣且難批量化,netsh(Network Shell)是Windows內置的網絡配置瑞士軍刀,支援從單條命令修改IP到完整配置腳本導出/導入/切換的全鏈路操作。
netsh interface ip set address "區域連線" static 192.168.1.100 255.255.255.0 192.168.1.1 1
netsh interface ip set dns "區域連線" static 8.8.8.8 primary
netsh interface ip add dns "區域連線" 8.8.4.4 index=2
接口名稱需精確匹配(如「區域連線」、「乙太網路」、「Wi-Fi」),可用 netsh interface show interface 列出。
netsh -c interface dump > C:\NetConfig\Office_LAN.txt
生成文本腳本,包含所有接口的IP、DNS、WINS、路由等完整配置。
copy C:\NetConfig\Office_LAN.txt C:\NetConfig\Client_Site_A.txt
notepad C:\NetConfig\Client_Site_A.txt
修改腳本中的IP、子網掩碼、網關、DNS等字段,保存。
netsh -f C:\NetConfig\Client_Site_A.txt
秒級完成全網絡配置切換,無需重啟。
管理多個客戶現場、測試網段、辦公室Wi-Fi的網管,可準備:
Office.txt、Client_A.txt、Client_B.txt、Home_WiFi.txt、Lab_Net.txtSwitchNet.bat 交互式選單調用對應 netsh -fnetsh wlan connect name="SSID" 實現有線+無線全自動切換| 領域 | 代表命令 | 用途 |
|---|---|---|
| Windows防火牆 | netsh advfirewall firewall show rule name=all | 導出/導入/批量管理防火牆規則 |
| WLAN配置 | netsh wlan show profiles / netsh wlan export profile | Wi-Fi配置文件導出/導入/密碼查看 |
| IPsec策略 | netsh ipsec static show all | IPsec策略命令行管理 |
| WinHTTP代理 | netsh winhttp show proxy | 系統級代理配置(非IE代理) |
| BranchCache | netsh branchcache show status all | 分支辦公室緩存狀態查看 |
Invoke-Expression "netsh -f $ScriptPath" 納入自動化部署腳本。netsh -f 實現域環境網絡配置統一下發。掌握 netsh,讓網絡配置從「手工點點點」進化為「代碼管理、版本控制、一鍵交付」的基礎設施即代碼實踐。