使用 PowerShell 获取要交换的 Azure Web 应用设置列表

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

当我们通过 Azure 门户执行交换时,它会向我们提供有关要交换的设置的警告和信息性消息。如下图所示:

我的问题是,有什么方法可以通过 PowerShell 代码获取这些消息列表(关于设置)?

我尝试用谷歌搜索,但找不到任何方法。

直接的解决方案是使用Invoke-RestMethod请求在Portal中捕获的API。问题是,这是一个非public API,不知道有没有变。

您可以使用 PowerShell 获取要交换的两个对象,获取它们的 appSettingsConnectionStrings,然后比较它们。 以下是供参考的脚本。

当Source和Destination不同时,脚本可以得到:
• 要添加到目的地
• 要从 Destination
中删除的目的地 • 已交换

$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

希望对你有帮助。