Set Up RDNS in Azure for a public IP

When setting up an Azure VM that will have mail functions, you may need to set up a rDNS record. The Azure portal at this time does not have that capability but you can set this up in with the Azure CLI or PowerShell.

Guide for creating Reverse DNS records: https://docs.microsoft.com/en-us/azure/dns/dns-reverse-dns-for-azure-services

Here’s a PowerShell script that can set PTR records:
Set the variables appropriately.

$TenantID = "insert_tenant_id" # Customer Tenant ID
$SubscriptionID = "insert_subscripton_id" # Customer's CSP Subscription ID
$UniqueName = "exampleIP" # A Unique Name to associate with the IP, doesn't really matter
$RealFQDN = "mail.example.com." # The actual PTR/rDNS record to set on the IP Address
$IpAddress = "insert_ip" # Insett he IP you are settingthe rDNS for. 

$ProvisioningCredentials = Get-Credential -Message "Enter Your @CoderoHosting or @Coderosandbox Azure Account Credentials"
    try {
    Write-Host "Checking Credentials" -ForegroundColor Green
    Login-AzureRmAccount -Credential $ProvisioningCredentials | Out-Null
    
    $context = Get-AzureRmContext
    if ($context.Account -eq $null) {throw "Error"}
    Write-Host "Credentials are valid.  Continuing..." -ForegroundColor Green
    $validcredentials = $true
    $context = $null
    }
    
    catch {
    Write-Host "Invalid Azure login credentials.  Please try to enter your credentials again." -ForegroundColor Red
    $validcredentials = $false
    $context = $null
    }

Try {
    Write-Host "Logging in to Customer Azure Subscription..." -ForegroundColor Green
    Login-AzureRmAccount -TenantId $TenantID -Credential $ProvisioningCredentials
    Select-AzureRmSubscription -SubscriptionId $SubscriptionID
}
Catch {
    Write-Host "Not able to log in to Customer Azure Subscription.  Check that your Azure credentials are in the AdminUsers AAD Group in the coderohosting or coderosandbox tenant. Exiting script. Error message is:"
    Write-Host $PSItem.tostring() -ForegroundColor Red
    Break
}

$pip = Get-AzureRmPublicIpAddress | Where-Object {$_.IpAddress -like $IpAddress}

$pip.DnsSettings = New-Object -TypeName "Microsoft.Azure.Commands.Network.Models.PSPublicIpAddressDnsSettings"

$pip.DnsSettings.DomainNameLabel = $UniqueName

$pip.DnsSettings.ReverseFqdn = $RealFQDN

Set-AzureRmPublicIpAddress -PublicIpAddress $pip