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
}
foreach ($subs in $azureSubscriptionID)loop. so i would start by checking the values at each step of your code.AdminOfThingshas shown a solution - good to know you have a solution.