使用Azure CLI创建Data Collection Rule时遇API版本无效错误
Azure数据收集规则创建失败:API版本无效问题
问题场景
我使用以下脚本创建Azure数据收集规则(DCR),但执行最后一条创建命令时遇到API版本无效的错误,且已通过az upgrade将Azure CLI升级至最新版本,问题仍存在。
完整脚本
az login testing_flag="false" admin_username=$(az account show --query "user.name" --output tsv) subscription_id=$(az account show --query id -o tsv) if [[ "$admin_username" == "my_email@gmail.com" ]]; then echo "Using my personal Resource Group for testing purposes." testing_flag="true" else echo "Using the lab Resource Group. Username: $admin_username" testing_flag="false" fi if [ "$testing_flag" = true ]; then echo "Testing mode with my personal Resource Group, not the one from the lab." rg_number="52281982" rg_name="az104-rg5" else rg_number=$(echo "$admin_username" | sed -E 's/^Admin-([0-9]+)@.*$/\1/') # Get the default resource group name rg_name=$(az group list --query "[0].name" -o tsv) fi # Location for the resources based on the resource group location=$(az group show --name "$rg_name" --query location -o tsv) echo "Location that will be used is: $location, and the Resource Group is: $rg_name" echo "Task 1: Create a Log Analytics Workspace" echo "---------------------------------------------------" log_analytics_workspace_name="AZ300-$rg_number-Workspace" az monitor log-analytics workspace create --resource-group $rg_name \ --workspace-name $log_analytics_workspace_name echo "Task 2: Enable the Azure Monitor Agent by using the Azure portal (NOT TESTED YET)" echo "-----------------------------------------------------------------" dcr_name="WindowsVM-Rule1" max_retries=5 sleep_seconds=30 # Get VM names into an array using Zsh array syntax vm_names=("${(@f)$(az vm list -g "$rg_name" --query "[].name" -o tsv)}") # Loop over each VM for vm in "${vm_names[@]}"; do echo "🔍 Checking VM '$vm'..." retries=0 while true; do state=$(az vm get-instance-view -g "$rg_name" -n "$vm" \ --query "instanceView.statuses[?starts_with(code, 'PowerState/')].code" -o tsv) if [[ "$state" == "PowerState/running" ]]; then echo "✅ '$vm' is running." break else echo "⚠️ '$vm' is not running (state: $state). Retry $((retries+1))/$max_retries..." ((retries++)) if (( retries >= max_retries )); then echo "❌ '$vm' did not start after $max_retries attempts." exit 1 fi sleep $sleep_seconds fi done done echo "🎉 All VMs are confirmed to be running." workspace_id=$(az monitor log-analytics workspace show \ --resource-group "$rg_name" \ --workspace-name "$log_analytics_workspace_name" \ --query id -o tsv) echo "Workspace ID: $workspace_id" exit 0 # Tested up to here dcr_json_template=' { "location": "your-region", "properties": { "dataSources": { "performanceCounters": [ { "streams": ["Microsoft-Perf"], "samplingFrequencyInSeconds": 60, "counterSpecifiers": ["\\Processor(_Total)\\% Processor Time", "\\Memory\\Available MBytes"], "name": "perfCounters" } ], "windowsEventLogs": [ { "streams": ["Microsoft-WindowsEvent"], "xPathQueries": ["*"], "name": "eventLogs" } ] }, "destinations": { "logAnalytics": [ { "workspaceResourceId": "WORKSPACE_ID_PLACEHOLDER", "name": "logAnalyticsDest" } ] }, "dataFlows": [ { "streams": ["Microsoft-Perf"], "destinations": ["logAnalyticsDest"] }, { "streams": ["Microsoft-WindowsEvent"], "destinations": ["logAnalyticsDest"] } ] } } ' # Example region="eastus" # Example: workspace_id="/subscriptions/a57df68e-a26a-4473-91d2-97aff58ce4ea/resourceGroups/AZ300-RGlod52281982/providers/Microsoft.OperationalInsights/workspaces/AZ300-52281982-Workspace" if [[ -z "$location" || -z "$workspace_id" ]]; then echo "❌ Error: One or more required variables are not set." echo "location: '$location'" echo "workspace_id: '$workspace_id'" exit 1 else echo "✅ region and workspace_id are set:" echo "location: $location" echo "workspace_id: $workspace_id" fi # Replace placeholders dcr_json_final="${dcr_json_template//your-region/$location}" dcr_json_final="${dcr_json_final//WORKSPACE_ID_PLACEHOLDER/$workspace_id}" # Do not use 'echo' as it will not preserte the \ characters in the JSON. printf "%s\n" "$dcr_json_final" | tee dcr-final.json az monitor data-collection rule create \ --resource-group "$resource_group" \ --data-collection-rule-name "$dcr_name" \ --location "$location" \ --rule-file dcr-final.json
错误信息
执行最后一条创建命令时返回:
user@MacBook-Pro-de-Alvaro lab 3 % az monitor data-collection rule create \ --resource-group "$resource_group" \ --data-collection-rule-name "$dcr_name" \ --location "$location" \ --rule-file dcr-final.json (InvalidApiVersionParameter) API版本'2023-03-11'无效。支持的版本包括'2025-04-01,2025-03-01,2024-10-01-preview,2024-11-01,2024-08-01,2024-07-01,2024-06-01-preview,2024-03-01,2023-07-01,2023-07-01-preview,2023-03-01-preview,2022-12-01,2022-11-01-preview,2022-09-01,2022-06-01,2022-05-01,2022-03-01-preview,2022-01-01,2021-04-01,2021-01-01,2020-10-01,2020-09-01,2020-08-01,2020-07-01,2020-06-01,2020-05-01,2020-01-01,2019-11-01,2019-10-01,2019-09-01,2019-08-01,2019-07-01,2019-06-01,2019-05-10,2019-05-01,2019-03-01,2018-11-01,2018-09-01,2018-08-01,2018-07-01,2018-06-01,2018-05-01,2018-02-01,2018-01-01,2017-12-01,2017-08-01,2017-06-01,2017-05-10,2017-05-01,2017-03-01,2016-09-01,2016-07-01,2016-06-01,2016-02-01,2015-11-01,2015-01-01,2014-04-01-preview,2014-04-01,2014-01-01,2013-03-01,2014-02-26,2014-04'. 错误代码:InvalidApiVersionParameter 错误信息:API版本'2023-03-11'无效。支持的版本为'2025-04-01,2025-03-01,2024-10-01-preview,2024-11-01,2024-08-01,2024-07-01,2024-06-01-preview,2024-03-01,2023-07-01,2023-07-01-preview,2023-03-01-preview,2022-12-01,2022-11-01-preview,2022-09-01,2022-06-01,2022-05-01,2022-03-01-preview,2022-01-01,2021-04-01,2021-01-01,2020-10-01,2020-09-01,2020-08-01,2020-07-01,2020-06-01,2020-05-01,2020-01-01,2019-11-01,2019-10-01,2019-09-01,2019-08-01,2019-07-01,2019-06-01,2019-05-10,2019-05-01,2019-03-01,2018-11-01,2018-09-01,2018-08-01,2018-07-01,2018-06-01,2018-05-01,2018-02-01,2018-01-01,2017-12-01,2017-08-01,2017-06-01,2017-05-10,2017-05-01,2017-03-01,2016-09-01,2016-07-01,2016-06-01,2016-02-01,2015-11-01,2015-01-01,2014-04-01-preview,2014-04-01,2014-01-01,2013-03-01,2014-02-26,2014-04'.
已尝试的操作
- 通过
az upgrade将Azure CLI升级至最新版本,但问题未解决。
解决方案建议
- 修复变量错误:脚本最后创建DCR的命令中使用了未定义的
$resource_group变量,应替换为前面定义的$rg_name,修正后的命令:az monitor data-collection rule create \ --resource-group "$rg_name" \ --data-collection-rule-name "$dcr_name" \ --location "$location" \ --rule-file dcr-final.json - 显式指定API版本:在命令中添加支持的API版本参数,例如最新稳定版:
az monitor data-collection rule create \ --resource-group "$rg_name" \ --data-collection-rule-name "$dcr_name" \ --location "$location" \ --rule-file dcr-final.json \ --api-version 2025-04-01 - 清除CLI缓存:执行
az cache purge清除可能存在的版本缓存,再重新运行命令。 - 验证JSON模板:确保DCR的JSON模板符合指定API版本的格式要求,避免因字段不兼容引发异常。
内容的提问来源于stack exchange,提问作者Álvaro
相关产品推荐
相关产品推荐

