How to export Custom Domains from an Azure Web App with PowerShell


Created a simple script for listing and exporting Custom Domains from an Azure Web App.

Finding the suitable cmdlet for the job

In the creation of the script first step was to find the cmdlet for listing information about Web Apps. A handy tool is to use the cmdlet Get-Command "*Get-AzWebApp* and filter by az module and Web App information. The result looks something like this:

PS C:\Get-Command "*Get-AzWebApp*

    CommandType     Name                                               Version    Source
    -----------     ----                                               -------    ------
    Function        Get-AzWebAppCustomHostname
    Cmdlet          **Get-AzWebApp**                                       1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppBackup                                 1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppBackupConfiguration                    1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppBackupList                             1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppCertificate                            1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppContainerContinuousDeploymentUrl       1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppMetrics                                1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppPublishingProfile                      1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSlot                                   1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSlotConfigName                         1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSlotMetrics                            1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSlotPublishingProfile                  1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSnapshot                               1.0.0      Az.Websites
    Cmdlet          Get-AzWebAppSSLBinding                             1.0.0      Az.Websites

Listing Custom Hostnames bound to a Web App

To list out custom hostnames I used the second cmdlet from the list above Get-AzWebApp. The script takes to mandatory parameters, -ResourceGroupName and -Name, and the output looks something like this:

PS C:\>Get-AzWebApp -ResourceGroupName "webapp-stage-rg" -Name "webapp7110-stage"

    GitRemoteName               :
    GitRemoteUri                :
    GitRemoteUsername           :
    GitRemotePassword           :
    AzureStoragePath            : {}
    State                       : Running
    HostNames                   : {webapp7110-stage.azurewebsites.net}
    RepositorySiteName          : webapp7110-stage
    UsageState                  : Normal
    Enabled                     : True
    EnabledHostNames            : {webapp7110-stage.azurewebsites.net, webapp7110-stage.scm.azurewebsites.net}
    AvailabilityState           : Normal
    HostNameSslStates           : {webapp7110-stage.azurewebsites.net, webapp7110-stage.scm.azurewebsites.net}
    ServerFarmId                : /subscriptions/3a5001ab-15ef-46e2-814e-6b29fd23927c/resourceGroups/webapp-stage-rg/providers/Microsoft.Web/serverfarms/webapp7110-stage
    Reserved                    : False
    IsXenon                     : False
    LastModifiedTimeUtc         : 05.09.2019 10:54:32
    SiteConfig                  : Microsoft.Azure.Management.WebSites.Models.SiteConfig
    TrafficManagerHostNames     :
    ScmSiteAlsoStopped          : False
    TargetSwapSlot              :
    HostingEnvironmentProfile   :
    ClientAffinityEnabled       : True
    ClientCertEnabled           : False
    HostNamesDisabled           : False
    OutboundIpAddresses         : 40.68.205.178,13.95.10.43,13.95.11.97,52.166.198.114,40.68.101.225
    PossibleOutboundIpAddresses : 40.68.205.178,13.95.10.43,13.95.11.97,52.166.198.114,40.68.101.225,51.144.109.85,52.233.165.183
    ContainerSize               : 0
    DailyMemoryTimeQuota        : 0
    SuspendedTill               :
    MaxNumberOfWorkers          :
    CloningInfo                 :
    ResourceGroup               : webapp-stage-rg
    IsDefaultContainer          :
    DefaultHostName             : webapp7110-stage.azurewebsites.net
    SlotSwapStatus              :
    HttpsOnly                   : False
    Identity                    :
    Id                          : /subscriptions/3a5001ab-15ef-46e2-814e-6b29fd23927c/resourceGroups/webapp-stage-rg/providers/Microsoft.Web/sites/webapp7110-stage
    Name                        : webapp7110-stage
    Kind                        :
    Location                    : West Europe
    Type                        : Microsoft.Web/sites
    Tags                        :

In the output above you can see a property named Hostnames (look for the bold one). We need this property to get access and list the hostnames. To do this we can run the cmdlet below. In the output of the cmdlet there is no other hostname than the default, so its not a large list per say.

PS C:\>(Get-AzWebApp -ResourceGroupName "webapp-stage-rg" -Name "webapp7110-stage").HostNames

webapp7110-stage.azurewebsites.net

This is pretty much the script for listing/collecting a list of hostnames bound to an Azure Web App, but a made a few more modification.

Creating export possibilities

I made some further enhancements to the script by adding an export possibility. I did this creating the cmdlet above into an advanced function and added a Swith parameter. When the switch parameter is used, by running the script with -OutPutToFile, like the cmdlet below; the script prompts you for the file path and filename, then it opens the txt-file in notepad. If you don’t use the switch parameter it lists the Hostnames in the console, same as the cmdlet above.

Get-AzWebAppCustomHostname -ResourceGroupName "webapp-stage-rg" -WebAppName "webapp7110-stage" -OutputToFile
function Get-AzWebAppCustomHostname {
    [CmdletBinding()]
    param (
        # Name of the Web App Resource Group
        [Parameter(Mandatory = $true)]
        [String]
        $ResourceGroupName,

        # Name of the App Service
        [Parameter(mandatory = $true)]
        [String]
        $WebAppName,

        # Optional, if you want to output the list to a .txt file
        [Parameter(Mandatory = $false)]
        [switch]
        $OutputToFile = $false
    )
    process {
         $Hostnames = (Get-AzWebApp -ResourceGroupName $ResourceGroupName -Name $WebAppName).HostNames

         if ($OutputToFile -eq $true) {
            $FilePath = Read-Host "Choose filepath..."
            $FileName = Read-Host "Choose filename..."
            $Hostnames | Out-File -FilePath "$FilePath\$FileName.txt" -Force

            notepad.exe "$FilePath\$FileName.txt"
         }
         else {
            Write-Output $Hostnames
         }
    }
}

The result from the script when using the switch parameter.

Result from the script using the switch parameter

A little cool experiment for learning more about the switch parameter and exporting to file operations with PowerShell, and with that have a great day!


For More Content See the Latest Posts