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.


$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

Michael JimenezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Instead of opening two remote sessions per record, open one and let the loop run remotely. Variables need to be passed as (positional!) arguments:
$password="Password Hash" | ConvertTo-SecureString
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username,$password

$DNSServer = 'SomeHost'
$DNSZone = 'a.b.c'
$NewComputerList = Import-Csv -Path "c:\DNSRecordsNEW.csv"

Invoke-Command -ComputerName $DNSServer -Credential $cred -ArgumentList $DNSZone, $NewComputerList -ScriptBlock {
	Param($DNSZone, $NewComputerList)
	$NewComputerList | ForEach-Object {
		& dnscmd.exe $ENV:ComputerName /RecordDelete $DNSZone $_.hostname $_.type /f
		& dnscmd.exe $ENV:ComputerName /RecordAdd $DNSZone $_.hostname $_.type $_.ipaddress /f

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Michael JimenezAuthor Commented:
Thanks! I'll give this a try.
Michael JimenezAuthor 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.
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Sorry, copy and paste error; remove the "/f" at the end of line 13.
Michael JimenezAuthor Commented:
Awesome. That was it!

Thanks for your help oBdA!
Michael JimenezAuthor Commented:
Very helpful and quick to respond!!

Thanks again!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.