none
Powershell script for adding additional DNS servers for specific NIC's RRS feed

  • Question

  • Hi Folks,

    I came up with a requirement of adding additional DNS servers for specific NIC's with specific IP addresses. I wrote this script which pretty much does the job by the interactive way but has <g class="gr_ gr_515 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="515" id="515">i'm</g> just trying to find out if there is any other finer way to do it and run the script on a fleet of servers. 

    Statement: All the servers (2016, 2012, 2012 R2) with Adapters having an IP address of 10.X.X.X should have 4 DNS servers added.

    The script which I've written is as below.

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} </style>

    Set-Item WSMan:\localhost\Client\TrustedHosts "10.x.x.x, 10.x.x.x, 10.x.x.x”

    $Cred = Get-Credential # store the cred in a vra

    Invoke-Command -ComputerName 10.x.x.x, 10.x.x.x, 10.x.x.x -Credential $Cred -ScriptBlock {

        Get-NetIPConfiguration 

        get-netadapter

    Set-DnsClientServerAddress -InterfaceAlias “Ethernet” -ServerAddresses 10.x.x.x, 10.x.x.x, 173.x.x.x, 10.x.x.x

    }

    Please let me know if this can be improved by just filtering the adapters having IP addresses of 10.x.x.x should have these DNS set

    Can <g class="gr_ gr_910 gr-alert gr_tiny gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace" data-gr-id="910" id="910">i</g> use something like 

    <style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px 'Helvetica Neue'; color: #454545} </style>

     Get-NetIPConfiguration | Get-NetIPAddress  and filter the IP's with 10.x.x.x and have DNS set only for this.

    Any suggestions would be of great help and learning.

    Thanks

    Shashi Kumar G

    Tuesday, July 31, 2018 8:04 AM

Answers

  • Hi Shashi,

    The following cmdlets should achieve what you are asking for.

    Get-NetIPAddress |
        Where-Object IPAddress -like "10.*" |
        Get-NetAdapter |
        Set-DnsClientServerAddress -ServerAddresses 10.x.x.x, 10.x.x.x, 173.x.x.x, 10.x.x.x

    You can add this in the Invoke-Command Scriptblock.

    Would be pleased to know if it was helpful.

    Best regards,

    John




    • Edited by John Seerden Tuesday, July 31, 2018 8:23 AM
    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    Tuesday, July 31, 2018 8:22 AM
  •  Get-NetIPAddress -IPAddress 10.* | Set-DnsClientServerAddress -ServerAddresses 10.x.x.x, 10.x.x.x, 173.x.x.x, 10.x.x.x

    \_(ツ)_/


    • Edited by jrv Tuesday, July 31, 2018 9:27 AM
    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    Tuesday, July 31, 2018 9:27 AM
  • Hi John and Jrv,

    Thank you so much for your suggestions, that really helped me in figuring out my final version of the script. By default, all VM adapters with Ethernet(default base windows adapter name) with specific IP subnet ie (10.x.x.x) will only be added with the new DNS entries. Other adapters which are re-named for eg (MGMT) or the virtual NIC's will not take the DNS. 


    Invoke-Command -ComputerName (Get-Content .\dnsnames.txt) -ScriptBlock {


    $NetAdapter = Get-NetAdapter -Name ethernet


    if ($NetAdapter) {


    $IPaddress = Get-NetIPAddress -InterfaceIndex $NetAdapter.ifIndex | Where-Object { ($_.IpAddress -like "10.*")}

    if ($IPaddress) 

            {       

    Set-DnsClientServerAddress -interfaceindex</g> $NetAdapter.IfIndex -ServerAddresses 10.x.x.x, 10.x.x.x, 172.x.x.x, 10.x.x.x -Verbose

    }

    }

    }

    since i'm new to scripting took a lot of time figuring out this but the suggestions really helped and worked out pretty. Now just the subnets for diff subnets for diff geography and done. (Created dnsnames.txt on the local desktop and fed hostnames in that sequentially)

    Thanks

    Shashi Kumar G



    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    • Edited by shashikg Friday, August 3, 2018 11:41 AM
    Friday, August 3, 2018 11:35 AM

All replies

  • Hi Shashi,

    The following cmdlets should achieve what you are asking for.

    Get-NetIPAddress |
        Where-Object IPAddress -like "10.*" |
        Get-NetAdapter |
        Set-DnsClientServerAddress -ServerAddresses 10.x.x.x, 10.x.x.x, 173.x.x.x, 10.x.x.x

    You can add this in the Invoke-Command Scriptblock.

    Would be pleased to know if it was helpful.

    Best regards,

    John




    • Edited by John Seerden Tuesday, July 31, 2018 8:23 AM
    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    Tuesday, July 31, 2018 8:22 AM
  •  Get-NetIPAddress -IPAddress 10.* | Set-DnsClientServerAddress -ServerAddresses 10.x.x.x, 10.x.x.x, 173.x.x.x, 10.x.x.x

    \_(ツ)_/


    • Edited by jrv Tuesday, July 31, 2018 9:27 AM
    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    Tuesday, July 31, 2018 9:27 AM
  • Hi John and Jrv,

    Thank you so much for your suggestions, that really helped me in figuring out my final version of the script. By default, all VM adapters with Ethernet(default base windows adapter name) with specific IP subnet ie (10.x.x.x) will only be added with the new DNS entries. Other adapters which are re-named for eg (MGMT) or the virtual NIC's will not take the DNS. 


    Invoke-Command -ComputerName (Get-Content .\dnsnames.txt) -ScriptBlock {


    $NetAdapter = Get-NetAdapter -Name ethernet


    if ($NetAdapter) {


    $IPaddress = Get-NetIPAddress -InterfaceIndex $NetAdapter.ifIndex | Where-Object { ($_.IpAddress -like "10.*")}

    if ($IPaddress) 

            {       

    Set-DnsClientServerAddress -interfaceindex</g> $NetAdapter.IfIndex -ServerAddresses 10.x.x.x, 10.x.x.x, 172.x.x.x, 10.x.x.x -Verbose

    }

    }

    }

    since i'm new to scripting took a lot of time figuring out this but the suggestions really helped and worked out pretty. Now just the subnets for diff subnets for diff geography and done. (Created dnsnames.txt on the local desktop and fed hostnames in that sequentially)

    Thanks

    Shashi Kumar G



    • Marked as answer by shashikg Friday, August 3, 2018 11:36 AM
    • Edited by shashikg Friday, August 3, 2018 11:41 AM
    Friday, August 3, 2018 11:35 AM