0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DNSの各ゾーンに、Powershellを使ってワイルドカードレコードを追加したい

Posted at

やりたいこと

ADのDNSの各ゾーンに、Powershellを使って名前が"*"、データが空欄のTXTレコード(ワイルドカードレコード)を1件ずつ追加したい。

ワイルドカードレコードって何?という方はこちらを参照するといいかもしれません。
DNS のワイルドカードレコードについて - プログラマーのメモ書き

ちなみに、別にワイルドカードレコードは中身が空欄でなくても、TXTレコードでなくても良いです。
また前提として、追加する対象となるゾーンに既存のワイルドカードレコードは存在しないものとします。(初めて追加するという状況です。)

細かい部分でやりたいことが違っていたら、適宜修正して使ってください。

というわけで、こんな感じで実装してみました。

実装

AddWildcardRecordsWithoutData.ps1
param (
	[Parameter(Mandatory=$true)]
	[string]$ZoneName
)

try {
    #Add a temporary TXT record
	Add-DnsServerResourceRecord -ZoneName $ZoneName -TXT -Name "*" -DescriptiveText "tmp"
	
    #Create a new TXT record with no descriptive text
	$old = Get-DnsServerResourceRecord -ZoneName $ZoneName -Name "*" -RRType TXT
	
	$new = $old.Clone()
	$new.RecordData.DescriptiveText = ""
	
    #Apply the updated record to the zone
	Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ZoneName $ZoneName
	
	Write-Host "succeeded : [$ZoneName]"
	} catch {
	Write-Host "failed : [$ZoneName]"
}
zoneList.txt
a.test.local
c.test.local
d.test.local
f.test.local
j.test.local
script.ps1
$baseDir = (Get-Location).Path
$targetZones = Get-Content -Path (Join-Path $baseDir "zoneList.txt")
$scriptPath = Join-Path $baseDir "AddWildcardRecordsWithoutData.ps1"
foreach ($tmpZone in $targetZones) {
    powershell.exe -File $scriptPath -ZoneName $tmpZone
}

使い方としては、同じディレクトリにAddWildcardRecordsWithoutData.ps1とzoneList.txtを配置しておき、そのディレクトリへ移動してからscript.ps1を管理者として実行するといった感じです。

ちょっとめんどいことしてない?

確かに、Add-DnsServerResourceRecord -ZoneName $ZoneName -TXT -Name "*" -DescriptiveText ""みたいなことして終わりやと思っていたのですが、DescriptiveText(データ)は空欄にするなと怒られたのでこうせざるを得ませんでした。

ちなみに、GUIでやればデータが空欄のTXTレコードは普通に作れます。
誰かの役に立て。いつか。

以上です。

環境
OS:Windows Server 2022
Powershell:5.1.14393.8422

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?