GinShell.GoDaddy/Public/Add-GsGoDaddyPointer.ps1
|
function Add-GsGoDaddyPointer { [CmdletBinding()] param ( [Parameter(Mandatory)] [string]$FullDomainName, [Parameter(Mandatory = $false)] [ValidateSet("A", "CNAME")] [string]$Type = 'CNAME', [Parameter(Mandatory)] [string]$Target, [switch]$Force ) $dns = $FullDomainName.Split('.') $len = $dns.Length $name = ($dns[0..($len - 3)] -join '.') $domain = "$($dns[-2]).$($dns[-1])" $creds = Get-GoDaddyCreds $headers = @{ Authorization = "sso-key $($creds.Key):$($creds.Secret)" } $uri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name" $existing = $null try { $existing = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET -ErrorAction Stop } catch {} if ($existing -and -not $Force) { Write-GsLog -Message "Record already exists: $($existing.data)" -Type Error return } $body = @( @{ data = $Target; name = $name; ttl = 3600; type = $Type } ) | ConvertTo-Json -Depth 3 if ($existing -and $Force) { Write-GsLog -Message "An existing DNS record for '$FullDomainName' was found pointing to '$($existing.data)'. Proceeding to update it as the -Force flag is specified." -Type Warning $patchUri = "https://api.godaddy.com/v1/domains/$domain/records/$Type/$name" try { Invoke-RestMethod -Uri $patchUri -Headers $headers -Method PUT -Body [$body] -ContentType 'application/json' Write-GsLog -Message "Updated $name.$domain -> $Target" -Type Success } catch { Write-GsLog -Message "Failed to update record: $_" -Type Error } return } $createUri = "https://api.godaddy.com/v1/domains/$domain/records" try { Invoke-RestMethod -Uri $createUri -Headers $headers -Method PATCH -Body [$body] -ContentType 'application/json' Write-GsLog -Message "Created $name.$domain -> $Target" -Type Success } catch { Write-GsLog -Message "Failed to create record: $_" -Type Error } } |