Convert DHCP IP to Static IP on Network Interface(s)

Logic

  1. Get the Network Adapter with DHCP enabled from Win32_networkadapterconfiguration
  2. Map MACAddress from step 1 to MACAddress from Win32_NetworkAdapter to retrieve InterfaceName
  3. Set the static IP with NETSH using InterfaceName and values from Step 1

Code

###########################################################################
#
#
NAME: DHCP-To-Static
#
#
AUTHOR: SivaMulpuru
#
#
COMMENT: Covert DHCP IP on the Network Adatper to Static
#
#
VERSION HISTORY:
#
1.0 12/7/2011 – Initial release
#
#
NOTE: Run as Admin
#
#
##########################################################################
cls
$WMINetworkAdapterConfObjs = Get-WmiObject -Query select * from Win32_networkadapterconfiguration where dhcpenabled = true and ipenabled = true

foreach ($WMINetworkAdapterConfObj in $WMINetworkAdapterConfObjs)
{

$IPAdderss = $WMINetworkAdapterConfObj.IPAddress
$IPSubnet = $WMINetworkAdapterConfObj.Ipsubnet
$DefaultIPGateway = $WMINetworkAdapterConfObj.DefaultIPGateway
$DNSServerSearchOrder = $WMINetworkAdapterConfObj.DNSServerSearchOrder
$WINSPrimaryServer = $WMINetworkAdapterConfObj.WINSPrimaryServer
$MACADDRESS = $WMINetworkAdapterConfObj.MACAddress

$WMINetworkAdapterObj = gwmi -query Select * from Win32_NetworkAdapter where MACAddress = ‘$MACADDRESS’
$InterfaceName = $WMINetworkAdapterObj.NetConnectionID

if($DNSServerSearchOrder.Length -gt 1)
{
#Set DNS
cmd.exe /c netsh interface ipv4 add dnsservers `”$InterfaceName`” $DNSServerSearchOrder[0]
cmd.exe /c
netsh interface ipv4 add dnsservers `”$InterfaceName`” $DNSServerSearchOrder[1] Index=2
}
else
{
#Set DNS
cmd.exe /c netsh interface ipv4 add dnsservers `”$InterfaceName`” $DNSServerSearchOrder Index=1
}
if($WINSPrimaryServer -ne $null)
{
#Set Wins
cmd.exe /c netsh interface ipv4 add winsservers `”$InterfaceName`” $WINSPrimaryServer
}
#Set Static IP
cmd.exe /c netsh interface ipv4 set address `”$InterfaceName`” static $IPAdderss $IPSubnet $DefaultIPGateway

}

References

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s