From 32e337bf1301f2ee8362509c5422a410e855a87f Mon Sep 17 00:00:00 2001 From: Cameron Battagler Date: Thu, 14 Jan 2021 15:17:56 -0600 Subject: [PATCH] Added option to register all subscriptions Added Switch Parameter -AllSubscriptions to get all subscriptions the account has access to, iterate over them and register them all --- ...scriptionsToSqlVmAutomaticRegistration.ps1 | 35 ++++++++++++++++--- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/AzureSQLVM/RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 b/AzureSQLVM/RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 index ea4d4b96..82f264f0 100644 --- a/AzureSQLVM/RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 +++ b/AzureSQLVM/RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 @@ -14,13 +14,18 @@ To register list of Subscriptions .\RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 -SubscriptionList SubscriptionId1,SubscriptionId2 + To register all Subscriptions + .\RegisterSubscriptionsToSqlVmAutomaticRegistration.ps1 -AllSubscriptions #> Param ( - [Parameter(Mandatory = $true)] + [Parameter(Mandatory = $false)] [ValidateNotNullOrEmpty()] [Guid[]] - $SubscriptionList + $SubscriptionList, + + [Switch] + $AllSubscriptions ); #Array of objects for storing failure subscriptionIds and failure reasons. @@ -35,12 +40,22 @@ if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -List Write-Host "Please login to your account which have access to the listed subscriptions"; $Output = Connect-AzureRmAccount -ErrorAction Stop; - foreach ($SubscriptionId in $SubscriptionList) { + # Get a list of all subscriptions you have access to + if ($AllSubscriptions) { + $SubscriptionIds = [System.Collections.ArrayList]@(); + foreach ($Subscription in Get-AzSubscription) { + $SubscriptionIds.Add([Guid]$Subscription.Id); + } + } else { + $SubscriptionIds = $SubscriptionList; + } + + foreach ($SubscriptionId in $SubscriptionIds) { Write-host "`n`n--------------------$SubscriptionId----------------------------`n`n"; try { Write-Host "Setting powershell context to subscriptionid: $SubscriptionId"; - $Output = Set-AzureRmContext -SubscriptionId $SubscriptionId -ErrorAction Stop; + $Output = Set-AzureRmContext -SubscriptionId $SubscriptionId -ErrorAction Stop; Write-Host "Registering subscription($SubscriptionId) to Microsoft.SqlVirtualMachine Resource provider"; $Output = Register-AzureRmResourceProvider -ProviderNamespace Microsoft.SqlVirtualMachine -ErrorAction Stop; @@ -69,7 +84,17 @@ else { Write-Host "Please login to your account which have access to the listed subscriptions"; $Output = Connect-AzAccount -ErrorAction Stop; - foreach ($SubscriptionId in $SubscriptionList) { + # Get a list of all subscriptions you have access to + if ($AllSubscriptions) { + $SubscriptionIds = [System.Collections.ArrayList]@(); + foreach ($Subscription in Get-AzSubscription) { + $SubscriptionIds.Add([Guid]$Subscription.Id); + } + } else { + $SubscriptionIds = $SubscriptionList; + } + + foreach ($SubscriptionId in $SubscriptionIds) { Write-host "`n`n--------------------$SubscriptionId----------------------------`n`n" try {