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

问题描述 投票:0回答:2

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

正如我现在使用的实体框架我还需要设置连接字符串。首先,我认为这应该像App Settings一样工作,只需添加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”类型

尽管$connectionStringsSystem.Collections.Hashtable类型的事实

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

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

谢谢

entity-framework powershell azure-functions connection-string
2个回答
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


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

编辑:

下面是一种在使用Entity Framework和Azure Functions时根本不需要连接字符串部分的方法:Missing ProviderName when debugging AzureFunction as well as deploying azure function

© www.soinside.com 2019 - 2024. All rights reserved.