应用服务使用 az cli 创建诊断设置
app service create diagnostic settings using az cli
我正在尝试为特定订阅中的所有应用程序服务启用诊断设置,但我收到此错误 ERROR: Expecting value: line 1 column 2 (char 1)
。我不确定这个错误是来自 JSON 响应还是我的 shell 脚本有问题。
$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
}
}
}
我最初认为我发送的 JSON 负载有问题。
然后我尝试更改为 https://github.com/Azure/azure-cli/issues/5637 但它仍然给我同样的错误。
我试过的两个JSON身体
$logs = '[
{
\"category\": "\AppServiceAntivirusScanAuditLogs\",
\"enabled\": true,
"retentionPolicy": {
\"enabled\": false,
\"days\": 0
}
}
]'
$metrics = '[{\"category\": \"AppServiceAntivirusScanAuditLogs\",
\"enabled\": true,
\"retentionPolicy\": {
\"enabled\": false,
\"days\": 0
}
}
]'
和
--logs '[
{
"category": "WorkflowRuntime",
"enabled": true,
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
]'
--metrics '[
{
"category": "WorkflowRuntime",
"enabled": true,
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
]'
根据我的测试,我们需要删除space。儿子应该是这样的$logs='[{\"category\":\"AppServiceAntivirusScanAuditLogs\",\"retentionPolicy\":{\"days\":0,\"enabled\":false},\"enabled\":true}]'
.
例如(我在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
此外,请注意用于存储日志和指标的资源和存储帐户应位于同一区域。同时,不同的资源类型有不同的分类。可以使用命令az monitor diagnostic-settings categories list --resource
查看。详情请参考here
我试过了,很管用。您可以尝试相同的 JSON 响应,但是将 (" ' " 替换为 ' \ " ')。因此 JSON 响应看起来像
$metricsSettingJSON = "[{'category': 'AllMetrics','enabled': true,'retentionPolicy': {'days': 0,'enabled': false},'timeGrain': null}]".Replace("'",'\"')
我正在尝试为特定订阅中的所有应用程序服务启用诊断设置,但我收到此错误 ERROR: Expecting value: line 1 column 2 (char 1)
。我不确定这个错误是来自 JSON 响应还是我的 shell 脚本有问题。
$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
}
}
}
我最初认为我发送的 JSON 负载有问题。 然后我尝试更改为 https://github.com/Azure/azure-cli/issues/5637 但它仍然给我同样的错误。 我试过的两个JSON身体
$logs = '[
{
\"category\": "\AppServiceAntivirusScanAuditLogs\",
\"enabled\": true,
"retentionPolicy": {
\"enabled\": false,
\"days\": 0
}
}
]'
$metrics = '[{\"category\": \"AppServiceAntivirusScanAuditLogs\",
\"enabled\": true,
\"retentionPolicy\": {
\"enabled\": false,
\"days\": 0
}
}
]'
和
--logs '[
{
"category": "WorkflowRuntime",
"enabled": true,
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
]'
--metrics '[
{
"category": "WorkflowRuntime",
"enabled": true,
"retentionPolicy": {
"enabled": false,
"days": 0
}
}
]'
根据我的测试,我们需要删除space。儿子应该是这样的$logs='[{\"category\":\"AppServiceAntivirusScanAuditLogs\",\"retentionPolicy\":{\"days\":0,\"enabled\":false},\"enabled\":true}]'
.
例如(我在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
此外,请注意用于存储日志和指标的资源和存储帐户应位于同一区域。同时,不同的资源类型有不同的分类。可以使用命令az monitor diagnostic-settings categories list --resource
查看。详情请参考here
我试过了,很管用。您可以尝试相同的 JSON 响应,但是将 (" ' " 替换为 ' \ " ')。因此 JSON 响应看起来像
$metricsSettingJSON = "[{'category': 'AllMetrics','enabled': true,'retentionPolicy': {'days': 0,'enabled': false},'timeGrain': null}]".Replace("'",'\"')