Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
bulk-registering Azure Resource Providers based on Regular Expression match on provider names
#Requires -Modules AzureServicePrincipalAccount, AzureRM.Profile
<#
=======================================
AUTHOR: Tao Yang
DATE: 15/12/2017
Version: 0.1
Comment: register Azure Resource Providers
=======================================
#>
[CmdletBinding()]
[OutputType([string])]
PARAM (
[Parameter(ParameterSetName='BySPConnection', Mandatory=$true)]
[Alias('Con','Connection')]
[String]$AzureConnectionName,
[Parameter(ParameterSetName='ByCred', Mandatory=$true)]
[Parameter(ParameterSetName='ByInteractiveSignIn', Mandatory=$true)]
[ValidateScript({
try
{
[System.Guid]::Parse($_) | Out-Null
$true
}
catch
{
$false
}
})]
[String]$TenantID,
[Parameter(ParameterSetName='ByCred', Mandatory=$true)]
[Parameter(ParameterSetName='ByInteractiveSignIn', Mandatory=$true)]
[ValidateScript({
try
{
[System.Guid]::Parse($_) | Out-Null
$true
}
catch
{
$false
}
})]
[String]$SubscriptionId,
[Parameter(ParameterSetName = 'ByCred',Mandatory = $true,HelpMessage = 'Please specify the Azure AD credential')]
[Alias('cred')]
[ValidateNotNullOrEmpty()]
[PSCredential]$Credential,
[Parameter(ParameterSetName = 'ByInteractiveSignIn',Mandatory = $true,HelpMessage = 'Please specify the Azure AD user name for interactive sign-in')]
[Alias('u')]
[ValidateNotNullOrEmpty()]
[string]$UserName
)
#region functions
Function Get-AllResourceProviders
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][Hashtable]$headers,
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$subscriptionId
)
$URI = "https://management.azure.com/subscriptions/$SubscriptionId/providers?api-version=2017-05-10"
$Response = Invoke-WebRequest -UseBasicParsing -Uri $URI -Method Get -Headers $headers -ContentType 'application/json'
$Result = (Convertfrom-json $Response.content).value
$Result
}
Function Register-ResourceProvider
{
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][Hashtable]$headers,
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$subscriptionId,
[Parameter(Mandatory = $true)][ValidateNotNullOrEmpty()][string]$Namespace
)
$RegisterURI = "https://management.azure.com/subscriptions/$SubscriptionId/providers/$Namespace`/register?api-version=2017-05-10"
$GetURI = "https://management.azure.com/subscriptions/$SubscriptionId/providers/$Namespace`?api-version=2017-05-10"
Try {
$Response = Invoke-WebRequest -UseBasicParsing -Uri $RegisterURI -Method POST -Headers $headers -ContentType 'application/json'
} Catch {
$Registered = $false
Trow $_.Exception
}
If ($Response.StatusCode -ge 200 -and $Response.StatusCode -le 299)
{
$Registered = $true
} else {
$Registered = $false
}
$Registered
}
#endregion
#region variales
$arrExcludedResourceProvidersRegex = New-Object System.Collections.ArrayList
$arrIncludedResourceProvidersRegex = New-Object System.Collections.ArrayList
$arrRegisterResourceProviders = New-Object System.Collections.ArrayList
[void]$arrIncludedResourceProvidersRegex.Add('^[Mm]icrosoft\..+')
[void]$arrExcludedResourceProvidersRegex.Add('^[Mm]icrosoft.[Cc]lassic.+')
#endregion
#region main
If ($PSCmdlet.ParameterSetName -eq 'BySPConnection')
{
$AzureConnection = Get-AutomationConnection -Name $AzureConnectionName
$token = Get-AzureADToken -AzureServicePrincipalConnection $AzureConnection
$SubscriptionId = $AzureConnection.SubscriptionId
} elseif ($PSCmdlet.ParameterSetName -eq 'ByCred') {
$token = Get-AzureADToken -TenantID $TenantID -Credential $Credential
} else {
$token = Get-AzureADToken -TenantID $TenantID -UserName $UserName
}
$headers = @{ 'Authorization' = $Token }
$ResourceProviders = Get-AllResourceProviders -headers $headers -subscriptionId $SubscriptionId
$UnregisteredNamespaces = ($ResourceProviders | Where-object {$_.registrationState -ieq 'notregistered' -or $_.registrationState -ieq 'unregistered'}).Namespace
Foreach ($namespace in $UnregisteredNamespaces)
{
$bShouldRegister = $false
Foreach ($item in $arrIncludedResourceProvidersRegex)
{
If ($namespace -imatch $item)
{
$bShouldRegister = $true
break
}
}
Foreach ($item in $arrExcludedResourceProvidersRegex)
{
If ($namespace -imatch $item)
{
$bShouldRegister = $false
break
}
}
If ($bShouldRegister)
{
[void]$arrRegisterResourceProviders.Add($namespace)
}
}
Foreach ($RP in $arrRegisterResourceProviders)
{
Write-output "Registering resource provider '$RP'"
$RegistrationResult = Register-ResourceProvider -headers $headers -subscriptionId $SubscriptionId -Namespace $RP
If ($RegistrationResult -eq $false)
{
Write-Error "Failed to register '$RP'."
break
}
}
Write-output "Done."
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.