最近幫朋友寫腳本,需要變更網路,這首選當然是用 PowerShell ,再來是 netsh 指令。
所以就查了這部份,以下這些腳本都需要管理者權限。
從固定IP改為DHCP
Set-NetIPInterface -InterfaceAlias 'Ethernet 2' -Dhcp Enabled
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute
從 DHCP 改為固定IP
Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress IpAddress 192.168.1.10 -PrefixLength 24 -DefaultGateway 192.168.1.1
變更固定IP
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetRoute
Get-NetIPAddress -InterfaceAlias 'Ethernet 2' | Remove-NetIpAddress
Get-NetIpAddress -InterfaceAlias 'Ethernet 2' | New-NetIpAddress ‑IpAddress 192.168.1.11 -PrefixLength 24 -DefaultGateway 192.168.1.1
重設DNS
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ResetServerAddresses
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ResetServerAddresses
設定DNS
Set-DnsClientServerAddress -InterfaceAlias "Ethernet" -ServerAddresses ("8.8.8.8","8.8.4.4")
Set-DnsClientServerAddress -InterfaceAlias "Wi-fi" -ServerAddresses ("8.8.8.8","8.8.4.4")
沒有留言:
張貼留言