We help IT Professionals succeed at work.

How to use DNSCMD to batch modify DNS records using different credentials

Michael Jimenez
on
997 Views
Last Modified: 2018-02-22
Dear Experts,

I'm trying to modify DNS records on a server in our DMZ using credentials in the DMZ.  But when I run the below modified code the command does not complete successfully, if the Invoke-Command is used.  Invoke-Expression works fine if I run this against a DNS server that's internal.

If there is another way of modifying DNS records using a CSV file, I'm all ears.

Thanks!

$username="Username"
$password="Password Hash" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password

$NewComputerList = Import-Csv -Path "c:\DNSRecordsNEW.csv"

foreach($record in $NewComputerList) { 
    # Capture the record contents as variables 
    $recordName = $record.hostname 
    $recordType = $record.type 
    $recordAddress = $record.ipaddress 

    # Build our DNSCMD DELETE command syntax 
    $cmdDelete = "dnscmd $DNSServer /RecordDelete $DNSZone $recordName $recordType /f" 

    # Build our DNSCMD ADD command syntax 
    $cmdAdd = "dnscmd $DNSServer /RecordAdd $DNSZone $recordName $recordType $recordAddress" 

    # Now we execute the command 
    Write-Host "Running the following command: $cmdDelete" 
    #Invoke-Expression $cmdDelete
    Invoke-Command -ComputerName $DNSServer -Credential $cred -ScriptBlock { $cmdDelete }

    Write-Host "Running the following command: $cmdAdd" 
    #Invoke-Expression $cmdAdd
    Invoke-Command -ComputerName $DNSServer -Credential $cred -ScriptBlock { $cmdAdd }
    }

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Thanks! I'll give this a try.

Author

Commented:
I've just tried the above code and was able to get the RecordDelete to complete but am unsuccessful with the RecordAdd.  I'm getting an Invalid Data and a Command failed:  ERROR_INVALID_PARAMETER  87  0x57.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

Author

Commented:
Awesome. That was it!

Thanks for your help oBdA!

Author

Commented:
Very helpful and quick to respond!!

Thanks again!

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions