Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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