The Multifunctioning DBA

Oct 21 2008   11:32PM GMT

Update Server DNS Search Order using Windows Powershell



Posted by: Colin Smith
Tags:
Microsoft Windows
Powershell

Recently my company had a DR Test. This is a test we do a couple times a year to make sure that in case of a disaster we would still be able to put out a product. During this test we found the DNS Search Order for some of our Windows Servers was not set up to be as efficient as possible. We also decided to put a second Domain Controller at our DR site. This means that we also need to add an IP to the Servers DNS Search Order.

I thought that this would be a good opportunity to do some Powershell and get this done quick and easy. Since I know Powershell can use WMI I thought that would be a good way to attack the issue. First I needed to get a list of the servers that needed the update. In order to do this I was able to extract data from a database and put it into a CSV file. The list that I obtained, however, was not a list of only the Windows Servers at the DR location but a list of all of the company’s servers in all of our locations. That is OK though; I will just have to use Powershell to sort the list for me and only modify the DNS Search Order on servers that need to have the change made. To do this all I had to do was take advantage of the power of the import-csv comandlet. Here is what I did:

$serverlist
=
Import-Csv
“D:\Scripts\Powershell\DNS Update\Data\newserverlist.csv”

$downservers
=
“D:\Scripts\Powershell\DNS Update\Data\downserver.txt”

echo
“name, status, facility, PrSysAdmin, OS” > $downservers

foreach ($line
in
$serverlist)

{

    $name
=
$line.DevName

    $status
=
$line.status

    $facility
=
$line.Facility

    $PrSysAdmin
=
$line.PrSysAdmin

    $os
=
$line.OS

    $nic
=
$null


    ####################################################################################

    ## Sort list looking for all Active Windows Servers in the Deer Valley Datacenter ##

    ####################################################################################


    if (($status
-eq
“Active”) -and ($os
-eq
“Windows”) -and ($facility
-eq
“Deer Valley”))

    {

        ########################################################################################

        ## Ping Every Server that is in this list to verify it is reachable. If not make note ##

        ########################################################################################

        $up
=
Get-WmiObject
-Class
Win32_PingStatus
-Filter
“address=’$name’”

        $ping
=
$up.StatusCode

        echo
“`n$name is $ping`n”

        if ($ping
-eq 0)

        {

            Echo
“If server is pingable I will then get the adapters on the server and rule out the -Bak interface by IP. I will then update the DNS Search order.”

            $nic
=
Get-WmiObject
Win32_networkAdapterConfiguration
-ComputerName
$name
|
where-Object{$_.IPEnabled
-eq
“True”} |
where-Object{$_.IPAddress
-notlike
“169.*.*.*”} |
where-Object{$_.IPAddress
-notlike
“192.*.*.*”} |
where-object {$_.DHCPEnabled
-ne
“True”} |
where-Object{$_.IPAddress
-ne
“0.0.0.0″} |
where-Object{$_.IPAddress
-notlike
“10.111.25.*”} |
where-Object{$_.IPAddress
-notlike
“10.111.24.*”}

            if ($nic
-eq
$null)

            {

                $nic
=
Get-WmiObject
Win32_networkAdapterConfiguration
-ComputerName
$name
|
where-Object{$_.IPEnabled
-eq
“True”} |
where-Object{$_.IPAddress
-notlike
“169.*.*.*”} |
where-Object{$_.IPAddress
-notlike
“192.*.*.*”} |
where-object {$_.DHCPEnabled
-eq
“True”} |
where-Object{$_.IPAddress
-ne
“0.0.0.0″} |
where-Object{$_.IPAddress
-notlike
“10.111.25.*”} |
where-Object{$_.IPAddress
-notlike
“10.111.24.*”}

                $ip
=
$nic.IPAddress

                Echo
“$name has the following $ip but is set up for DHCP so this script will not modify this server” >> “D:\Scripts\Powershell\DNS Update\Data\iplist.txt”

            }

            Echo
“`n$nic will be modified on $name to the new search order”

            $ip
=
$nic.IPAddress

            echo
“$name has the following $ip” >> “D:\Scripts\Powershell\DNS Update\Data\iplist.txt”

        }

        else

        {

            #Echo “$name is down and sysadmin to notify is $PrSysAdmin”

            Echo
“$name, $status, $facility, $PrSysAdmin, $OS” >> $downservers

        }

    }

}

So this looks like a lot of code but it is not really too bad. First I am getting the list of my servers into a variable so that I can start to determine what servers are of interest to me and need to be changed. Once I have determined what servers I need to update, I then attempt to ping the server to verify that it is available for me to make the change. Some of the servers may be in a DMZ and some may just be obsolete and no longer in use. (Depends on if the administrators update the database.) If I am able to ping the server then I connect to WMI and I sort through the NICs that are on the server and determine what one I need to change. The standard for us is that one will be on our main network and has a static IP. Because of this I can determine what needs to be changed by sorting on those requirements. If a server has more then one NIC that meets that criteria then I do not want this script to change it. I want the servers administrator to take a look at it to verify that it has been set up correctly. In the above script I am just outputting to a file a list of all the servers that will be updated and a list that will not. When I actually make the change I will put in the following code in order to do the update:


$DNSServers
=
“10.111.1.36″,“10.97.65.23″,“10.111.1.88″,“10.111.1.19″

$nic
=
Get-WmiObject
Win32_networkAdapterConfiguration
-ComputerName
$servername
|
where-Object{$_.IPAddress
-notlike
“10.111.25.*”} |
where-Object{$_.IPEnabled
-eq
“True”}

$nic

foreach ($adapter
in
$nic)

{

    $adapter

    #$adapter.setDNSServerSearchOrder($DNSServers)

}

foreach ($adapter
in
$nic)

{


$adapter.setDNSServerSearchOrder($DNSServers)

}


This code will do Update the DNS Search Order with the values defined in the $DNSServers variable.

I hope that you have found this post useful and if you have any questions please let me know. Also you can check out my website at sysadminsmith.com for help with just about anything tech and see other blog posts as well.

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: