troubleshooting Question

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

Avatar of Michael Jimenez
Michael Jimenez asked on
6 Comments2 Solutions1018 ViewsLast Modified:
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.


$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 }
Join our community to see this answer!
Unlock 2 Answers and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros