Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
500 views
in Technique[技术] by (71.8m points)

dns - How to use PowerShell to delete a single NAPTR record?

I created a new NAPTR records using the GUI. However I'd like to delete one of them using PowerShell.

I can prove there are 2 records:

> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType naptr

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
foo                       NAPTR      35         0                    01:00:00
foo                       NAPTR      35         0                    01:00:00

> $OldObj = Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType naptr
> $OldObj[0].RecordData.Data
010001000155036F6E65036F6E6500
> $OldObj[1].RecordData.Data
0200020001550374776F0374776F00
> $OldObj[2].RecordData.Data
>

If I try to delete the records, I can do it interactively:

> Remove-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR

Confirm
Removing DNS resource record set by name foo of type NAPTR from zone example.com on TOMDEV server. Do you want to continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y

Sadly that deletes both of the records!

What if I try to remove the object, instead?

> $OldObj[1] | Remove-DnsServerResourceRecord -ZoneName "example.com" -Name "foo"

Confirm
Removing DNS resource record set by name foo of type NAPTR from zone example.com on TOMDEV server. Do you want to
continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType naptr
Get-DnsServerResourceRecord: Failed to get foo record in example.com zone on TOMDEV server.

Oh no! That deleted both records!

Can I specify a single record to Remove-DnsServerResourceRecord?

Here's what I've tried:

This just gets rejected out-right:

> Remove-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR -RecordData "0200020001550374776F0374776F00"
Remove-DnsServerResourceRecord: InputObject for resource record has an invalid value. Failed to remove the resource record on TOMDEV server. Please check extended error for additional details.

If I try to capture the one record and pipe it to Remove-, that removes both records:

# First, we single out exactly one record:
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR | Where-Object {$_.HostName -eq "foo" -and $_.RecordData.Data -eq "0200020001550374776F0374776F00" }

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
foo                       NAPTR      35         0                    01:00:00

>
# That worked! Let's use that query and pipe it to Remove!
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR | Where-Object {$_.HostName -eq "foo" -and $_.RecordData.Data -eq "0200020001550374776F0374776F00" } | Remove-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -Force
# Nope, that removed both records!
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType naptr
Get-DnsServerResourceRecord: Failed to get foo record in example.com zone on TOMDEV server.
>

It isn't removing everything at "foo", just the NAPTR records there. (In fact, the message says "record sets" (plural), not "record".

Let's prove that by creating an A record plus 2 NAPTR records all at label "foo". We'll see the same problem:

> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo"

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
foo                       A          1          0                    01:00:00        4.4.4.4
foo                       NAPTR      35         0                    01:00:00
foo                       NAPTR      35         0                    01:00:00

> $OldObj = Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType naptr
> $OldObj[0].RecordData.Data
010001000155036F6E65036F6E6500
> $OldObj[1].RecordData.Data
0200020001550374776F0374776F00
> $OldObj[2].RecordData.Data
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR | Where-Object {$_.HostName -eq "foo" -and $_.RecordData.Data -eq "0200020001550374776F0374776F00" } | Remove-DnsServerResourceRecord -ZoneName "example.com" -Name "foo"

Confirm
Removing DNS resource record set by name foo of type NAPTR from zone example.com on TOMDEV server. Do you want to
continue?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"): y
> Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo"

HostName                  RecordType Type       Timestamp            TimeToLive      RecordData
--------                  ---------- ----       ---------            ----------      ----------
foo                       A          1          0                    01:00:00        4.4.4.4

>

How can I delete a single NAPTR record?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you are saying you want to delete one of them, and it does not matter which one, are saying you also tried this:

Get-DnsServerResourceRecord -ZoneName "example.com" -Name "foo" -RRType NAPTR | 
Where-Object {
    $PSItem.HostName -eq "foo" -and 
    $PSItem.RecordData.Data -eq "0200020001550374776F0374776F00" 
} |
Select-Object -First 1 |
Remove-DnsServerResourceRecord -WhatIf

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...