简体   繁体   中英

How to export LocalNetworkGateway info from multiple ResourceGroups but also across multiple subscriptions

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
}

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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