-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSet-GoDaddyDNS.ps1
165 lines (132 loc) · 6.64 KB
/
Set-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
<#
.Synopsis
Updates DNS records.
.DESCRIPTION
Updates DNS records for domains hosted with GoDaddy. If multiple records exist with the same name and type, Set-GoDaddyDNS will replace them all.
.EXAMPLE
Set-GoDaddyDNS -Domain google.com -Type A -Name mail -IP 8.8.8.8
This example creates an A records for google.com with the name mail and an IP of 8.8.8.8.
#>
function Set-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
{
$apiKey = Import-Csv "$PSScriptRoot\apiKey.csv"
}
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 = @{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 = @{data="$Data";priority=$Priority;ttl=$TTL}
$body = "[" + (ConvertTo-Json $record) + "]"
}
#---- Build standard record ----#
else {
$record = @{data="$Data";ttl=$TTL}
$body = "[" + (ConvertTo-Json $record) + "]"
}
#---- Build the request URI based on domain ----#
$uri = "https://api.godaddy.com/v1/domains/$Domain/records/$Type/$Name"
#---- Make the request ----#
Invoke-WebRequest -Uri $uri -Method Put -Headers $headers -Body $body -UseBasicParsing | Out-Null
#---- Validate record with Get-GoDaddyDNS ----$
Get-GoDaddyDNS -Domain $Domain | Where-Object {$_.type -eq $Type -and $_.name -eq $Name}
}
End
{
}
}