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

Michael JimenezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

oBdACommented:
Instead of opening two remote sessions per record, open one and let the loop run remotely. Variables need to be passed as (positional!) arguments:
$username="Username"
$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

0

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.
0
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.
0
Discover the Answer to Productive IT

Discover app within WatchGuard's Wi-Fi Cloud helps you optimize W-Fi user experience with the most complete set of visibility, troubleshooting, and network health features. Quickly pinpointing network problems will lead to more happy users and most importantly, productive IT.

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

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

Thanks again!
0
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
DNS

From novice to tech pro — start learning today.