AzureRM Web App - 如何使用 Powershell 控制插槽设置
AzureRM Web App - How to control the slot setting with Powershell
我想使用 powershell 设置我的 Azure Web 应用程序的连接字符串和应用程序设置。我希望这些设置与插槽保持一致,而不是在交换时与应用程序保持一致。
应用程序设置的代码如下所示并且有效:
$PropertiesObject = @{"SMTPUser"="myuser"; "SMTPPassword"="secretpwd";}
$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"
New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force
$stickSlotConfigObject = @{"connectionStringNames"=@(); "appSettingNames" = @("SMTPUserName","SMTPPassword");}
$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/slotConfigNames -ApiVersion 2015-08-01 -Force
这行得通。当我转到 Azure 门户中 Web 应用程序的插槽 blade 时,"Slot Setting" 复选框已按我希望的方式选中。
我正在努力解决如何设置 连接字符串 以同时选中 "slot setting" 框的问题。我尝试了以下,
$PropertiesObject = @{
AzureWebJobsStorage = @{
Type = "Custom";
Value = "somestring"
};
Common = @{
Type = "SQLAzure";
Value = "somedatabasestring"
};
};
$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"
New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force
$stickSlotConfigObject = @{"appSettingNames"=@();"connectionStringNames"=@("AzureWebJobsStorage","Common"); }
$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/appsettings -ApiVersion 2015-08-01 -Force
这没有用。我收到以下错误:
New-AzureRmResource : {"Code":"BadRequest","Message":"The parameter properties has an invalid value.","Target":null,"Details":[{"Message":"The parameter properties has an invalid value."},{"Code":"BadRequest"},{"ErrorEntity":{"Code":"BadRequest","Message":"The parameter properties has an invalid value.","ExtendedCode":"51008","MessageTemplate":"The parameter {0} has an invalid value.","Parameters":["properties"],"InnerErrors":null}}],"Innererror":null}
我尝试了另一个调整(我忘记了),它说 $PropertiesObject object 的格式不正确。
如何在 Powershell 中对其进行编码,以便我可以选中 Web 应用程序连接字符串的插槽设置复选框(或将其配置为 "sticky"?
$resourceName = $webappname + “/slotconfigname”
$stickySlot = Get-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -ApiVersion “2015-08-01”
然后您可以通过以下方式检查现有的:
$stickySlot.Properties.AppSettingNames
在这里你需要不同的方法。如果这些从一开始就为空,则需要使用以下设置创建一个新数组:
$settings = @(“AppSetting1, “AppSetting2”)
$stickySlot.Properties.AppSettingNames = $settings
如果已有其他值,而您想保留它们:
$stickySlot.Properties.AppSettingNames += “AppSetting1”
$stickySlot.Properties.AppSettingNames += “AppSetting2”
然后完成后:
Set-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -Properties $stickySlot.Properties -ApiVersion “2015-08-01"
取自:https://msftplayground.com/2016/02/adding-azure-app-service-application-settings-powershell/
请尝试使用以下代码将连接字符串设置为插槽的粘性设置。它对我来说工作正常。有关使用 PowerShell ARM 方式自动化 Azure WebApps 的更多信息,请参阅 document.
$connectionString = @{}
$webAppName = "Web AppName"
$resourceGroup ="Resource Group Name"
$slotName ="slot Name"
$connectionString.Add("AzureWebJobsStorage", @{ value = "The Actual connecting string here" ; Type = 3 }) #Custom
$connectionString.Add("Common", @{ value = "The Actual connecting string here" ; Type = 2 }) #Azure SQL
Login-AzureRmAccount
# creat slot connection string
New-AzureRmResource -PropertyObject $connectionString `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/sites/slots/config" `
-ResourceName "$webAppName/$slotName/connectionstrings" `
-ApiVersion 2015-08-01 -Force
# set connection string as sticky setting
$stickSlotConfigObject = @{"connectionStringNames" = @("AzureWebJobsStorage","Common")} #connection string Name
Set-AzureRmResource -PropertyObject $stickSlotConfigObject `
-ResourceGroupName $resourceGroup `
-ResourceType Microsoft.Web/sites/config `
-ResourceName $webAppName/slotConfigNames `
-ApiVersion 2015-08-01 -Force
现在有两个新的 cmdlet 可以控制插槽设置:Get-AzureRmWebAppSlotConfigName and Set-AzureRmWebAppSlotConfigName
例如,我想确保我的连接字符串不是插槽配置,所以我执行了:
Set-AzureRmWebAppSlotConfigName -ResourceGroupName MyRg -Name MyWebApp -RemoveAllConnectionStringNames
我想使用 powershell 设置我的 Azure Web 应用程序的连接字符串和应用程序设置。我希望这些设置与插槽保持一致,而不是在交换时与应用程序保持一致。
应用程序设置的代码如下所示并且有效:
$PropertiesObject = @{"SMTPUser"="myuser"; "SMTPPassword"="secretpwd";}
$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"
New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force
$stickSlotConfigObject = @{"connectionStringNames"=@(); "appSettingNames" = @("SMTPUserName","SMTPPassword");}
$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/slotConfigNames -ApiVersion 2015-08-01 -Force
这行得通。当我转到 Azure 门户中 Web 应用程序的插槽 blade 时,"Slot Setting" 复选框已按我希望的方式选中。
我正在努力解决如何设置 连接字符串 以同时选中 "slot setting" 框的问题。我尝试了以下,
$PropertiesObject = @{
AzureWebJobsStorage = @{
Type = "Custom";
Value = "somestring"
};
Common = @{
Type = "SQLAzure";
Value = "somedatabasestring"
};
};
$webAppName = "mywebapp"
$slotName = "demo"
$resourceGroupName = "myResourceGroup"
New-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/slots/config -ResourceName $webAppName/$slotName/appsettings -ApiVersion 2015-08-01 -Force
$stickSlotConfigObject = @{"appSettingNames"=@();"connectionStringNames"=@("AzureWebJobsStorage","Common"); }
$result = Set-AzureRmResource -PropertyObject $stickSlotConfigObject -ResourceGroupName $resourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $webAppName/appsettings -ApiVersion 2015-08-01 -Force
这没有用。我收到以下错误:
New-AzureRmResource : {"Code":"BadRequest","Message":"The parameter properties has an invalid value.","Target":null,"Details":[{"Message":"The parameter properties has an invalid value."},{"Code":"BadRequest"},{"ErrorEntity":{"Code":"BadRequest","Message":"The parameter properties has an invalid value.","ExtendedCode":"51008","MessageTemplate":"The parameter {0} has an invalid value.","Parameters":["properties"],"InnerErrors":null}}],"Innererror":null}
我尝试了另一个调整(我忘记了),它说 $PropertiesObject object 的格式不正确。
如何在 Powershell 中对其进行编码,以便我可以选中 Web 应用程序连接字符串的插槽设置复选框(或将其配置为 "sticky"?
$resourceName = $webappname + “/slotconfigname”
$stickySlot = Get-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -ApiVersion “2015-08-01”
然后您可以通过以下方式检查现有的:
$stickySlot.Properties.AppSettingNames
在这里你需要不同的方法。如果这些从一开始就为空,则需要使用以下设置创建一个新数组:
$settings = @(“AppSetting1, “AppSetting2”)
$stickySlot.Properties.AppSettingNames = $settings
如果已有其他值,而您想保留它们:
$stickySlot.Properties.AppSettingNames += “AppSetting1”
$stickySlot.Properties.AppSettingNames += “AppSetting2”
然后完成后:
Set-AzureRmResource -ResourceName $resourceName -ResourceGroupName -ResourceType “Microsoft.Web/Sites/config” -Properties $stickySlot.Properties -ApiVersion “2015-08-01"
取自:https://msftplayground.com/2016/02/adding-azure-app-service-application-settings-powershell/
请尝试使用以下代码将连接字符串设置为插槽的粘性设置。它对我来说工作正常。有关使用 PowerShell ARM 方式自动化 Azure WebApps 的更多信息,请参阅 document.
$connectionString = @{}
$webAppName = "Web AppName"
$resourceGroup ="Resource Group Name"
$slotName ="slot Name"
$connectionString.Add("AzureWebJobsStorage", @{ value = "The Actual connecting string here" ; Type = 3 }) #Custom
$connectionString.Add("Common", @{ value = "The Actual connecting string here" ; Type = 2 }) #Azure SQL
Login-AzureRmAccount
# creat slot connection string
New-AzureRmResource -PropertyObject $connectionString `
-ResourceGroupName $resourceGroup `
-ResourceType "Microsoft.Web/sites/slots/config" `
-ResourceName "$webAppName/$slotName/connectionstrings" `
-ApiVersion 2015-08-01 -Force
# set connection string as sticky setting
$stickSlotConfigObject = @{"connectionStringNames" = @("AzureWebJobsStorage","Common")} #connection string Name
Set-AzureRmResource -PropertyObject $stickSlotConfigObject `
-ResourceGroupName $resourceGroup `
-ResourceType Microsoft.Web/sites/config `
-ResourceName $webAppName/slotConfigNames `
-ApiVersion 2015-08-01 -Force
现在有两个新的 cmdlet 可以控制插槽设置:Get-AzureRmWebAppSlotConfigName and Set-AzureRmWebAppSlotConfigName
例如,我想确保我的连接字符串不是插槽配置,所以我执行了:
Set-AzureRmWebAppSlotConfigName -ResourceGroupName MyRg -Name MyWebApp -RemoveAllConnectionStringNames