How can I combine Add-DnsServerResourceRecordA & Add-DnsServerResourceRecordPTR cmdlets to automate creating DNS records?

PowerShell gurus,

We are running Windows Server 2012 R2 with PowerShell v4.0 and want to take advantage of the new Add-DnsServerResourceRecordA and  Add-DnsServerResourceRecordPTR cmdlets (and remove-dnsserverresourcerecord) to automate as much as possible the creation/deletion of statically configured DNS records.

Question: how can I combine these two cmdlets to run to create the DNS 'A' and DNS 'PTR' records in one long cmdlet?  How would we make it so we can run both of these below together as one cmdlet?

Add-DnsServerResourceRecordA -Name "hostname123" -ZoneName "contoso.com" -AllowUpdateAny -IPv4Address "192.168.0.123" -TimeToLive 01:00:00

Add-DnsServerResourceRecordPtr -Name "123" -ZoneName "0.168.192.in-addr.arpa" -AllowUpdateAny -TimeToLive 01:00:00 -AgeRecord -PtrDomainName "hostname123.contoso.com"  

As we develop our competence with powershell, we would like to make it so we can use (get-content) to import DNS entries from a .csv file then as much as possible automate the creation of DNS 'A' and 'PTR' records.  How can we do this?

Thank you for your insight and ideas!

Cheers!

August 22nd, 2014 10:41pm

I don't see a reason why you can't simply run each of these cmdlets separately. Each cmdlet requires different pieces of information and so trying to make this one long cmdlet isn't going to help much. In fact, it'll probably make it more difficult. Now, storing information in a CSV to help facilitate creating these quicker - wonderful idea.

Start with a CSV named DNSRecords.csv that has the format seen below. I have included the representation in both Excel and in Programmer's Notepad.

This example script below hasn't been tested. It has been designed to one, import the data in the CSV file into a variable, and two, iterate through each record store in the variable. As it looks at each entry it determines the type of record (A or PTR) and then heads the correct way so that the proper record is created. Best of luck and hope this is helpful.

$DNSRecords = Import-Csv -Path 'C:\Users\tommymaynard\Desktop\DNSRecords.csv'
Foreach ($Entry in $DNSRecords) {

    If ($Entry.Type -eq 'A') {
        Add-DnsServerResourceRecordA -Name $Entry.Name -ZoneName $Entry.ZoneName -AllowUpdateAny -IPv4Address $Entry.IPv4Address -TimeToLive $Entry.TimeToLive
        Write-Output -Verbose "Added A Record for $($Entry.Name)"

    } ElseIf ($Entry.Type -eq 'PTR') {
        Add-DnsServerResourceRecordPtr -Name $Entry.Name -ZoneName $Entry.ZoneName -AllowUpdateAny -TimeToLive $Entry.TimeToLive -AgeRecord -PtrDomainName $Entry.PtrDomainName
        Write-Output -Verbose "Added PTR Record for $($Entry.Name)"

    } Else {
        Write-Output -Verbose 'Unable to determine DNS Record Type.'
    }

}

Free Windows Admin Tool Kit Click here and download it now
August 23rd, 2014 1:44am

Thanks a lot Tommy!

I will test this on Mon or Tue then report results.  

August 23rd, 2014 7:09pm

Hi Matt,

Any updates on this issue?

If there is anything else regarding this issue, please feel free to post back.

Best Regards,

Anna Wang

Free Windows Admin Tool Kit Click here and download it now
August 27th, 2014 12:29pm

Thank You Tommy.... you rock!  This .ps1 script to create PTR records and A records worked!  

I successfully created 286 DNS records using this outstanding PSv4 script.  


September 3rd, 2014 1:51am

I can't speak for PowerShell v4.0, but in 3.0 there is an option -CreatePTR that can be applied at the end of the command.

get-help Add-DnsServerResourceRecordA

NAME
    Add-DnsServerResourceRecordA

SYNTAX
    Add-DnsServerResourceRecordA [-ZoneName] <string> [-Name] <string> [-IPv4Address] <ipaddress[]> [-AllowUpdateAny]
    [-CreatePtr] [-ComputerName <string>] [-TimeToLive <timespan>] [-AgeRecord] [-PassThru] [-CimSession
    <CimSession[]>] [-ThrottleLimit <int>] [-AsJob] [-WhatIf] [-Confirm]  [<CommonParameters>]

Free Windows Admin Tool Kit Click here and download it now
February 12th, 2015 10:33am

That's definitely good to know, and a nice feature of the Add-DnsServerResourceRecordA cmdlet--thanks.

In this problem, the OP was creating one of two records: an A record, or a PTR record. While it is possible that he had entries in the CSV file where an A record and PTR record were being created for the same IP, I think trying to script around that possibility, and using the -CreatePtr parameter would have been unnecessarily difficult. As well, the OP didn't have an IP address as part of each row, regardless of the record type--take a look at his CSV.

I haven't put a great deal of thought into using this parameter, but at first glance, that's my feeling. Thanks for the info!

February 12th, 2015 11:39am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics