Azure Subscription Management with Resource Providers

You might have heard about the Azure Resource providers while working on Azure. It comes handy if you are responsible for the management of the subscriptions. Basically by registering and unregistering Resource Providers you allow what type of resources are going to be provisioned by the users.

By default if you have the subscription created you have all the Resource Providers available to work with, however just some are registered by default. In this blog post I am going to show you how you can automate the registration of the resource providers from the csv file. But first let’s check how we can access it from the portal.

If you login to your Azure portal and then go to Subscriptions, choose one of the subscription and then from the Settings menu click on Resource providers, you will get the list with all available resource providers. You can simply register and un-register by clicking on one or another.

However it is not very efficient. Especially if you have to click around on another 10 subscriptions to make sure that every resource provider is (un)registered accordingly. So let’s check how we can do it automatically.

First, let’s create the csv file and lets call the Header ResourceProviders Let’s say that this is the list with only resource providers need to be registered.

ResourceProviders
Microsoft.BingMaps
Microsoft.BizTalkServices
Microsoft.BotService
Microsoft.CertificateRegistration
Microsoft.ClassicCompute
Microsoft.ClassicNetwork
Microsoft.ClassicStorage
Microsoft.ClassicInfrastructureMigrate
Microsoft.AAD
Microsoft.ADHybridHealthService
Microsoft.Advisor
Microsoft.AlertsManagement
Microsoft.AnalysisServices
Microsoft.ApiManagement
Microsoft.Authorization
Microsoft.Automation
Microsoft.AzureActiveDirectory
Microsoft.Billing
Microsoft.Compute

And with the following script performing the following:

  1. Logging into my Azure Subscription
  2. Reading the csv list with resource providers need to be registered
  3. Unregistering providers wchich are not in the list but was registered previously/by default
  4. Register all the rest
Add-AzureRmAccount

Get-AzureRmSubscription
Select-AzureRmSubscription -SubscriptionId  

$csvLoc = ".\ResourceProviders.csv" 
$ImportedResourceProvidersList = Import-Csv $csvLoc 

$PrimaryResourceProviders = (Get-AzureRmResourceProvider -ListAvailable | Where-Object -Property RegistrationState -eq "Registered").ProviderNamespace
$UnregResourceProvidersArray = (Compare-Object $PrimaryResourceProviders -DifferenceObject $ImportedResourceProvidersList.ResourceProviders | Where-Object {$_.SideIndicator -eq '<='}).inputobject $RegResourceProvidersArray = (Compare-Object $PrimaryResourceProviders -DifferenceObject $ImportedResourceProvidersList.ResourceProviders | Where-Object {$_.SideIndicator -eq '=>'}).inputobject

Function UnregisterResourceProviders
{
    foreach ($element in $UnregResourceProvidersArray)
    {
        Unregister-AzureRmResourceProvider -ProviderNamespace $element
    }    
}

Function RegisterResourceProviders
{
    foreach ($element in $RegResourceProvidersArray)
    {
        Register-AzureRmResourceProvider -ProviderNamespace $element
    }    
}

$DefaultResources = Get-AzureRmResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState > Defaultlist.log
Write-Output  "Default List of Azure Resource Providers: $DefaultResources"

Try
{
    UnregisterResourceProviders
}
catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Host "Error Message: $ErrorMessage" 
    Write-Host "Failed Item Name: $FailedItem"
}

$UnregResources = Get-AzureRmResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState > Unregistered.log
Write-Output  "Azure Resource Providers after unregistering defaults: $UnregResources"

Try
{
    RegisterResourceProviders
}
catch
{
    $ErrorMessage = $_.Exception.Message
    $FailedItem = $_.Exception.ItemName
    Write-Host "Error Message: $ErrorMessage" 
    Write-Host "Failed Item Name: $FailedItem"
}

Wait 30
$RegResources = Get-AzureRmResourceProvider -ListAvailable | Select-Object ProviderNamespace, RegistrationState > FinalList.log
Write-Output  "Final List of Azure Resource Providers for HUB: $RegResources"

Write-Output  "Registering Features. Might take up to an hour..."
RegisterResourceProvidersFeaturesForZoneAvailability

Happy days!

Leave a Reply

Your email address will not be published.