Search code examples
powershellazureswapazure-web-app-service

Get the list of azure web app settings to be swapped using PowerShell


When we perform the swap through the azure portal, it gives us the warning & informative messages regarding the settings to be swapped. Just like in below image: enter image description here

My question is, is there any way I can get these messages list (regarding the settings) through PowerShell code?

I tried googling it but couldn't find any way.


Solution

  • The direct solution is to use the Invoke-RestMethod to request the API captured in the Portal. The problem is that this is a non-public API, and we don't know if it has changed.

    You could use PowerShell to get the two objects to be exchanged, gets their appSettings and ConnectionStrings, and compares them. The following is a script for reference.

    When Source and Destination are different, the script can get:
    • To be added in the Destination
    • The destination to be removed from the Destination
    • Exchanged

    $rsgName = "xxxxx"
    $appName = "xxxxx"
    $slotName = "xxxxxx"
    
    $destination = Get-AzureRmWebApp -ResourceGroupName $rsgName -Name $appName
    $destinationAppSettings = $destination.SiteConfig.AppSettings
    $destinationConnectionStrings  = $destination.SiteConfig.ConnectionStrings
    
    $source = Get-AzureRmWebAppSlot -ResourceGroupName $rsgName -Name $appName -Slot $slotName
    $sourceAppSettings = $source.SiteConfig.AppSettings
    $sourceConnectionStrings = $source.SiteConfig.ConnectionStrings
    
    #Get slot configurations
    $slotConfigure = Get-AzureRmWebAppSlotConfigName -ResourceGroupName $rsgName -Name $appName
    
    $toBeAdded = New-Object System.Collections.ArrayList
    $toBeSwapped = New-Object System.Collections.ArrayList
    $toBeDeleted = New-Object System.Collections.ArrayList
    
    foreach($appSetting in $sourceAppSettings){
        if(-not $slotConfigure.AppSettingNames.Contains($sourceAppSettings.Name)){
            $flag = $true
            foreach($_appSetting in $destinationAppSettings){
                if($_appSetting.Name -eq $appSetting.Name){
                    $flag = $false
                    [void]$toBeSwapped.Add([pscustomobject]@{Name = $appSetting.Name; Source = $appSetting.Value; Destination = $_appSetting.Value})
                }
            }
            if($flag){
                [void]$toBeAdded.Add($appSetting)
            }
        }
    }
    
    foreach($appSetting in $destinationAppSettings){
        $flag = $true
        foreach($_appSetting in $sourceAppSettings){
            if($_appSetting.Name -eq $appSetting.Name){
                $flag = $false
            }
        }
        if($flag){
            [void]$toBeDeleted.Add($appSetting)
        }
    }
    
    
    # AppSettings
    # To be added to destination
    $toBeAdded 
    
    # To be swapped to destination
    $toBeSwapped 
    
    # To be delete in destination
    $toBeDeleted 
    
    
    $toBeAdded = New-Object System.Collections.ArrayList
    $toBeSwapped = New-Object System.Collections.ArrayList
    $toBeDeleted = New-Object System.Collections.ArrayList
    
    foreach($connectionString in $sourceConnectionStrings){
        if(-not $slotConfigure.ConnectionStringNames.Contains($connectionString.Name)){
            $flag = $true
            foreach($_connectionString in $destinationConnectionStrings){
                if($_connectionString.Name -eq $connectionString.Name){
                    $flag = $false
                    [void]$toBeSwapped.Add([pscustomobject]@{Name = $connectionString.Name; Source = $connectionString.Value; Destination = $_connectionString.Value})
                }
            }
            if($flag){
                [void]$toBeAdded.Add($connectionString)
            }
        }
    }
    
    foreach($connectionString in $destinationConnectionStrings){
        $flag = $true
        foreach($_connectionString in $sourceConnectionStrings){
            if($_connectionString.Name -eq $connectionString.Name){
                $flag = $false
            }
        }
        if($flag){
            [void]$toBeDeleted.Add($connectionString)
        }
    }
    
    
    # ConnectionStrings
    # To be added to destination
    $toBeAdded 
    
    # To be swapped to destination
    $toBeSwapped 
    
    # To be delete in destination
    $toBeDeleted
    

    Hope it helps you.