PowerShell - How to Set Static IP and Routing
Environment
Version
Introduction
Make use PowerShell to set static IP and Routing
Code
Have set this before run the script
Reference
- PowerShell
Version
- 2010-Jul-19
Introduction
Make use PowerShell to set static IP and Routing
Code
Have set this before run the script
C:\Set-ExecutionPolicy RemoteSigned -ForceIf not, would get an error, for example,
Fail C:\SetIP.ps1 cannot be loaded because the execution of scripts is disabled on this systemThis is the script to set IP and routing
#Get the adapter is on the Intranet
$NIC = Get-WmiObject Win32_NetworkAdapterConfiguration | Where {$_.DefaultIPGateway -eq "192.168.2.1"}
If ($NIC) {
#Set routing firstly as "The route addition error: The network location cannot be reached"
#This is caused the ip is changing
route add 192.168.0.0 mask 255.255.0.0 192.168.2.1
#Reset this not to be via DHCP
$NIC.EnableStatic("192.168.2.100", "255.255.255.0")
$NIC.SetGateways("192.168.2.10")
#Need an array to set DNS server
$DNSServers = "192.168.2.254", "192.168.2.253"
$NIC.SetDNSServerSearchOrder($DNSServers)
}
Reference