You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法通过PowerShell或CLI命令将ARM模板反编译为Bicep文件

问题描述

文件夹/文件结构

文件结构

arm-storage-account.json

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
      {
        "type": "Microsoft.Storage/storageAccounts",
        "apiVersion": "2022-09-01",
        "name": "testsa08",
        "location": "eastus",
        "sku": {
          "name": "Standard_LRS"
        },
        "kind": "StorageV2"
      }
    ]
  }

Variables.ps1

$RG="bicepRG"
$subscription="<Subsricption_Id>"
$location="eastus"

反编译尝试命令

02-02 Authoring, Deploying.ps1中的命令

内置命令

az bicep decompile --file .\arm-storage-account.json

独立CLI命令

bicep decompile .\arm-storage-account.json

执行结果与错误

执行时先返回警告:

WARNING: Decompilation is a best-effort process, as there is no guaranteed mapping from ARM JSON to Bicep.
You may need to fix warnings and errors in the generated bicep file(s), or decompilation may fail entirely if an accurate conversion is not possible.
If you would like to report any issues or inaccurate conversions, please see https://github.com/Azure/bicep/issues.

随后出现致命错误:

C:\Users\hasher\source\repos\arm-storage-account.json: Decompilation failed with fatal error "Failed to read file:///C:/Users/hasher/source/repos/arm-storage-account.json"

每次执行反编译都会出现上述错误,如何解决?


解决方案

针对"无法读取文件"的错误,按以下步骤排查修复:

  1. 确认文件路径与权限

    • 检查arm-storage-account.json是否确实存在于C:\Users\hasher\source\repos\路径下,排查文件名拼写、大小写、后缀名是否有误
    • 右键文件→属性→安全选项卡,确认当前执行命令的用户拥有文件读取权限
    • 若文件在OneDrive等同步文件夹中,暂时关闭同步或移至本地普通文件夹重试
  2. 切换终端工作目录

    • 执行反编译命令前,先通过cd命令切换到文件所在目录:
      cd C:\Users\hasher\source\repos
      
      再执行反编译命令,避免相对路径解析错误
  3. 使用绝对路径执行命令

    • 直接使用文件绝对路径替换相对路径,规避路径解析问题:
      az bicep decompile --file "C:\Users\hasher\source\repos\arm-storage-account.json"
      
      独立CLI版本:
      bicep decompile "C:\Users\hasher\source\repos\arm-storage-account.json"
      
  4. 更新Bicep版本

    • 旧版Bicep CLI可能存在路径解析bug,更新至最新版本:
      • 内置CLI:执行az bicep upgrade
      • 独立CLI:下载最新版本覆盖安装
  5. 检查文件完整性

    • 打开arm-storage-account.json确认文件未损坏,可通过JSON校验工具验证文件格式是否合法

内容的提问来源于stack exchange,提问作者anon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 05:02:03