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

时间:2019-04-03 05:04:38

标签: entity-framework powershell azure-functions connection-string

出于部署目的,我创建了一个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

当我现在使用实体框架时,我还需要设置连接字符串。首先,我认为这应该与“应用程序设置”一起使用,并且只需添加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:无法验证参数'ConnectionStrings'上的参数。连接字符串类型值对必须为“ System.Collections.Hashtable”类型

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

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

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

谢谢

2 个答案:

答案 0 :(得分:3)

根据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>]

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

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

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

答案 1 :(得分:0)

@ 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.)。当添加新的连接字符串时,我可以使用例如SQLAZURESQLAzure

编辑:

这是一种使用实体框架和Azure函数时根本不需要连接字符串部分的方法:Missing ProviderName when debugging AzureFunction as well as deploying azure function