Powershell : remote machines static dns to DHCP

This will read an input file , and using the WMI object change any statically assigned DNS to DHCP.

 

# grabs the ListofMachines from the CSV
$ListofMachines=Get-Content “c:tempmachines.csv”
# loop starts here
foreach ($machine in $ListofMachines)
{
#Performs a release ( if needed ! use with care remotely ! )
#Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {$_.ReleaseDHCPLease()}
# query the $machine that has the DHCP enabled adapter and performs the renewal
$wmi = Get-WmiObject win32_networkadapterconfiguration -computerName $machine -filter “ipenabled =’true'”;
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $machine | Where { $_.IpEnabled -eq $true -and $_.DhcpEnabled -eq $true} | ForEach-Object -Process {ipconfig /flushdns}

}

Leave a Reply

Your email address will not be published. Required fields are marked *