ExpertAssist
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_NetworkAdapterConfig uration -Filter IPEnabled=true -ComputerName $computer | ForEach-Object -Process {$_.InvokeMethod("EnableDH CP", $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_NetworkAdapterConfig uration -ComputerName "$computers" -Filter "DHCPEnabled=TRUE"
foreach($NIC in $NICs) {$NIC.SetDNSServerSearchOr der($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.
$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_NetworkAdapterConfig
}
}
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_NetworkAdapterConfig
foreach($NIC in $NICs) {$NIC.SetDNSServerSearchOr
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.SetDNSServerSearchOrd er <<<< ($DNSServers)
+ CategoryInfo : InvalidOperation: (SetDNSServerSearchOrder:S trin
g) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull"
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.SetDNSServerSearchOrd
+ CategoryInfo : InvalidOperation: (SetDNSServerSearchOrder:S
g) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull"
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
ASKER
$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_NetworkAdapterConfig
foreach($NIC in $NICs) {$NIC.SetDNSServerSearchOr
}}