简体   繁体   中英

Azure Powershell - across MULTIPLE subscriptions in an EA

I have "billing reader" access to several hundred subscriptions in an EA.

I'm trying to get a list of virtual machines and their sizes across all subscriptions.

So currently when I run a "Get-AzureRMSubscription" it shows me all the subscriptions (hundreds of them), but i'm not sure how to actually run a script against all the subscriptions?

Would be great to get a "Get-AzureRMVM" across them all

Any suggestions? Thanks in advance!

You can possibly do something like this:

$azureSubs = Get-AzureRMSubscription
$azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRMVM -WarningAction SilentlyContinue}

You are essentially setting an array variable to hold all your Azure Subscription and piping it to the ForEach-Object cmdlet to iterate all of the objects in the array. Then you pipe it to the Get-AzureRMVM cmdlet to list all VMs in each subscription.

This is definitely not optimized for performance and there might be better solutions out there, but at least you can run it and forget it.

The reason for the Out-Null and -WarningAction is to suppress the outputs you do not need.

You didn't ask but for classic resources we have the following script run on a regular basis and its output stored in a SQL Database.

$subscriptions = Get-AzureSubscription
foreach ($sub in $subscriptions)
{
    $sub | Select-AzureSubscription
    Get-AzureService | % {
           Get-AzureDeployment -ServiceName $_.ServiceName 
        } | % { 
          New-Object -TypeName 'PSObject' -Property @{ 'ServiceName' = $_.ServiceName; 'Addresses' = $_.VirtualIPs.Address; } 
        } | sort Addresses | ft
}

% is ForEach-Object , ft is Format-Table although some kind souls may come along and try to edit this and make it harder to reuse. You can add/remove properties in the select statement to tailor your output as needed. Try it in one subscription to refined your needs, then create a script to make it easy to reuse.

We recently released Azure Resource Graph to support these types of searches across multiple subscriptions. See documentation here https://docs.microsoft.com/en-us/azure/governance/resource-graph/overview

I want to do something similar to this. But instead of VM storage information, I want the vulnerability information for each VM all in one output regardless of what subscription it is in.

Instead of:

$azureSubs = Get-AzureRMSubscription
$azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRMVM -WarningAction SilentlyContinue}

I was thinking something along the lines of:

$azureSubs = Get-AzureRMSubscription
$azureSubs | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzSKSubscriptionSecurityStatus -WarningAction SilentlyContinue}

Can anyone please provide some guidance on this? Maybe on how to get the output into a .csv file.

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