Avatar of Michael Jimenez
Michael Jimenez
 asked on

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

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

DNSPowershell

Avatar of undefined
Last Comment
Michael Jimenez

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michael Jimenez

ASKER
Thanks! I'll give this a try.
Michael Jimenez

ASKER
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.
SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Michael Jimenez

ASKER
Awesome. That was it!

Thanks for your help oBdA!
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Michael Jimenez

ASKER
Very helpful and quick to respond!!

Thanks again!