Update-DnsServerResourceRecordA.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<#
.Synopsis
Update-DnsServerResourceRecordA simplifies the process of updating a DNS
record.
.DESCRIPTION
Update-DnsServerResourceRecordA simplifies the process of updating a DNS
record.
.PARAMETER ComputerName
Active Directory/DNS Server(s), default localhost
.PARAMETER ZoneName
DNS Zone Name, default current domain name
.PARAMETER RecordType
Record type, Default A, but AAAA is acceptible
.PARAMETER Name
The name of the A record
.PARAMETER IPAddress
The new IP address for the A record
.PARAMETER Force
Force to create a new resource record if it doesn't exist.
.PARAMETER RRIndex
If you have more than one A record for a given name, the script will
default to change the first one. Use the $Index parameter to choose
a different record.
.EXAMPLE
Update-DnsServerResourceRecordA -Name server01 -IPAddress 10.146.2.250 -ZoneName domain.com
Changes the DNS A record for server01 to 10.146.2.250 on the local DNS server for domain.com.
.EXAMPLE
Update-DnsServerResourceRecordA -Name server01 -IPAddress 10.146.2.250 -ComputerName DC01 -ZoneName domain.com
Changes the DNS A record for server01 to 10.146.2.250 on the DNS server DC01 for domain.com.
.EXAMPLE
Update-DnsServerResourceRecordA -Name server01 -IPAddress 10.146.2.250 -ComputerName DC01 -ZoneName domain.com
Changes the DNS A record for server01 to 10.146.2.250 on the DNS server DC01 for domain.com, and if the record
doesn't exist, the record is created.
.NOTES
Created by: Jason Wasser
Modified: 4/14/2015 02:00:10 PM
Version: 1.2
 
Changelog:
 * Added -AllowUpdateAny on creating a new record.
 * Added the RRIndex parameter so we can account for multiple resource records
   with the same name. Defaulting to first record for simplicity.
.LINK
https://gallery.technet.microsoft.com/scriptcenter/Update-DnsServerResourceRec-4503d9f8
#>

#Requires -Modules DnsServer
function Update-DnsServerResourceRecordA
{
    [CmdletBinding()]
    #[OutputType([Microsoft.Management.Infrastructure.CimInstance#root/Microsoft/Windows/DNS/DnsServerResourceRecord])]
    Param
    (
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [string[]]$ComputerName=$env:COMPUTERNAME,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        [string]$ZoneName=$env:USERDNSDOMAIN,
        
        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        [string]$RecordType="A",
        
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=3)]
        [string]$Name,
        
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=4)]
        [ipaddress]$IPAddress,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=5)]
        [switch]$Force=$false,

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=6)]
        [int]$RRIndex=0

    )

    Begin
    {
    }
    Process
    {
        foreach ($Computer in $ComputerName) {
            if (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
                
                # Get the current resource record.
                try {
                    $OldRR = Get-DnsServerResourceRecord -ComputerName $Computer -Name $Name -RRType $RecordType -ZoneName $ZoneName -ErrorAction Stop
                    $NewRR = Get-DnsServerResourceRecord -ComputerName $Computer -Name $Name -RRType $RecordType -ZoneName $ZoneName -ErrorAction Stop
                    
                    # Ensure that the resource record exists before proceeding.
                    if ($NewRR -and $OldRR) {
                        if ($OldRR.Count) {
                            # More than one record found.
                            $NewRR[$RRIndex].RecordData.IPv4Address=[ipaddress]$IPAddress
                            $UpdatedRR = Set-DnsServerResourceRecord -NewInputObject $NewRR[$RRIndex] -OldInputObject $OldRR[$RRIndex] -ZoneName $ZoneName -ComputerName $Computer -PassThru
                            $UpdatedRR
                            }
                        else {
                            $NewRR.RecordData.IPv4Address=[ipaddress]$IPAddress
                            $UpdatedRR = Set-DnsServerResourceRecord -NewInputObject $NewRR -OldInputObject $OldRR -ZoneName $ZoneName -ComputerName $Computer -PassThru
                            $UpdatedRR
                            }
                        }
                    }
                catch {
                    # If it doesn't exist create it if the -Force parameter.
                    if ($Force) {
                        $NewRR = Add-DnsServerResourceRecordA -ComputerName $Computer -Name $Name -ZoneName $ZoneName -IPv4Address $IPAddress -PassThru -AllowUpdateAny
                        $NewRR
                        }
                    else {
                        Write-Error "Existing record $Name.$ZoneName does not exist. Use -Force to create it."
                        }
                    }
                }
            else {
                Write-Error "Unable to connect to $Computer"
                }
            }
    }
    End
    {
    }
}