0

I am trying to enable diagnostic settings for all app services in a particular subscription but i am getting this error ERROR: Expecting value: line 1 column 2 (char 1). I am not sure this error is from a JSON response or something is wrong in my shell script.

$webapps = az webapp list --query '[].{Name : name, ResourceGroup:resourceGroup, id: id}' | ConvertFrom-Json

$funcapps = az functionapp list --query '[].{Name : name, ResourceGroup:resourceGroup, id: id}' | ConvertFrom-Json

$logicapps = az logic workflow list --query '[].{Name:name, ResourceGroup:resourceGroup, id: id}' | ConvertFrom-Json

$apps = @($webapps,$funcapps, $logicapps)

$applist = $apps  

foreach ($alist in $applist)
{       
    foreach($i in $alist)
    {
        
        $logsettingsList = az monitor diagnostic-settings list --resource-group $i.resourceGroup --resource $i.id | ConvertFrom-Json

       if($logsettingsList.value.Length -eq 0){
           
            Write-Output "No diagnostic setting found for" $i.Name  


            Write-Output "Creating diagnostic profile for" $i.Name  

            $logs = '[
                {
                  \"category\": "\AppServiceAntivirusScanAuditLogs\",
                  \"enabled\": true,
                  "retentionPolicy": {
                    \"enabled\": false,
                    \"days\": 0
                  }
                }
              ]'


              $metrics = '[{\"category\": \"AppServiceAntivirusScanAuditLogs\",
                  \"enabled\": true,
                  \"retentionPolicy\": {
                    \"enabled\": false,
                    \"days\": 0
                  }
                }
              ]'



             $test = az monitor diagnostic-settings create --resource $i.id --name "$($i.Name)profile"  --storage-account {storageAccount} --logs $logs --metrics $metrics
                $test
                


            foreach ($ishow in $logsettingsList) {

                $logSettings = az monitor diagnostic-settings show --resource-group $i.resourceGroup --resource $i.id --name $i.name | ConvertFrom-Json
    
                $logSettings
                
            }
        
        }

        else {
        
            Write-Output "Diagnostic Profile already created for" $i.Name  


        }     
    }

}

I initially thought there is something wrong with my JSON load which I am sending. Then I tried to change to https://github.com/Azure/azure-cli/issues/5637 but it's still giving me the same error. The two JSON bodies i have tried

 $logs = '[
                {
                  \"category\": "\AppServiceAntivirusScanAuditLogs\",
                  \"enabled\": true,
                  "retentionPolicy": {
                    \"enabled\": false,
                    \"days\": 0
                  }
                }
              ]'


              $metrics = '[{\"category\": \"AppServiceAntivirusScanAuditLogs\",
                  \"enabled\": true,
                  \"retentionPolicy\": {
                    \"enabled\": false,
                    \"days\": 0
                  }
                }
              ]'

AND

--logs '[
     {
       "category": "WorkflowRuntime",
       "enabled": true,
       "retentionPolicy": {
         "enabled": false,
         "days": 0
       }
     }
   ]'
   --metrics '[
     {
       "category": "WorkflowRuntime",
       "enabled": true,
       "retentionPolicy": {
         "enabled": false,
         "days": 0
       }
     }
   ]'

Above referred from here https://learn.microsoft.com/en-us/cli/azure/monitor/diagnostic-settings?view=azure-cli-latest#code-try-1

Bheeshma
  • 143
  • 3
  • 12

2 Answers2

1

According to my test, we need to remove the space. The son should be like $logs='[{\"category\":\"AppServiceAntivirusScanAuditLogs\",\"retentionPolicy\":{\"days\":0,\"enabled\":false},\"enabled\":true}]'.

For example( I test in powershell)

 $logs='[{\"category\":\"AppServiceAntivirusScanAuditLogs\",\"retentionPolicy\":{\"days\":0,\"enabled\":false},\"enabled\":true}]'
              
$metrics= '[{\"category\":\"AllMetrics\",\"retentionPolicy\":{\"days\":0,\"enabled\":false},\"enabled\":true}]'
            
$test = az monitor diagnostic-settings create --resource $i.id --name "test"  --storage-account <the resource id of storage account> --logs $logs --metrics $metrics --debug
$test

enter image description here

Besides, please note that the resource and the storage account used to stored logs and metrics should be in the same region. Meanwhile, the different resource type has different categories. You can use the command az monitor diagnostic-settings categories list --resource to check it. For more details, please refer to here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
0

I tried this and it worked. You can probably try the same JSON response however by replacing (" ' " to ' \ " '). So the JSON response will look like

$metricsSettingJSON = "[{'category': 'AllMetrics','enabled': true,'retentionPolicy': {'days': 0,'enabled': false},'timeGrain': null}]".Replace("'",'\"')
Bheeshma
  • 143
  • 3
  • 12