Powershell

From My Notepad
Jump to: navigation, search

Administering Windows DNS with PowerShell

DNSServer Module Docs

To verify the DNS module is installed:

Get-Module DNSServer –ListAvailable

To add an A record:

$zonename = 'powershelllab.local'
$computername = 'localhost'
$recordname = 'new-a'

Add-DnsServerResourceRecordA -ComputerName $computername -ZoneName $zonename -Name $recordname -AllowUpdateAny -IPv4Address "1.1.1.1"

To add a CNAME record:

$zonename = 'powershelllab.local'
$computername = 'localhost'
$recordname = 'test-cname'

Add-DnsServerResourceRecordCName -ComputerName $computername -ZoneName $zonename -Name $recordname -AllowUpdateAny -HostNameAlias 'test.goskyhawk.com'

To update an existing A record:

$zonename = 'powershelllab.local'
$computername = 'localhost'
$recordname = '@'

$old = Get-DnsServerResourceRecord -ComputerName $computername -ZoneName $zonename -Name $recordname -RRType A
$new = Get-DnsServerResourceRecord -ComputerName $computername -ZoneName $zonename -Name $recordname -RRType A
$new.RecordData.IPv4Address = '2.2.2.2'
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ComputerName $computername -ZoneName $zonename

To update an existing CNAME record:

$zonename = 'powershelllab.local'
$computername = 'localhost'
$recordname = 'www'

$old = Get-DnsServerResourceRecord -ComputerName $computername -ZoneName $zonename -Name $recordname -RRType CNAME
$new = Get-DnsServerResourceRecord -ComputerName $computername -ZoneName $zonename -Name $recordname -RRType CNAME
$new.RecordData.HostNameAlias = "test.powershelllab.local"
Set-DnsServerResourceRecord -NewInputObject $new -OldInputObject $old -ComputerName $computername -ZoneName $zonename

To remove a record or list of records:

$zonename = 'powershelllab.local'
$computername = 'localhost'
$recordname = 'test-cname'
$recordtype = 'CNAME'

Get-DnsServerResourceRecord -ComputerName $computername -ZoneName $zonename -Name $recordname -RRType $recordtype | Remove-DNSServerResourceRecord –ZoneName $zonename –ComputerName $computername