Expecting property name enclosed in double quotes when passing JSON string to Azure CLI (from PowerShell)
Asked Answered
T

1

8

To create diagnostic settings on an Azure Firewall I want to pass in logs and metrics settings.

I define these in 2 variables and then pass 'em into Azure CLI:

$logsSetting = "[{'category': 'AzureFirewallApplicationRule','enabled': true,'retentionPolicy': {'days': 0,'enabled': false}},{'category': 'AzureFirewallNetworkRule','enabled': true,'retentionPolicy': {'days': 0,'enabled': false}}]"
$metricsSetting = "[{'category': 'AllMetrics','enabled': true,'retentionPolicy': {'days': 0,'enabled': false},'timeGrain': null}]"

az monitor diagnostic-settings create --name $FW_NAME `
   --resource $FW_NAME -g $VNET_GROUP --resource-type Microsoft.Network/azureFirewalls `
   --resource-group $VNET_GROUP `
   --workspace $FW_NAME `
   --logs $logsSetting `
   --metrics $metricsSetting

Executing this I get Expecting property name enclosed in double quotes: line 1 column 3 (char 2).

I tried without success

  • changed from multiline string literal to single line (which caused another error)
  • replaced ' with "
Terra answered 23/3, 2020 at 18:27 Comment(0)
T
17

Adding --debug parameter to Azure CLI revealed, that single quotes are eliminated in a argument transformation process which seems to cause the error:

Alias Manager: Transformed args to ['monitor', ... '--logs', '[{category: AzureFirewallApplicationRule,enabled: true,retentionPolicy: {days: 0,enabled: false}},{category: AzureFirewallNetworkRule,enabled: true,retentionPolicy: {days: 0,enabled: false}}]', '--metrics', '[{category: AllMetrics,enabled: true,retentionPolicy: {days: 0,enabled: false},timeGrain: null}]', '--debug']

Solution: escaping the quotes with \" made it work:

$logsSetting = "[{'category': 'AzureFirewallApplicationRule','enabled': true,'retentionPolicy': {'days': 0,'enabled': false}},{'category': 'AzureFirewallNetworkRule','enabled': true,'retentionPolicy': {'days': 0,'enabled': false}}]".Replace("'",'\"')
$metricsSetting = "[{'category': 'AllMetrics','enabled': true,'retentionPolicy': {'days': 0,'enabled': false},'timeGrain': null}]".Replace("'",'\"')
Terra answered 23/3, 2020 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.