How to enable dhcp and configure dns on remote machines

There may come a time when a group of machines with static ip's need to be set to DHCP, this could be a time consuming pain if there is many of them and worse still if the machines are at a remote location. This script will force DHCP on the remote machine and tell it to get it's DNS settings from the DHCP server

Make a list

First we need our list of computers, this can be from a csv or in this example imported from AD


#Importthecomputerlist#
$computers=Get-ADComputer-Filter*


Start a foreach loop

Now we run the list of machines through a foreach loop


foreach ($comp in $computers) {


Find the network card

This line will find the network cards of the remote machine but only the cards whcih are currently active


$Netcard = Get-WMIObject Win32_NetworkAdapterConfiguration -computername $comp.name | where{$_.IPEnabled -eq TRUE}


Set DNS

This line simply tells the card to use the DHCP settings given by the DHCP server


$netcard.SetDNSServerSearchOrder()


Set DHCP

Now we apply DHCP to all the active cards, you may not want to apply DHCP to all the cards but without knowing more about the specific card it is going to be tricky to nail just one card in the machine.


$netcard | ForEach-Object -Process {$_.EnableDHCP()} }


Cut - Paste - Edit

This is the full script, in this example I have also included a small foreach loop which will check that all the network cards are not set to DHCP


#Importthecomputerlist#
$computers=Get-ADComputer-Filter*



#ChangetoDHCPandAUTODNS

foreach($compin$computers){

$Netcard=Get-WMIObjectWin32_NetworkAdapterConfiguration-computername$comp.name|where{$_.IPEnabled-eqTRUE}

$netcard.SetDNSServerSearchOrder()

$netcard|ForEach-Object-Process{$_.EnableDHCP()}}







#Checktheconfigonallchangedmachines

foreach($compin$computers){

echo$comp.name
Get-WMIObjectWin32_NetworkAdapterConfiguration-computername$mc.ip|where{$_.IPEnabled-eqTRUE}|where{$_.DHCPEnabled-eqTRUE}|`
selectDHCPEnabled,DNSDomain

}


Comments
// Collect comments ''
Search