如何通过CMD的az命令生成Azure Functions的local.settings.json
解决方案:用Azure CLI(CMD)生成符合格式的local.settings.json
第一步:解决az account set无响应问题
如果执行az account set --subscription "<订阅ID/名称>"无响应,尝试以下操作:
- 先执行
az account list --output table,确认目标订阅的ID和名称是否正确,避免输入错误 - 改用订阅ID执行命令(比名称更精准):
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
- 若仍无响应,添加
--debug参数查看详细日志定位问题:
az account set --subscription "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" --debug
第二步:生成符合local.settings.json格式的配置文件
az functionapp config appsettings list的默认输出是数组格式,需要转换成local.settings.json要求的{"IsEncrypted": false, "Values": {...}}结构,推荐两种方法:
方法1:使用jq工具(推荐)
- 先安装jq工具:可通过Chocolatey包管理器执行
choco install jq,或自行下载官方二进制文件 - 执行以下命令直接生成文件:
az functionapp config appsettings list --name MyFunctionApp --resource-group MyResourceGroup --output json | jq '{IsEncrypted: false, Values: (map({(.name): .value}) | add)}' > local.settings.json
方法2:使用PowerShell命令在CMD中执行(无需额外工具)
如果不想安装额外工具,可在CMD中调用PowerShell处理输出:
az functionapp config appsettings list --name MyFunctionApp --resource-group MyResourceGroup --output json | powershell -Command "$settings = ConvertFrom-Json; $output = @{IsEncrypted = $false; Values = @{}}; foreach ($s in $settings) { $output.Values[$s.name] = $s.value }; $output | ConvertTo-Json -Depth 100 | Out-File local.settings.json -Encoding utf8"
验证结果
生成文件后,打开local.settings.json确认结构是否正确,示例格式如下:
{ "IsEncrypted": false, "Values": { "FUNCTIONS_WORKER_RUNTIME": "powershell", "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;...", // 其他配置项 } }
内容的提问来源于stack exchange,提问作者ingo_ww
相关产品推荐
相关产品推荐

