header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
DNS Script change value
Last Post 06 Jul 2010 09:25 AM by aceiii222. 13 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
Duane HaasUser is Offline
New Member
New Member
Posts:13
Avatar

--
22 Oct 2009 12:17 PM
    I found this script:

    function Set-DNSWINS {
    #Get NICS via WMI
    $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName $_ | where{$_.IPAddress -like "*10.201.8.*"}

    foreach($NIC in $NICs) {
    $DNSServers = "10.201.8.98","10.201.158.22"
    $NIC.SetDNSServerSearchOrder($DNSServers)
    $NIC.SetDynamicDNSRegistration("TRUE")
    $NIC.SetWINSServer("10.201.8.98","10.201.158.22")
    }
    }

    function Get-FileName {
    $computer = Read-Host "c:\TEMP\servers.txt"
    return $computer
    }

    $f = Get-FileName
    Get-Content $f foreach {Set-DNSWINS}

    Cant take credit:

    http://fatbeards.blogspot.com/2008/...rvers.html

    Problem is everytime I run it I get:

    A positional parameter cannot be found that accepts argument 'foreach'.
    At :line:19 char:11
    + Get-Content <<<<  $f foreach {Set-DNSWINS}

    And it prompted me for a parameter everytime i run it???


    AythUser is Offline
    Basic Member
    Basic Member
    Posts:232
    Avatar

    --
    22 Oct 2009 12:20 PM
    Looks like the post even has this mistake, try changing the line:

    Get-Content $f foreach {Set-DNSWINS}

    To:

    Get-Content $f | foreach {Set-DNSWINS}

    Note the missing pipe character between Get-Content and Foreach-Object(he used the alias foreach in the script). Can't test as I don't have servers I could run it on(without getting myself in deep dodo), but that should work.


    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    Duane HaasUser is Offline
    New Member
    New Member
    Posts:13
    Avatar

    --
    22 Oct 2009 12:30 PM
    PERFECT THANKS




    AythUser is Offline
    Basic Member
    Basic Member
    Posts:232
    Avatar

    --
    24 Oct 2009 12:37 PM
    You're welcome, I actually posted a comment on the blog post in question mentioning it, it's a great script otherwise, and duplicates one of my vbscripts I use often.


    My Blog about Powershell http://poweroftheshell.blogspot.com/ Follow me on twitter @darrinhenshaw
    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    02 Jul 2010 12:31 PM
    I have done all the recommended changes and I am still getting an error. Could someone help me? This is the error I receive:

    Get-Content : Cannot bind argument to parameter 'Path' because it is an empty string.
    At line:20 char:12
    + Get-Content <<<< $filePath | foreach {Set-DNSWINS}
    + CategoryInfo : InvalidData: (:) [Get-Content], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.GetContentComma
    nd


    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    02 Jul 2010 01:41 PM
    So, lets clean this up a bit as well as fix the problem:

    ### Script Begin ###
    param (
        [parameter(Mandatory=$True)]
        [string]$file
    )

    function Set-DNSWINS {
        param (
            [string]$computer,
            [array]$DNS = "10.201.8.98","10.201.158.22",
            [array]$WINS = "10.201.8.98","10.201.158.22"
        )
        #Get NICS via WMI
        gwmi Win32_NetworkAdapterConfiguration -ComputerName $computer | ? { $_.IPAddress -like "10.201.8.*" } | % {
            $_.SetDNSServerSearchOrder($DNS)
            $_.SetDynamicDNSRegistration("TRUE")
            $_.SetWINSServer($WINS)
        }
    }

    Get-Content $file | % { Set-DNSWINS $_ }

    ### Script End ###

    Now, to run this script you'll need to provide a parameter with the filename like:
    Set-DNSWINS -file "C:\Temp\list.txt"


    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    06 Jul 2010 07:05 AM
    Thanks for helping me out! My next question is where will I need to insert the filename parameter?

    I am really new to powershell scripting so thanks for helping a noob out :)


    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    06 Jul 2010 07:40 AM
    in the command line:

    .\Set-DNSWINS.ps1 -file "filename.txt"


    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    06 Jul 2010 08:31 AM
    This is what I get:
    __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 70 __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 0 __GENUS : 2 __CLASS : __PARAMETERS __SUPERCLASS : __DYNASTY : __PARAMETERS __RELPATH : __PROPERTY_COUNT : 1 __DERIVATION : {} __SERVER : __NAMESPACE : __PATH : ReturnValue : 68
    I am attaching what my script looks like as well.

    dns.ps1

    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    06 Jul 2010 08:47 AM
    One thing I'm noticing quickly is that your arrays are being created as strings instead of arrays, or single item arrays.
    Notice I've added quotes around each IP in $DNS instead of the entire string and changed $WINS to a string since you only have one WINS IP.

    param (
    [parameter(Mandatory=$True]
    [string]$file
    )

    function Set-DNSWINS {
    param (
    [string]$computer,
    [array]$DNS = "10.0.0.101","10.0.0.100"
    [string]$WINS = "10.0.0.100"
    )

    #Get NICS via WMI
    gwmi Win32_NetworkAdapterConfiguration -ComputerName $computer | ? { $_.IPAddress -like "10.0.0.*" } | % {
    $_.SetDNSServerSearchOrder($DNS)
    $_.SetDynamicDNSRegistration("TRUE")
    $_.SetWINSServer($WINS)
    }
    }

    Get-Content $file | % { Set-DNSWINS $_ }

    Now, you may still see output that seems odd because $_.SetNNN is going to output its results to the console. Add " | Out-Null" to each line if you do not want any output.


    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    06 Jul 2010 08:55 AM
    Thanks Cruisader! That did the trick! Thanks for the help!


    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    06 Jul 2010 09:16 AM
    I was actually able to successfully change the server to have just one DNS IP address but when I try to set the secondary DNS server IP I receive this error message:
    Missing expression after ','.
    At C:\dns.ps1:10 char:36
    + [array]$DNS = "10.0.0.100", <<<< "10.0.0.101"
    + CategoryInfo : ParserError: (,:OperatorToken) [], ParseExceptio
    n
    + FullyQualifiedErrorId : MissingExpressionAfterToken

    Any ideas?


    PoSherLifeUser is Offline
    Basic Member
    Basic Member
    Posts:364
    Avatar

    --
    06 Jul 2010 09:21 AM
    [array]$DNS = @("10.0.0.101","10.0.0.100")

    yup, i didn't debug. in param () arrays must be defined using @(). My bad.


    When at first you don't succeed Step-Into

    http://theposherlife.blogspot.com
    http://www.jandctravels.com

    aceiii222User is Offline
    New Member
    New Member
    Posts:6
    Avatar

    --
    06 Jul 2010 09:25 AM
    Got it. Thanks again!


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer