Powershell命令修改本機IP爲固定地址

2019.8.24

老規矩,轉載不打招呼的至少把原文鏈接擺到最前面。另外限於本人要準備考研,今天發文只是沒管住手,半年內沒空完善,敬請諒解。


原文鏈接:https://blog.51cto.com/12078224/2432278

系統環境:Windows 10 1903 Pro


背景:在學校用機房的網線插到自己電腦上上網,但需要改爲固定的IP、默認網關,DNS也可以改。每次都從電腦右下角進入更改網絡適配器選項太麻煩了,網上能查到用命令行修改IP的辦法基本都是netsh,想用cmd結果來個這麼個提示:

圖片.png

圖一

追求技術最前線,於是乎用powershell試試,cmd的話保存成文件是bat,powershell的話文件就保存爲ps1。


正文

下圖是我的目標配置:圖片.png

圖二


下圖是我電腦裏原先的上網配置:

圖片.png

圖三


Get-NetIPAddress
https://docs.microsoft.com/en-us/powershell/module/nettcpip/get-netipaddress?view=win10-ps
根據這個文檔,可以執行Get-NetIPAddress -AddressFamily IPv4查看本地IPv4網絡配置,相當於cmd的ipconfig了:

圖片.png

這裏解釋一些參數:AddressFamily,可以設爲IPv4或IPv6;InterfaceIndex接口序號,InterfaceAlias接口別名,在網上看到的這些英文網站似乎都是以index爲12舉例的,即有線網絡的適配器;PrefixLength : 16,這個就是子網掩碼寫成/x的形式,CIDR嘛。


New-NetIPAddress
https://docs.microsoft.com/en-us/powershell/module/nettcpip/new-netipaddress?view=win10-ps

The first command adds a new IPv4 address to the network interface at index 12. The PrefixLength parameter specifies the subnet mask for the IP address. In this example, the PrefixLength of 24 equals a subnet mask of 255.255.255.0. When you add an IPv4 address, the address specified for the Default Gateway must be in the same subnet as the IPv4 address that you add.

上面英文大家應該都能看得懂,就是IP和默認網關在同一子網下。

PS C:\WINDOWS\system32> New-NetIPAddress -InterfaceIndex 12 -IPAddress 192.168.80.83 -PrefixLength 24  
圖片.png


這樣就配置完了圖三的上面第一個框,之後要配DNS才能讓電腦上網:

Set-DnsClientServerAddress
https://docs.microsoft.com/en-us/powershell/module/dnsclient/set-dnsclientserveraddress?view=win10-ps

PS C:\WINDOWS\system32> Set-DNSClientServerAddress -InterfaceIndex 12 -ServerAddresses 211.111.111.1,211.111.111.12

這樣就配完了圖三剩下DNS的兩行,上面命令裏的DNS是我瞎寫的,中間半角逗號分隔。

保存爲308.ps1:

圖片.png

powershell裏運行:.\1.ps1(測試用,改名了)

圖片.png


如果修改這個IP爲192.168.80.83,我覺得是這個操作:(沒有實現

Set-NetIPAddress
https://docs.microsoft.com/en-us/powershell/module/nettcpip/set-netipaddress?view=win10-ps

如果刪除配置的IP,

Remove-NetIPAddress
https://docs.microsoft.com/en-us/powershell/module/nettcpip/remove-netipaddress?view=win10-ps

下圖我敲了兩次回車。最終結果是刪掉了圖三的前兩行,配置了的默認網關和DNS沒有刪掉。
圖片.png


現在存在的問題:

1.powershell改網絡配置要有管理員權限,直接雙擊改不了。

https://www.windows10.pro/right-click-menu-run-as-administrator-ps1/

試了但沒用。

2.怎麼把已經配好的手動狀態換回原來的自動獲取狀態暫時還不清楚。


其他參考鏈接:

Configure Static IP Address on a Network Adapter using PowerShell
https://thebackroomtech.com/2018/04/16/configure-static-ip-address-on-network-adapter-using-powershell/


How to Change Your IP Address Using PowerShell
https://www.howtogeek.com/112660/how-to-change-your-ip-address-using-powershell/


PowerShell2.0之維護網絡(三)設置網絡適配器 - @天行健中國元素 - 博客園
https://www.cnblogs.com/fuhj02/archive/2011/01/24/1942786.html

PowerShell 4.0實現自動化設置服務器_PowerShell_腳本之家
https://www.jb51.net/article/72738.htm

Set-DnsClientServerAddress -InterfaceIndex 12 -ResetServerAddresses可以清除掉圖三DNS的配置。


PowerShell設置DHCP自動獲取IP地址 - PowerShell - 洪哥筆記
http://www.splaybow.com/post/powershell-dhcp-set.html



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章