0

I have this Powershell line which should list all virtual machines from selected subscription and I have total of 3 subscriptions.

$azureSubscriptionID = "xxxxx-xxxxx-xxxxx-xxxx"
foreach ($subs in $azureSubscriptionID)
    {
        Write-Output "Collecting VM facts from subscription $subs"

         $vms += Get-AzureRMSubscription | ForEach-Object {Select-AzureRMSubscription $_ | Out-Null; Get-AzureRmVM -WarningAction SilentlyContinue} 
    }

Issue is that when running the script and using $vms is that it will list all available vms subscriptions three times in a row like this:

VM A     VM B     VM C
VM A     VM B     VM C
VM A     VM B     VM C

What am I doing wrong and how to fix that? or are there alternative ways to get all vms from X subscription in few lines? Using this in Azure runbook.

3
  • your code shows just ONE item in the foreach ($subs in $azureSubscriptionID) loop. so i would start by checking the values at each step of your code. Commented Mar 26, 2020 at 13:57
  • Ye, I know but in the future there are maybe more than one subscription as parameter and that is why i need loop them. Commented Mar 30, 2020 at 6:35
  • thank you for the "why". [grin] i see that AdminOfThings has shown a solution - good to know you have a solution. Commented Mar 30, 2020 at 12:02

1 Answer 1

3

If you just want to cycle through all of your subscriptions and list all of the VMs, you can do the following:

Get-AzureRMSubscription | ForEach-Object {
    $sub = Select-AzureRMSubscription $_
    Write-Output "Collecting VM facts from subscription $($sub.Subscription.Id)"
    Get-AzureRmVM
}

The issue with your attempt is you are getting all subscriptions (Get-AzureRmSubscription) during each loop iteration regardless of the value(s) contained in $azureSubscriptionID. To fix your code, you would need to run Get-AzureRMSubscription -SubscriptionId $subs or Select-AzureRMSubscription -SubscriptionId $subs.


If you want to do further processing with the data you have gathered, I would consider some alternative approach for when you explicitly target known subscriptions.

$azureSubscriptionIDs = "xxxxx-xxxxx-xxxxx-xxxx","yyyyy-yyyyy-yyyyy-yyyy","zzzzz-zzzzz-zzzzz-zzzz"
# $vms is an array of custom objects
# each custom object contains a subscription ID and the associated VMs Names
$vms = foreach ($sub in $azureSubscriptionIDs) {
    $null = Select-AzureRMSubscription -SubscriptionId $sub
    $subvms = Get-AzureRmVM | Select -Expand Name
    $sub | Select @{n='Subscription';e={$_}},@{n='VMs';e={$subvms}}
}
# You can access the subscription ID now with the Subscription property
# You can access the VMs Names with the VMs property
# List all vms under subscription 'xxxxx-xxxxx-xxxxx-xxxx'
$vms | Where Subscription -eq 'xxxxx-xxxxx-xxxxx-xxxx' | Select -Expand VMs
# List all vms for each subscription with a custom console message
foreach ($sub in $vms) {
    Write-Output "Here are all the VMs for subscription $($sub.Subscription)"
    $sub.VMs
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.