Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 467
  • Last Modified:

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.
0
ExpertAssist
Asked:
ExpertAssist
  • 4
2 Solutions
 
ExpertAssistAuthor Commented:
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}
}}
0
 
Jamie McKillopIT ManagerCommented:
Hello,

This combines both scripts and will not change the DNS Server Search Order if DNCP isn't enabled:

$DNSServers = @()	
$computers = get-content "C:\dns\computers.txt"
foreach($computer in $computers) {
	if($computer -eq $null) {Write-Host"script has completed"} else
	{
		Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName $computer | ForEach-Object -Process {$_.InvokeMethod("EnableDHCP", $null)}
		Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=true -ComputerName $computer | ForEach-Object {
			if ($_.DHCPEnabled -eq $true) {
				$NIC.SetDNSServerSearchOrder($DNSServers)
			} Else {
				Write-Host "DHCP is not enabled on $NIC"
			}
		}
   	} 
}	

Open in new window

0
 
ExpertAssistAuthor Commented:
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"
0
 
ExpertAssistAuthor Commented:
When the script makes the change to the DHCP, the connection is lost and the script does not get to the DNSOrder Null.  You could run the DNSOrder Null first, however, there could be an issue if the DNSOrder is nulled that the setting to use DHCP does not work.  Thanks for the script jjmck.  The script runs, but given my discovery, I am planning to run the scripts seperately after all.
0
 
ExpertAssistAuthor Commented:
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.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now