Forum Discussion
Assistance with looping through multiple subscriptions
Hello Hairy_Zeus,
One of the option would be to leave current function intact and add another function (wrapper) around it. Wrapper will loop through all Azure Subscriptions and will execute Get-AzVMBackupInformation for each subscription.
Something similar to this:
#List to store all results
$Result=New-Object System.Collections.Generic.List[PSObject]
#All Azure Subscriptions
$Subscriptions = Get-AzSubscription
#Looping through each and every subscription
foreach ($sub in $Subscriptions) {
#Setting context so the script will be executed within the subscription's scope
Get-AzSubscription -SubscriptionName $sub.Name | Set-AzContext
#Getting VM backup info for all VMs in subscription
$Obj=.\Get-AzVMBackupInformation.ps1 -AllVirtualMachines
#Enriching the output with SubscriptionId for future reference and convenience
$Obj | Add-Member -NotePropertyName SubscriptionId -NotePropertyValue $Sub.Id
#Adding results to resulting List
$Result.Add($Obj)
}
$Result
Hope that helps.
I modified the script as mentioned by you. However, I'm getting the below error message: ( if you can assist fixing it will be highly appreciated )
Line |
160 | $Obj=.\Get-AzVMBackupInformation.ps1 -AllVirtualMachines
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| The term '.\Get-AzVMBackupInformation.ps1' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that
| the path is correct and try again.
Export-Csv: Cannot bind argument to parameter 'InputObject' because it is null.
== My Objective is to execute the above code for multiple subscription ==
I have modified the above script to capture VM TAG details and VM subscription details.
I'm unable to get an accurate subscription details.
I executed the below code ( --> It is getting looping through multiple subscription. However I'm not getting subscription name in the output csv file)
====Code====
- AndySvintsFeb 27, 2023Steel Contributor
Hello Subhankar131,
Get-AzVMBackupInformation.ps1 is the name of the function that you posted.
So the algorithm is the following. Save your posted function to Get-AzVMBackupInformation.ps1 file and then code that I've provided is additional function which will executed the existing one.
Hope that helps.
- Subhankar131Feb 28, 2023Copper Contributor
Hello AndySvints,
Thank you for your clarification.
The above code mentioned by you it works. I need to Export it to a csv file along with subscription name mentioned with VM details. Can it be addressed by modifying your code?
- Subhankar131Feb 28, 2023Copper Contributor