0
votes

I'm new to powershell and azure and need to export all the LocalNetworkGateway information from multiple Resource Groups but also from across multiple Subscriptions.

A contributor has kindly provided me with a script that can output the data from multiple Resource Groups within a single subscription but I need to find a way of doing this across all subscriptions without having to set the subscription context manually for each one and then running the script for each subscription.

I have used the

$azureSubs = Get-AzSubscription 

as a way of extracting information across multiple subscriptions that does not require Resource Group Names but I am now stuck. The code below has successfully provided info across RG's but within a single subscription.

$resourceGroups = Get-AzResourceGroup
$resourceGroups.foreach{ 
 Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName | 
     Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
}
1

1 Answers

0
votes

you need to create another loop around subscription (similar to resource groups), the only trick is that you need to switch active subscription before doing requests to the subscription:

$azureSubs = Get-AzSubscription 
$azureSubs.foreach{
    Select-AzSubscription $_ # << change active subscription
    $resourceGroups = Get-AzResourceGroup # << same resource group loop starts here
    $resourceGroups.foreach{ 
        Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName | 
          Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
    }
}