11 May 2017

How to convert dynamic IP configuration to static using PowerShell

For example to start running DHCP or DNS server, you need to have static IP configuration. If you already have an DHCP Server configured IP configuration, you might want to use the same IP address and DNS configuration. To do this automatically, use this script:

$nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | ogv –PassThru

#convert to static
$IPAddress = $nic.IPAddress[0]
$IPSubnet = $nic.IPSubnet[0]
$DefaultIPGateway = $nic.DefaultIPGateway
$DNSServerSearchOrder = $nic.DNSServerSearchOrder
$nic.EnableStatic($IPAddress, $IPSubnet)
$nic.SetGateways($DefaultIPGateway)
$nic.SetDNSServerSearchOrder($DNSServerSearchOrder)

#enable dhcp for ip and dns
$nic.EnableDHCP()
$nic.SetDNSServerSearchOrder()

First section is for selecting the correct network adapter. By running the second section, the current information is copied as static configuration. By running the third part, DHCP default configuration is set.

No comments:

Post a Comment