Logic
- Get the Network Adapter with DHCP enabled from Win32_networkadapterconfiguration
- Map MACAddress from step 1 to MACAddress from Win32_NetworkAdapter to retrieve InterfaceName
- 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“
}
#
# 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“
}