help on powershell script

John JY 221 Reputation points
2021-01-15T22:02:46.79+00:00

Hi,

We need to change around 2000 member servers dynamic DNS A records to static. I have it in csv or text format.
Can anyone share how to script to change them?

Thank you.

Windows DHCP
Windows DHCP
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.DHCP: Dynamic Host Configuration Protocol (DHCP). A communications protocol that lets network administrators manage centrally and automate the assignment of Internet Protocol (IP) addresses in an organization's network.
1,021 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,358 questions
{count} votes

Accepted answer
  1. Dave Patrick 426.1K Reputation points MVP
    2021-01-15T22:54:02.273+00:00

    This PowerShell should sort it.

    foreach ($server in (Get-Content c:\Computers.txt)) {
    
    $OldObj = Get-DnsServerResourceRecord -Name $server -ZoneName "mydomain.local.com" -RRType "A"
    $NewObj = $OldObj.Clone()
    $NewObj.TimeToLive = [System.TimeSpan]::FromHours(0)
    Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject $OldObj -ZoneName "mydomain.local.com" -PassThru
    
    }
    

    --please don't forget to Accept as answer if the reply is helpful--

    1 person found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. John JY 221 Reputation points
    2021-01-29T18:02:22.647+00:00

    Sorry for the reply and did not get a chance

    Here is error I got when I ran the script. (I did replace mydomain.local.com with my zone name)

    Get-DnsServerResourceRecord : Failed to get the zone information for mydomain.local.com on server server1.
    At line:2 char:12

    • $OldObj = Get-DnsServerResourceRecord -Name $server -ZoneName "mydomain ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (mydomain.local.com:root/Microsoft/...rResourceRecord) [Get-DnsServerResourceRec
      ord], CimException
    • FullyQualifiedErrorId : WIN32 1722,Get-DnsServerResourceRecord

    You cannot call a method on a null-valued expression.
    At line:3 char:2

    • $NewObj = $OldObj.Clone()
    • ~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

    The property 'TimeToLive' cannot be found on this object. Verify that the property exists and can be set.
    At line:4 char:2

    • $NewObj.TimeToLive = [System.TimeSpan]::FromHours(0)
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (:) [], RuntimeException
    • FullyQualifiedErrorId : PropertyNotFound

    Set-DnsServerResourceRecord : Cannot validate argument on parameter 'NewInputObject'. The argument is null or empty.
    Provide an argument that is not null or empty, and then try the command again.
    At line:5 char:46

    • Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject ...
    • ~~~~~~~
    • CategoryInfo : InvalidData: (:) [Set-DnsServerResourceRecord], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Set-DnsServerResourceRecord
    0 comments No comments

  2. Dave Patrick 426.1K Reputation points MVP
    2021-01-29T18:36:37.483+00:00

    Sounds like the record for that $server did not exist.

    --please don't forget to Accept as answer if the reply is helpful--

    0 comments No comments

  3. John JY 221 Reputation points
    2021-01-29T19:20:24.757+00:00

    Thanks for your quick response and help.

    I did check Get-Content c:\tmp\Computers.txt and got the computers names
    changed the computer name in txt file to bios name with no mydomain.local.com

    still same errors and let me know what else I can try? Do I need to run the script on a domain controller?

    Thank you again!


  4. Dave Patrick 426.1K Reputation points MVP
    2021-01-29T19:23:28.36+00:00

    Do I need to run the script on a domain controller?

    I think that's how I did my testing but it was quite a while ago.

    --please don't forget to Accept as answer if the reply is helpful--

    0 comments No comments