-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathAdd-GoDaddyDNS.ps1
176 lines (142 loc) · 7.08 KB
/
Add-GoDaddyDNS.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<#
.Synopsis
Adds new DNS records.
.DESCRIPTION
Adds a new DNS record for domains hosted with GoDaddy. Useful for creating multiple records with the same name.
.EXAMPLE
Add-GoDaddyDNS clintcolding.com -Type A -Name "test" -IP 10.10.10.14
type name data ttl
---- ---- ---- ---
A test 10.10.10.13 3600
A test 10.10.10.14 3600
#>
function Add-GoDaddyDNS
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true,
Position=0)]
[string]$Domain,
[Parameter(Mandatory=$true,
Position=1)]
[ValidateSet('A','CNAME','MX','TXT','NS','SRV','AAAA')]
[string]$Type,
[Parameter(Mandatory=$true,
Position=2)]
[string]$Name,
[Parameter(Mandatory=$true,
Position=3)]
[string]$Data,
[Parameter(Position=4)]
[int]$TTL=3600,
[Parameter(Position=5)]
[int]$Priority=0
)
DynamicParam {
if ($Type -eq "SRV") {
# Inititalize runtime dictionary
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
# Priority ParameterAttribute object
$priorityAttribute = New-Object System.Management.Automation.ParameterAttribute
$priorityAttribute.Mandatory = $true
$priorityAttribute.HelpMessage = "Please enter record priority:"
# AttributeCollection object for the above attribute
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add attribute to collection
$attributeCollection.Add($priorityAttribute)
# Add paramater specifying the attribute collection
$priorityParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Priority', [int32], $attributeCollection)
# Service ParameterAttribute object
$serviceAttribute = New-Object System.Management.Automation.ParameterAttribute
$serviceAttribute.Mandatory = $true
$serviceAttribute.HelpMessage = "Please enter SRV service:"
# AttributeCollection object for the above attribute
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add attribute to collection
$attributeCollection.Add($serviceAttribute)
# Add paramater specifying the attribute collection
$serviceParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Service', [string], $attributeCollection)
# Protocol ParameterAttribute object
$protocolAttribute = New-Object System.Management.Automation.ParameterAttribute
$protocolAttribute.Mandatory = $true
$protocolAttribute.HelpMessage = "Please enter SRV protocol:"
# AttributeCollection object for the above attribute
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add attribute to collection
$attributeCollection.Add($protocolAttribute)
# Add paramater specifying the attribute collection
$protocolParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Protocol', [string], $attributeCollection)
# Port ParameterAttribute object
$portAttribute = New-Object System.Management.Automation.ParameterAttribute
$portAttribute.Mandatory = $true
$portAttribute.HelpMessage = "Please enter SRV port:"
# AttributeCollection object for the above attribute
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add attribute to collection
$attributeCollection.Add($portAttribute)
# Add paramater specifying the attribute collection
$portParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Port', [int32], $attributeCollection)
# Weight ParameterAttribute object
$weightAttribute = New-Object System.Management.Automation.ParameterAttribute
$weightAttribute.Mandatory = $true
$weightAttribute.HelpMessage = "Please enter SRV weight:"
# AttributeCollection object for the above attribute
$attributeCollection = New-Object System.Collections.ObjectModel.Collection[System.Attribute]
# Add attribute to collection
$attributeCollection.Add($weightAttribute)
# Add paramater specifying the attribute collection
$weightParam = New-Object System.Management.Automation.RuntimeDefinedParameter('Weight', [int32], $attributeCollection)
#Add the names of our parameters
$paramDictionary.Add('Priority', $priorityParam)
$paramDictionary.Add('Service', $serviceParam)
$paramDictionary.Add('Protocol', $protocolParam)
$paramDictionary.Add('Port', $portParam)
$paramDictionary.Add('Weight', $weightParam)
return $paramDictionary
}
}
Begin
{
$apiKeySecure = Import-Csv "$PSScriptRoot\apiKey.csv"
# Decrypt API Key
$apiKey = @(
[PSCustomObject]@{
Key = [System.Net.NetworkCredential]::new("", ($apiKeySecure.Key | ConvertTo-SecureString)).Password
Secret = [System.Net.NetworkCredential]::new("", ($apiKeySecure.Secret | ConvertTo-SecureString)).Password
}
)
}
Process
{
#---- Build authorization header ----#
$headers = @{}
$headers["Authorization"] = 'sso-key ' + $apiKey.key + ':' + $apiKey.secret
$headers["Content-Type"] = "application/json"
$headers["Accept"] = "application/json"
#---- If SRV, build SRV record ----#
if ($Type -eq "SRV") {
$record = @{type="$Type";name="$Name";data="$Data";ttl=$TTL;priority=$PSBoundParameters.Priority;service="{0}" -f $PSBoundParameters.Service;protocol="{0}" -f $PSBoundParameters.Protocol;port=$PSBoundParameters.Port;weight=$PSBoundParameters.Weight}
$body = "[" + (ConvertTo-Json $record) + "]"
}
#---- If MX, build MX record ----#
if ($Type -eq "MX") {
$record = @{type="$Type";name="$Name";data="$Data";priority=$Priority;ttl=$TTL}
$body = "[" + (ConvertTo-Json $record) + "]"
}
#---- Build standard record ----#
else {
$record = @{type="$Type";name="$Name";data="$Data";ttl=$TTL}
$body = "[" + (ConvertTo-Json $record) + "]"
}
#---- Build the request URI based on domain ----#
$uri = "https://api.godaddy.com/v1/domains/$Domain/records"
#---- Make the request ----#
Invoke-WebRequest -Uri $uri -Method Patch -Headers $headers -Body $body -UseBasicParsing | ConvertFrom-Json
#---- Validate record with Get-GoDaddyDNS ----$
Get-GoDaddyDNS -Domain $Domain | Where-Object {$_.type -eq $Type -and $_.name -eq $Name -and $_.data -eq $Data}
}
End
{
}
}