public/Set-GDDomainRecord.ps1

Set-StrictMode -Version Latest

function Set-GDDomainRecord {
   [CmdletBinding(DefaultParameterSetName = 'Secure', SupportsShouldProcess = $true, ConfirmImpact = "High")]
   param(
      [parameter(ParameterSetName = 'Secure', Mandatory = $true, HelpMessage = 'API Key')]
      [string] $key,

      [parameter(ParameterSetName = 'Secure', Mandatory = $true, HelpMessage = 'API Key Secret')]
      [securestring] $secret,

      [parameter(ParameterSetName = 'Credentials', Mandatory = $true)]
      [System.Management.Automation.PSCredential] $credentials,

      [parameter(Mandatory = $true)]
      [string] $domain,

      [parameter(Mandatory = $true)]
      [string] $type,

      [parameter(Mandatory = $true)]
      [string] $name,

      [parameter(Mandatory = $true)]
      [string] $ipAddress,

      [int] $ttl = 600,

      [switch]$Force
   )

   Begin {
   }

   Process {
      if ($credentials) {
         $key = $credentials.UserName
         $PlainSecret = _convertSecureStringTo_PlainText -SecureString $credentials.Password
      }
      else {
         $PlainSecret = _convertSecureStringTo_PlainText -SecureString $secret
      }

      $body = @{
         data = "$ipAddress"
         ttl  = $ttl
      }

      Write-Verbose "Body $body"

      # It is very important that even if the user only provides
      # a single value above that the item is an array and not
      # a single object or the call will fail.
      # You must call ConvertTo-Json passing in the value and not
      # not using pipeline.
      # https://stackoverflow.com/questions/18662967/convertto-json-an-array-with-a-single-item
      $json = ConvertTo-Json @($body) -Compress

      Write-Verbose "json $json"

      $Headers = @{accept = 'application/json'; Authorization = "sso-key $($key):$($PlainSecret)"}
      if ($Force -or $PSCmdlet.ShouldProcess($name, "Update domain DNS record")) {
         Invoke-RestMethod -Uri "https://api.godaddy.com/v1/domains/$($domain)/records/$($type)/$($name)" -Headers $Headers -Method Put -Body $json -ContentType 'application/json'
      }
   }

   End {      
   }
}