Azure CLI中az account set报错:argument --subscription/-s/--name/-n: expected one argument
问题:Azure CLI
az account set 命令提示缺少订阅参数 我在Variables.ps1文件中初始化了变量:
$RG="bicepRG" $Subscription="subscription_id" $location="eastus"
同路径下的另一个bash文件包含以下Azure CLI命令:
az login --only-show-errors -o table --query Dummy az account set -s $Subscription
az login能正常弹出登录提示并执行成功,但az account set -s $Subscription报错:
argument --subscription/-s/--name/-n: expected one argument
运行环境为Visual Studio Code,当前路径是C:\Users\hasher\source\repos\bicep>
问题原因
PowerShell脚本里的变量和bash脚本属于不同的shell环境,两者的变量不共享。bash脚本中的$Subscription没有被赋值,导致az account set命令缺少必要的订阅参数,因此报错。
解决办法
方法1:直接在bash脚本中指定订阅信息
要么直接把订阅ID写死在命令里,要么在bash脚本内部定义变量:
# 直接使用订阅ID az account set -s "subscription_id" # 或者在bash里先定义变量再使用 Subscription="subscription_id" az account set -s $Subscription
方法2:从PowerShell传递变量到bash脚本
先在PowerShell中加载变量并导出为环境变量,再调用bash脚本:
- 在PowerShell终端执行:
# 加载变量文件 . .\Variables.ps1 # 将变量导出为环境变量 $env:Subscription = $Subscription # 调用bash脚本 bash .\你的bash脚本文件名.sh
- 修改bash脚本,读取环境变量:
az login --only-show-errors -o table --query Dummy az account set -s $Subscription
方法3:直接在PowerShell中执行所有命令
Azure CLI支持在PowerShell环境中运行,无需切换到bash:
# 加载变量文件 . .\Variables.ps1 # 执行登录命令 az login --only-show-errors -o table --query Dummy # 设置订阅 az account set -s $Subscription
内容的提问来源于stack exchange,提问作者anon
相关产品推荐
相关产品推荐

