Azure 函数和 PowerShell:无法使用 Set-AzWebApp 设置连接字符串

Azure Function and PowerShell: unable to set connection strings with Set-AzWebApp

出于部署目的,我创建了一个 PowerShell 脚本来设置应用程序设置。通过

这很好用
$currentAppSettings = $app.SiteConfig.AppSettings

$appSettings = @{}
# Add existing App Settings
ForEach ($currentAppSetting in $currentAppSettings) {
    $appSettings[$currentAppSetting.Name] = $currentAppSetting.Value
}

# Add new App Settings
$appSettings["someKey"] = "someValue"

# Update App Settings
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -AppSettings $appSettings

因为我现在正在使用Entity Framework我还需要设置连接字符串。首先,我认为这应该像应用程序设置一样工作,只需添加 ConnectionStrings 参数:

$currentConnectionStrings = $app.SiteConfig.ConnectionStrings

$connectionStrings = @{}

ForEach ($currentConnectionString in $currentConnectionStrings) {
    $connectionStrings[$currentConnectionString.Name] = $currentConnectionString.ConnectionString
}

$connectionStrings["someKey"] = "someValue"

Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings

但这失败并出现错误

Set-AzWebApp : Cannot validate argument on parameter 'ConnectionStrings'. Connection string type value pair must be of type 'System.Collections.Hashtable'

尽管 $connectionStrings 的类型是 System.Collections.Hashtable

也尝试使用自定义对象、数组等失败。

如何正确传递连接字符串? 如何指定类型(例如 SQLAZURE)?

谢谢

根据 Set-AzwebApp 的文档 ConnectionStrings 应该是 Hastable

Set-AzWebApp
   [[-AppServicePlan] <String>]
   [[-DefaultDocuments] <String[]>]
   [[-NetFrameworkVersion] <String>]
   [[-PhpVersion] <String>]
   [[-RequestTracingEnabled] <Boolean>]
   [[-HttpLoggingEnabled] <Boolean>]
   [[-DetailedErrorLoggingEnabled] <Boolean>]
   [[-AppSettings] <Hashtable>]
   [[-ConnectionStrings] <Hashtable>]
   [[-HandlerMappings] <System.Collections.Generic.IList`1[Microsoft.Azure.Management.WebSites.Models.HandlerMapping]>]
   [[-ManagedPipelineMode] <String>]
   [[-WebSocketsEnabled] <Boolean>]
   [[-Use32BitWorkerProcess] <Boolean>]
   [[-AutoSwapSlotName] <String>]
   [-ContainerImageName <String>]
   [-ContainerRegistryUrl <String>]
   [-ContainerRegistryUser <String>]
   [-ContainerRegistryPassword <SecureString>]
   [-EnableContainerContinuousDeployment <Boolean>]
   [-HostNames <String[]>]
   [-NumberOfWorkers <Int32>]
   [-AsJob]
   [-AssignIdentity <Boolean>]
   [-HttpsOnly <Boolean>]
   [-AzureStoragePath <WebAppAzureStoragePath[]>]
   [-ResourceGroupName] <String>
   [-Name] <String>
   [-DefaultProfile <IAzureContextContainer>]
   [<CommonParameters>]

您应该使用 Hashtable 设置 connectionstring,如下所示:

$connectionStrings = @{connectionStrings = @{Name="<ConnectionStringName>";Value="<ConnectionSyring>";type="<SQLAzure/SQLServer/Custom/MySql>"}}

Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings

@KetanChawda-MSFT:感谢您的建议,它对我开发解决方案有很大帮助(但在我的上下文中并没有像下面提到的那样工作)。

这是我现在的工作方式:

$currentConnectionStrings = $app.SiteConfig.ConnectionStrings
$connectionStrings = @{}

# Add existing connection strings
ForEach ($currentConnectionString in $currentConnectionStrings) {
    $connectionStrings[$currentConnectionString.Name] =  
        @{
            Value=$currentConnectionString.ConnectionString;
            Type=($currentConnectionString.Type | Out-String).ToUpper()
        }
}

# Add database connection string
$connectionStrings["connName"] = @{
    Value="connString";
    Type="SQLAZURE"
}

# Update settings
Set-AzWebApp -ResourceGroupName $resourceGroupName -Name $appName -ConnectionStrings $connectionStrings

有趣的是,在现有连接字符串的情况下,我必须将其格式化为字符串并使用 toUpper,否则它对我不起作用(错误:Set-AzWebApp : Cannot validate argument on parameter 'ConnectionStrings'. Connection string type must be specified.)。添加新的连接字符串时,我可以使用例如SQLAZURE 以及 SQLAzure.

编辑:

这是一种在使用 Entity Framework 和 Azure Functions 时根本不需要连接字符串部分的方法: