Link to home
Start Free TrialLog in
Avatar of ExpertAssist
ExpertAssistFlag for United States of America

asked on

DNSSearchOrder--Nulling

I need to change from a static IP address to DHCP and turn DNS Search Order off on remote workstations.  I have been able to use the following script to change the network configureation to DHCP.

$computers = get-content "C:\dns\computers.txt"
foreach($computer in $computers) {
if($computer -eq $null) {Write-Host"script has completed"} else
{
#      Write-Host "Connect to $computers..."

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName $computer | ForEach-Object -Process {$_.InvokeMethod("EnableDHCP", $null)}
      }
}

Now I am trying to use the following script to change the DNSServerSearchOrder to "Null".  Actually what I would like to do is just turn the radial button on that states "obtain DNS server address automatically".  The only way I have been able get the radial button to change is to be in DHCP mode and to null the DNSServerSearchOrder.  The script I am currently working on to null the DNSServerSearchOrder is:

$computers = Get-Content "c:\DNS\computers.txt"
$DNSServers = "172.16.15.118", "172.16.15.119"
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName "$computers" -Filter "DHCPEnabled=TRUE"

foreach($NIC in $NICs) {$NIC.SetDNSServerSearchOrder($Null)}

Ultimately, I would like to put the two scripts together in one process and run them at the same time.  Bottom line is, I want to turn on DHCP and set "obtain DNS server address automatically" on if DHCP is enabled.
Avatar of ExpertAssist
ExpertAssist
Flag of United States of America image

ASKER

I found some additional information that has helped with the script to "Null" the DNS Search Order.  I had to create an empty array for the $DNSServers variable.  Here is a script that works, however, I would like to consolidate the two scripts into one script.  Also, I want to make sure that I do not end up nulling the DNS on workstations that have not yet been switched to DHCP.  For instance, if I run the script and for some reason, it does not change the IP configuration to DHCP, I don't want the script to continue running and possibly change the DNS Server Search Order to "Null".  Here is the script that I was able to get to work.  I will still need to test to verify that it will work remotely and not just on my local workstation.  

$computers = get-content "C:\DNS\computers.txt"
foreach($computer in $computers) {
if($computer -eq $null) {Write-Host"script has completed"} else
{
Write-Host "Connect to $computer..."
$DNSServers = @()
$NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $ComputerName -Filter "IPEnabled=TRUE"

foreach($NIC in $NICs) {$NIC.SetDNSServerSearchOrder($DNSServers) | out-null}
}}
ASKER CERTIFIED SOLUTION
Avatar of Jamie McKillop
Jamie McKillop
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi jjmck,

I tried the script this morning with the following results.  DHCP was enabled, but the DNS Server Search Order was not nulled.  I received a message:

"You cannot call a method on a null-valued expression.
At line:7 char:45
+                 $NIC.SetDNSServerSearchOrder <<<< ($DNSServers)
    + CategoryInfo          : InvalidOperation: (SetDNSServerSearchOrder:Strin
   g) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I chose my answer to clarify that the script that jjmck wrote does what I asked, however, because of an additional issue that I discovered, I decided to run the scripts seperately.  I also explained that reversing the order of the tasks performed in jjmck's script would resolve the issue that I discovered regarding the loss of connection when changing the DHCP.