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

Michael Jimenez
Michael Jimenez used Ask the Experts™
on
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

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

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.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
Sorry, copy and paste error; remove the "/f" at the end of line 13.

Author

Commented:
Awesome. That was it!

Thanks for your help oBdA!

Author

Commented:
Very helpful and quick to respond!!

Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial