Weekend Scripter: PowerShell, Network Adapters, and Domain Name System

Doctor Scripto

Dr Scripto

Summary: Learn how to use the networking Windows PowerShell DSC resource.

Microsoft Scripting Guy, Ed Wilson, is here. I have said on numerous occasions, “I love Windows PowerShell.” Today is no exception. Today I want to use the Wave 2 version of the xNetworking Module in the Windows PowerShell Desired State Configuration Resource Kit (it released in Wave 1, and it was updated in Wave 2). For example, I could use this module to set the DNS server address for all of my machines if I had a primary DNS server crash and I need to make the secondary server the primary, or if I wanted to add some new stuff to the network.

Note  Today is the seventh day in a series of blog posts about Desired State Configuration.

First I have to do something else

When I started looking at setting the IP address for my DNS server, I found that it wants to identify which network adapter will be bound to which the DNS server information. Of course, this makes sense. So I perform a quick audit of my network adapters by using the CIM cmdlets:

$cimsession = Get-CimSession -ComputerName dc1, s1, s2, c1

Get-NetAdapter -CimSession $cimsession -Physical |

Select name, pscomputername, status

When I run it, I find that all but one of my network adapters is named Ethernet. This is shown in the following image:

Image of command output

So I modify the script a bit, and rename the network adapter, as shown here:

$cimsession = Get-CimSession -ComputerName dc1, s1, s2, c1

(Get-NetAdapter -CimSession $cimsession -Physical).where{$_.name -ne 'Ethernet'} |

Rename-NetAdapter -NewName 'Ethernet' -PassThru

Everything’s cool, let’s change the DNS

So now I want to use the networking resource to change the DNS server information on all four of the computers. I decide to use parameters instead of using a configuration data file. Using parameters makes the configuration script much easier to use, and it also makes it more flexible. Parameters for a configuration look just like parameters for an advanced function, as shown here:

Configuration SetDnsIPAdddress

{

 Param (

        [string[]]$NodeName = 'localhost',

        [Parameter(Mandatory)]

        [string]$DnsIPAddress,

        [String]$InterfaceName = 'Ethernet',

        [ValidateSet('IPv4','IPv6')]

        [string]$AddressFamily = 'IPv4')

Once again, I need to ensure that the networking resource module is available to provide access to the family. I import the resource, then I can call it in the usual manner. The really neat thing is that when I call the configuration, I can modify the way it runs at run time. Here is the command:

SetDnsIPAdddress -NodeName s1, s2, c1 -DnsIPAddress '192.168.3.3' -OutputPath c:\dnsip

Start-DscConfiguration -Path c:\dnsip -Wait -Verbose

Following is the complete configuration script:

Configuration SetDnsIPAdddress

{

 Param (

        [string[]]$NodeName = 'localhost',

        [Parameter(Mandatory)]

        [string]$DnsIPAddress,

        [String]$InterfaceName = 'Ethernet',

        [ValidateSet('IPv4','IPv6')]

        [string]$AddressFamily = 'IPv4') 

        Import-DscResource -Module xNetworking 

    Node $NodeName

    {

        xDnsServerAddress DnsServerAddress

        {

            Address        = $DnsIPAddress

            InterfaceAlias = $InterfaceName

            AddressFamily  = $AddressFamily

        }

    }

    }

SetDnsIPAdddress -NodeName s1, s2, c1 -DnsIPAddress '192.168.10.1' -OutputPath c:\dnsip

Start-DscConfiguration -Path c:\dnsip -Wait -Verbose

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Doctor Scripto
Dr Scripto

Scripter, PowerShell, vbScript, BAT, CMD

Follow Dr Scripto   

0 comments

    Leave a comment