简体   繁体   English

如何使用 Az PowerShell 命令在多个订阅上运行 Powershell 脚本

[英]How to run Powershell script on multiple Subscriptions Using Az PowerShell Command

I have multiple azure tenants, each having multiple subscriptions, and I have to run a single PowerShell script for all my subscriptions.我有多个 azure 租户,每个租户都有多个订阅,我必须为我的所有订阅运行一个 PowerShell 脚本。

This can be achieved using Azure CLI , and it works perfectly.这可以使用Azure CLI来实现,并且效果很好。

I use Azure CLI as below;我使用Azure CLI如下;

$az_account = (az account list --query "[].[name]" -o tsv)
foreach ($account in $az_account) {
    az account set --name $account
    #<RUN SCRIPTS HERE>#
}

But in some situations, I have to use the Az PowerShell command instead of Azure CLI .但在某些情况下,我必须使用Az PowerShell命令而不是Azure CLI

So could anyone help me所以任何人都可以帮助我

  1. How to run Az PowerShell commands for multiple subscriptions如何为多个订阅运行Az PowerShell命令
  2. Or the Az PowerShell profile file path ( same as Azure CLI which is C:\Users\%USER\.Azure\azureProfile.json ). Or the Az PowerShell profile file path ( same as Azure CLI which is C:\Users\%USER\.Azure\azureProfile.json ).
  1. How to run Az PowerShell commands for multiple subscriptions如何为多个订阅运行Az PowerShell命令

You can use the below PowerShell Scripts to run the multiple subscription PowerShell commands.您可以使用以下 PowerShell 脚本来运行多个订阅 PowerShell 命令。

# Get the Subscription Details using Get-AzSubscription Command
Get-AzSubscription | ForEach-Object {
    # Set the context Details using Set-AzContext Command which is equalent to the az account set CLI Command
    $_ | Set-AzContext
    $subscriptionName = $_.Name
      #<RUN YOUR SCRIPTS HERE>#

}

在此处输入图像描述

  1. Or the Az PowerShell profile file path ( same as Azure CLI which is C:\Users\%USER\.Azure\azureProfile.json ).Az PowerShell配置文件路径(与Azure CLI相同,即C:\Users\%USER\.Azure\azureProfile.json )。

Refer here for profile file path location请参阅此处了解配置文件路径位置

Resolution解析度

After a couple of tries, finally found some methods to solve my issue.经过几次尝试,终于找到了一些方法来解决我的问题。 posting the same here might be helpful for someone.在此处发布相同内容可能对某人有所帮助。

You can connect all the subscriptions using the below commands您可以使用以下命令连接所有订阅

Connect-AzAccount -Tenant "xxxx-xxxxx-xxxx-xxx" -Subscription "xxxx-xxxxx-xxxx-xxx"

To List all the connected subscriptions列出所有连接的订阅

Get-AzContext -ListAvailable | Select-Object Name, Subscription, Tenant

在此处输入图像描述

If you want to rename to a friendly Name,如果要重命名为友好名称,

Rename-AzContext -SourceName "Azure subscription 1 (xxxxx-xxxx-xxxx-xxxx-xxxx) - xxxxx-xxxx-xxx-xxxx-xxxx-xxx - jawad@xyz.com" -TargetName "MySubscription"

To save these profiles to file,要将这些配置文件保存到文件中,

Save-AzContext -Path "C:\Users\jawad\Downloads\AzpwshProfile.json"

You can import any time these profiles using the below command, (test first clear the profile Clear-AzContext -Force )您可以随时使用以下命令导入这些配置文件,(首先测试清除配置文件Clear-AzContext -Force

Import-AzContext -Path "C:\Users\jawad\Downloads\AzpwshProfile.json"

Now you easily use for loop to set the subscriptions, for example现在您可以轻松地使用for循环来设置订阅,例如

$acc = (Get-AzContext -ListAvailable | Select-Object Name)
foreach ($account in $acc) {
>> Select-AzContext $account.Name
>> Get-AzVM | Export-Csv -Path "inventory.csv"
>> }

Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM