Skip to content

Instantly share code, notes, and snippets.

@jasongaylord
Created April 29, 2019 18:43
Show Gist options
  • Save jasongaylord/1ee3a1060cfcbcabe133f315d08fbfb1 to your computer and use it in GitHub Desktop.
Save jasongaylord/1ee3a1060cfcbcabe133f315d08fbfb1 to your computer and use it in GitHub Desktop.
A script to create a new CName on an existing zone in Azure. You can use this script in an Azure DevOps pipeline using a service account with the proper permissions.
param(
[CmdletBinding()]
# Subscription ID
[Parameter(Position=0, Mandatory=$true)]
[string]$subId,
# DNS Resource Group Name
[Parameter(Position=1, Mandatory=$true)]
[string]$resourceGroup,
# DNS Zone Name
[Parameter(Position=2, Mandatory=$true)]
[string]$zoneName,
# CName Record Name
[Parameter(Position=3, Mandatory=$true)]
[string]$cnameName,
# Value CName Record Points To
[Parameter(Position=4, Mandatory=$true)]
[string]$cnameValue
)
#If the Azure AZ module is not loaded, load it.
if(-not (Get-Module Az)) {
Install-Module -Name Az -AllowClobber -Scope CurrentUser -SkipPublisherCheck
}
#Set Azure Context
Set-AzContext -SubscriptionId $subId
#Get the record set to see if it already exists and if not, create it
New-AzDnsRecordSet -Name $cnameName -RecordType CNAME -ZoneName $zoneName -ResourceGroupName $resourceGroup -Ttl 3600 -DnsRecords (New-AzDnsRecordConfig -Cname $cnameValue) -Overwrite -Confirm:$False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment