无法通过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"
每次执行反编译都会出现上述错误,如何解决?
解决方案
针对"无法读取文件"的错误,按以下步骤排查修复:
确认文件路径与权限
- 检查
arm-storage-account.json是否确实存在于C:\Users\hasher\source\repos\路径下,排查文件名拼写、大小写、后缀名是否有误 - 右键文件→属性→安全选项卡,确认当前执行命令的用户拥有文件读取权限
- 若文件在OneDrive等同步文件夹中,暂时关闭同步或移至本地普通文件夹重试
- 检查
切换终端工作目录
- 执行反编译命令前,先通过
cd命令切换到文件所在目录:
再执行反编译命令,避免相对路径解析错误cd C:\Users\hasher\source\repos
- 执行反编译命令前,先通过
使用绝对路径执行命令
- 直接使用文件绝对路径替换相对路径,规避路径解析问题:
独立CLI版本:az bicep decompile --file "C:\Users\hasher\source\repos\arm-storage-account.json"bicep decompile "C:\Users\hasher\source\repos\arm-storage-account.json"
- 直接使用文件绝对路径替换相对路径,规避路径解析问题:
更新Bicep版本
- 旧版Bicep CLI可能存在路径解析bug,更新至最新版本:
- 内置CLI:执行
az bicep upgrade - 独立CLI:下载最新版本覆盖安装
- 内置CLI:执行
- 旧版Bicep CLI可能存在路径解析bug,更新至最新版本:
检查文件完整性
- 打开
arm-storage-account.json确认文件未损坏,可通过JSON校验工具验证文件格式是否合法
- 打开
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

