简体   繁体   中英

Update DNS records with powershell

i would like to write a powershell script that will update the name of a list of DNS A records.

However, i also need to update the PTR records that correspond to the DNS records. As far as i read there is no parameter that will do this for me automatically and i decided to write a separate script that will handle the Ptr records.

I am not sure what is the correct approach to update the pointer record PtrDomainName(see code below). I managed to find info about the approach to change the IP and the TTL (both in my code) but the name is missing.

$records = Get-Content .\ptr_records.txt

foreach ($record in $records){
    #modify the records
    $OldObj = Get-DnsServerResourceRecord -ZoneName 10.in-addr.arpa -RRType Ptr | Where-Object {$_.RecordData.PtrDomainName -like $record }
    $NewObj = $OldObj.Clone()
    ###below line modifies time to live
    $NewObj.TimeToLive = [System.TimeSpan]::FromHours(2) 
    ###below line modifies ip
    $NewObj.RecordData.IPv4Address = [ipaddress]'8.8.8.8'
    ###how do i modify the record data?????
    $NewObj.RecordData= ??? 
Set-DnsServerResourceRecord -NewInputObject $NewObj -OldInputObject $OldObj -ZoneName "10.in-addr.arpa" -PassThru
    
}

here is also some powershell output:

in the example below dnsrecord1 will be changed to dnsrecord2



PS C:\Users\da.v.kolev> $test = Get-DnsServerResourceRecord -ZoneName 10.in-addr.arpa -RRType Ptr | Where-Object {$_.RecordData.PtrDomainName -like 'dnsrecord1.contoso.com.' }
PS C:\Users\da.v.kolev> $test

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
1.33.22                   PTR        12         0                    01:00:00     dnsrecord1.contoso.com.


PS C:\Users\da.v.kolev> $test.RecordData

PtrDomainName                          PSComputerName
-------------                          --------------
dnsrecord1.contoso.com.


PS C:\Users\da.v.kolev> $test.RecordData.PtrDomainName
dnsrecord1.contoso.com.
PS C:\Users\da.v.kolev>

The point is you update a DNS A-record with a new IP.

For a PTR record, the IP-address is part of the name. Can't be updated. So the PTR record will be obsolete and need to be removed. And a new record need to be created.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM