Exporting all Azure Web App settings for every app on every resource group for both production and staging slots to CSV

I've currently got about 40-50 apps running on Azure's App service offering being deploying using Azure ARM deployment templates. Each app contains AppSettings. There were times when I've noticed that some apps don't get all the AppSettings defined in the ARM template so I wrote a script to print and export the list of appsettings for each of my 40-50 apps for sanity checking.

$allWebApps = Get-AzureRmWebApp
#$allWebApps = $allWebApps | Where-Object {$_.ResourceGroup -eq 'nameOfresourceGroup'}
$resourceGroups = $allWebApps | Select-Object 'ResourceGroup' -Unique

$outItems = New-Object System.Collections.Generic.List[System.Object]


Function Process-App{

Param ([System.Array]$settingsArray,[String]$siteName)

    $collection = {$settingsArray}.Invoke()
    $webAppNameObj = [PSCustomObject]@{Key = 'WebAppName'; Value = $siteName }            
    $emptyLine = [PSCustomObject]@{ Key = ''; Value = ''}
    $collection.Add($webAppNameObj)
    $collection.Add($emptyLine)

    $collection = $collection | Sort-Object -Property Name         
    $collection | Export-Csv -Path C:\Azure\allAppSettings.csv -NoTypeInformation -Append

    $outItems.AddRange($collection) 
}

foreach($r in $resourceGroups)
{
    $rgName = $r.ResourceGroup    
    $webApps = (Get-AzureRmWebApp -ResourceGroupName $rgName | Sort-Object -Property Name)    

    foreach($w in $webApps)
    {
        Write-Host Processing Webapp : $w.Name

        $webApp = Get-AzureRmWebApp -ResourceGroupName $rgName -Name $w.Name
        $webAppSlots = Get-AzureRmWebAppSlot -WebApp $webApp

        foreach($s in $webAppSlots)
        {
            $indexOf = $s.Name.IndexOf('/')
            $slotName = $s.Name.SubString($indexOf+1)
            $slot = Get-AzureRmWebAppSlot -WebApp $webApp -Slot $slotName
            $slotSettings = $slot.SiteConfig.AppSettings.GetEnumerator() | Select-Object -Property @{n='Key';e={$_.Name}},Value

            Process-App -settingsArray $slotSettings -siteName $s.Name
        }

         $appSettings = $webApp.SiteConfig.AppSettings        
         $settings = $appSettings.GetEnumerator() | Select-Object -Property @{n='Key';e={$_.Name}},Value                 
         
         Process-App -settingsArray $settings -siteName $w.Name
    }    
}

$outItems | Format-Table -AutoSize
comments powered by Disqus