GinShell.GoDaddy/Public/Add-GsGoDaddyPointer.ps1
|
function Add-GsGoDaddyPointer { <# .SYNOPSIS Creates a DNS record (A or CNAME) via the GoDaddy API. .DESCRIPTION If a record already exists and -Force is specified, updates it instead. .PARAMETER FullDomainName Fully qualified domain name (e.g., app.example.com). .PARAMETER Type DNS record type: A or CNAME. Default: CNAME. .PARAMETER Target The target value (IP address for A, hostname for CNAME). .PARAMETER Force Update the record if it already exists. .EXAMPLE Add-GsGoDaddyPointer -FullDomainName 'app.example.com' -Target 'lb.example.com' #> [CmdletBinding(SupportsShouldProcess)] param ( [Parameter(Mandatory)] [string]$FullDomainName, [ValidateSet('A', 'CNAME')] [string]$Type = 'CNAME', [Parameter(Mandatory)] [string]$Target, [switch]$Force ) $dns = Resolve-GoDaddyDomain -FullDomainName $FullDomainName $headers = Get-GoDaddyHeaders $uri = "https://api.godaddy.com/v1/domains/$($dns.Domain)/records/$Type/$($dns.Name)" # Check existing $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). Use -Force to overwrite." -Type Error return } $body = @(@{ data = $Target; name = $dns.Name; ttl = 3600; type = $Type }) | ConvertTo-Json -Depth 3 if ($existing -and $Force) { Write-GsLog -Message "Existing record found for '$FullDomainName' pointing to '$($existing.data)'. Updating (-Force)." -Type Warning if (-not $PSCmdlet.ShouldProcess($FullDomainName, "Update $Type record to '$Target'")) { return } try { Invoke-RestMethod -Uri $uri -Headers $headers -Method PUT -Body "[$body]" -ContentType 'application/json' -ErrorAction Stop Write-GsLog -Message "Updated $($dns.Name).$($dns.Domain) -> $Target" -Type Success } catch { Write-GsLog -Message "Failed to update record: $_" -Type Error } return } if (-not $PSCmdlet.ShouldProcess($FullDomainName, "Create $Type record pointing to '$Target'")) { return } $createUri = "https://api.godaddy.com/v1/domains/$($dns.Domain)/records" try { Invoke-RestMethod -Uri $createUri -Headers $headers -Method PATCH -Body "[$body]" -ContentType 'application/json' -ErrorAction Stop Write-GsLog -Message "Created $($dns.Name).$($dns.Domain) -> $Target" -Type Success } catch { Write-GsLog -Message "Failed to create record: $_" -Type Error } } |