【问题标题】:Using Set-AzureRmResource - JSON Powershell Syntax?使用 Set-AzureRmResource - JSON Powershell 语法?
【发布时间】:2016-05-12 23:40:59
【问题描述】:

我正在尝试使用“Set-AzureRmResource”在 Azure 中配置 WebApp 配置。

我这里有一段代码可以设置“alwaysOn = True”:

$PropertiesObject = @{
"alwaysOn" = $true;
}
Set-AzureRmResource -PropertyObject $PropertiesObject -ResourceGroupName $ResourceGroupName -ResourceType Microsoft.Web/sites/config -ResourceName $SiteName/web -ApiVersion 2015-08-01 -Force
### in this case "alwaysOn' sits within "properties": {. 

我现在想设置另一个属性,但是这个是嵌套的。例如:

"azureBlobStorage": {
"sasUrl": null,
"retentionInDays": null,
"enabled": false
}
### in this case I want to set SasUrl which sits within "properties"{ AzureBlobStorage {

我不知道用来设置像这样嵌套的属性的语法。

对不起,如果我的术语不正确,powershell 和 Json 对我来说很新,我边走边学。

Azure 文档简单说明:

# SET web
$PropertiesObject = @{
#Property = value;
}
#
"properties": {
"httpLogs": {
  "fileSystem": {
    "retentionInMb": 35,
    "retentionInDays": null,
    "enabled": false
  },
  "azureBlobStorage": {
    "sasUrl": null,
    "retentionInDays": null,
    "enabled": false
  }
},

【问题讨论】:

  • 另请注意azureBlobStorage$SiteName/logs 下,而不是$SiteName/web

标签: azure azure-web-app-service


【解决方案1】:

以防万一其他人看到这一点而无法弄清楚;我以一种更简单的方法使用 Hashtables,而不是创建内部对象。以下是为 ApplicationLogs 关闭 FileSystem 日志记录的示例:

$propertiesObject = [ordered] @{
                            'applicationLogs' = @{
                                'fileSystem' = @{
                                    'level' = 'Off'
                                 }   
                            }
                        }
Set-AzureRmResource -PropertyObject $propertiesObject -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName "$($webAppName)/logs" -ApiVersion 2015-08-01 -Force

你可以对设置HttpLogs sasUrl属性的原始OP请求做同样的事情:

$propertiesObject = [ordered] @{
                            'httpLogs' = @{
                                'azureBlobStorage' = @{
                                    "sasUrl": "SASURL"
                                 }   
                            }
                        }
Set-AzureRmResource -PropertyObject $propertiesObject -ResourceGroupName $resourceGroup -ResourceType Microsoft.Web/sites/config -ResourceName "$($webAppName)/logs" -ApiVersion 2015-08-01 -Force

【讨论】:

  • - 感谢您发布您的更新,这是更容易和更简单的方法。我现在可以看到它全部采用 JSON 格式。
【解决方案2】:

我也为此苦苦挣扎了一段时间!

我有一个答案here 涵盖了它。您基本上必须创建内部对象,然后将该对象添加到外部属性。

所以如果你想要类似的东西

{
"Property":  [
        {
            "prop2":  1,
            "prop1":  "string"
        }
    ]
}

你会使用

$InnerPropertiesObject = @{
        prop1 = "string"
        prop2 = 1
      }
$PropertiesObject = @{
    "Property" = [Object[]]$InnerPropertiesObject 
}

然后使用

New-AzureRmResource -Name $ResourceName `
                -Location $ResourceLocation `
                -PropertyObject $PropertiesObject `
                -ResourceGroupName examplecomRG `
                -ResourceType $resourceType `
                -ApiVersion 2015-08-01 -Force

【讨论】:

  • 嗨,迈克尔,感谢您的回复。这有点让我头疼,因为我还不了解哈希表,但它给了我一些阅读和学习的东西。通读 JSON 模板,我相信我需要更深入地设置正确的值,因此我可以按照您的示例进行进一步嵌套。例如:“属性”:{“httpLogs”:{“azureBlobStorage”
  • @James 它正在弄清楚如何做到这一点,这让我理解了哈希表! (它们真是太疯狂了!)-至少好一点-但是是的,您应该能够进一步嵌套它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-10
  • 1970-01-01
  • 2022-01-11
  • 1970-01-01
相关资源
最近更新 更多