如何使用Az PowerShell命令在多个订阅中运行脚本
多Azure租户/订阅下使用Az PowerShell批量运行脚本方案
1. 使用Az PowerShell在多个订阅中运行脚本
对应你之前用Azure CLI实现的逻辑,用Az PowerShell可以这么写:
# 获取当前已登录账号下的所有订阅 $subscriptions = Get-AzSubscription # 循环遍历每个订阅执行目标脚本 foreach ($subscription in $subscriptions) { # 切换到当前订阅上下文 Set-AzContext -Subscription $subscription.Name # 在这里插入你要运行的脚本内容 # <RUN SCRIPTS HERE> }
如果需要只针对某个特定租户下的订阅执行,可以在获取订阅时添加筛选条件:
# 指定租户ID筛选订阅 $targetTenantId = "你的租户ID" $subscriptions = Get-AzSubscription -TenantId $targetTenantId
2. Az PowerShell配置文件路径
Az PowerShell的上下文配置文件路径和Azure CLI类似,不同系统路径如下:
- Windows系统:
C:\Users\<你的用户名>\.Azure\AzureRmContext.json - Linux/macOS系统:
~/.Azure/AzureRmContext.json
这个文件存储了你的登录上下文信息,包括已订阅的租户、订阅详情、登录凭证相关内容,和Azure CLI的azureProfile.json作用类似。
内容的提问来源于stack exchange,提问作者jawad846
相关产品推荐
相关产品推荐

