Mohamed Wali
Mohamed Wali

Reputation: 185

Retrieve something from different Azure subscriptions at the same time

I want to run a PowerShell Cmdlet against multiple Azure subscriptions, that's why I've thought about running it within a foreach loop but it did not work:

$Subscriptions = (Get-AzureRmSubscription).SubscriptionId
foreach ($sub in $Subscriptions)
{
    Select-AzureRmSubscription -Subscription $sub
    Do the task Cmdlet
}

Actually what it does is to run the task against the last subscription it was able to select. Any better ways to workaround this?

Unfortunately the result cannot be exported to a csv file or a variable because it is displayed under the subscription info, as shown in the following figure. enter image description here

Upvotes: 1

Views: 85

Answers (1)

Martin Brandl
Martin Brandl

Reputation: 59001

Try using the Set-AzureRmContext cmdlet to set the subscription:

Get-AzureRmSubscription | ForEach-Object {
  $_ | Set-AzureRmContext
  # do your task
 }

Upvotes: 1

Related Questions