PowerShell を使用して DNS ゾーンとレコードを作成および管理する方法

WindowsでDNSゾーンとレコードを管理するのは、特に一括処理や自動化が必要な場合、時に非常に面倒な作業になることがあります。従来のDnscmdCLIツールも使えますが、やや時代遅れで使いにくいと感じます。そこで、より現代的な環境を構築するには、PowerShellのDNSServerモジュールをインストールすると、非常に役立ちます。DNSレコードやゾーンを頻繁に変更する場合、PowerShellでの操作方法を知っておくと、大幅に時間を節約できるだけでなく、スクリプトの作成や自動化も容易になります。管理するゾーンが少数でも、CSVファイルから一括でレコードを変更する場合でも、このガイドには基本的な操作方法が網羅されています。DNSサーバー上のレコードやゾーンの作成、変更、削除に役立つコマンドも紹介されています。使い方さえ覚えてしまえば、どれも非常に簡単です。

Windows で PowerShell を使用して DNS 管理の問題を解決する方法

DNSServer PowerShell モジュール

まず、 RSAT(リモートサーバー管理ツール)の一部であるDNSServerモジュールが必要です。Windows 10では、RSATを別途インストールする必要があるかもしれません。「設定」>「アプリ」>「オプション機能」>「機能の追加」に移動し、「RSAT: DNS Server Tools」を探してください。Windows Serverでは簡単です。「サーバーマネージャー」>「管理」>「役割と機能の追加」から有効化できます。モジュールがインストールされているかどうかわからない場合は、次のコマンドを実行してください。

Get-Module DNSServer –ListAvailable

結果が表示されれば問題ありません。そうでない場合は、RSATをインストールするか、DNSサーバーツールを有効にする必要があります。使用可能なコマンドを確認するには、以下を試してください。

Get-Module DNSServer

これですべてのコマンドレットが表示されるので、何を調整できるかがわかります。よくある問題の一つは、PowerShellがモジュールを認識しないことです。これは通常、モジュールがインストールされていないか、セッションにインポートされていないことが原因です。手動でインポートするには、 を実行してくださいImport-Module DNSServer。ただし、新しいWindowsでは、インストールされている場合は自動的に読み込まれるはずです。

PowerShell で DNS ゾーンを管理する

ゾーンを表示または作成する必要がある場合、まず次のコマンドを実行して、DNSサーバー上の既存のゾーンを確認します。

Get-DnsServerZone –ComputerName dc01

dc01 をDNSサーバーのホスト名またはIPアドレスに置き換えてください。空白またはエラーが表示される場合は、接続または権限に問題があります。Active Directory内にwoshub.comという新しいプライマリゾーンを追加するには、次のコマンドを実行します。

Add-DnsServerPrimaryZone -Name woshub.com -ReplicationScope "Forest" –PassThru

これで統合ゾーンが作成され、ADにリンクされた状態になります。逆引き参照ゾーン(PTRレコードに適しています)が必要な場合は、以下を実行します。

Add-DnsServerPrimaryZone -NetworkId "192.168.100.0/24" -ReplicationScope Domain

これは逆DNSマッピングを設定するときに便利です。また、新しいゾーンを追加した場合は、環境内の他のDNSサーバーと同期することを忘れないでください。特に複数のドメインコントローラーがある場合は重要です。同期するには、次の手順に従ってください。

Sync-DnsServerZone –passthru

ゾーンにはまだレコードが何も表示されませんが、これは正常です。ゾーンの内容を確認するには、次のコマンドを実行してください。

Get-DnsServerResourceRecord -ComputerName dc01 -ZoneName contoso.local

何らかの理由でゾーンを削除したい場合も、同様に簡単です。

Remove-DnsServerZone -Name woshub.com -ComputerName dc01

これによりゾーンが消去され、記録も削除されるため、注意が必要です。実際に実行する前に必ず二重チェックを行ってください。

DNSレコードをプロのように管理する

新しいDNSレコードの追加は非常に簡単です。ホスト名のAレコードを作成するには、以下のコマンドを実行します。

Add-DnsServerResourceRecordA -Name ber-rds1 -IPv4Address 192.168.100.33 -ZoneName woshub.com -TimeToLive 01:00:00

また、PTR レコード (逆 DNS 用) も必要な場合は、そのコマンドに–CreatePtr を追加するか、個別に実行します。

Add-DNSServerResourceRecordPTR -ZoneName 100.168.192.in-addr.arpa -Name 33 -PTRDomainName ber-rds1.woshub.com

CNAMEも同様です。CNAMEは基本的にAレコードのエイリアスです。設定するには、次のコマンドを実行します。

Add-DnsServerResourceRecordCName -ZoneName woshub.com -Name Ber-RDSFarm -HostNameAlias ber-rds1.woshub.com

既存のレコードのIPアドレスを更新する必要がある場合は、少し時間がかかります。現在のレコードを取得し、プロパティを変更してから、置き換えます。

$OldDNS = get-DnsServerResourceRecord -Name ber-rds1 -ZoneName woshub.com -ComputerName dc01
$NewDNS = $OldDNS | Select-Object -Property * -ExcludeProperty RecordData

$NewDNS. RecordData. IPv4Address = [System. Net. IPAddress]::parse('192.168.100.133')
Set-DnsServerResourceRecord -NewInputObject $NewDNS -OldInputObject $OldDNS -ZoneName woshub.com -ComputerName dc01

And make sure it’s updated:

Get-DnsServerResourceRecord -Name ber-rds1 -ZoneName woshub.com

Want to list all CNAME records to troubleshoot? Just add -RRType CNAME:

Get-DnsServerResourceRecord -ComputerName DC01 -ZoneName woshub.com -RRType CNAME

Or filter records with specific names or patterns using:

Get-DnsServerResourceRecord -ZoneName woshub.com -RRType A | Where-Object HostName -like "*rds*"

Removing records? Easier than you think. For example, delete a CNAME with:

Remove-DnsServerResourceRecord -ZoneName woshub.local -RRType CName -Name Ber-RDSFarm

Or an A record:

Remove-DnsServerResourceRecord -ZoneName woshub.local -RRType A -Name ber-rds1 –Force

Same goes for PTR records in reverse zones.

Bulk Creating Records from CSV Files

Now, here’s the thing — doing records one by one sucks. Instead, if you have a CSV with all your hostnames and IPs, automate the whole process. Create a text file, say NewDnsRecords.txt, with this format:

HostName, IPAddress host1, 192.168.100.10 host2, 192.168.100.11 

And run this PowerShell snippet:

Import-Csv "C:\PS\NewDnsRecords.txt" | %{ Add-DnsServerResourceRecordA -ZoneName woshub.com -Name $_."HostName" -IPv4Address $_."IPAddress" }

If you want PTR records for reverse DNS, add –CreatePtr and modify the script accordingly, or process a file with the hostname, octet, and zone info. For example, a CSV like:

octet, hostName, zoneName 102, ber-rds2.woshub.com, 100.168.192.in-addr.arpa 103, ber-rds3.woshub.com, 100.168.192.in-addr.arpa 

And run a similar import script:

Import-Csv "C:\PS\NewDnsPTRRecords.txt" | %{ Add-DNSServerResourceRecordPTR -ZoneName $_."zoneName" -Name $_."octet" -PTRDomainName $_."hostName" }

Always double-check your DNS through the DNS Manager console (dnsmgmt.msc) after bulk changes — nothing worse than missing records or typos.

Wrap-up

Handling DNS with PowerShell isn’t rocket science, but it’s easy to get overwhelmed when you’re doing it manually or in big batches. These commands are a good starting point, and once you get why they work, automating DNS tasks becomes way less stressful. Just keep testing in a lab environment if possible, and remember — always backup your DNS zones before making major changes, in case something goes sideways. Overall, scripting DNS management with PowerShell can seriously streamline your workflow, especially if you manage lots of zones or records regularly.

Summary

  • Make sure the DNSServer module is installed and imported.
  • Use Get-DnsServerZone and Add-DnsServerPrimaryZone to oversee and create zones.
  • Add or update DNS records with commands like Add-DnsServerResourceRecordA or Set-DnsServerResourceRecord.
  • Bulk operations? Just import from CSV and run the script.
  • Always verify your changes in DNS Manager or with Get-DnsServerResourceRecord.

Fingers crossed this helps speed up your DNS workflows — because Windows DNS can be a real stubborn beast sometimes, but PowerShell makes it a lot more manageable.