Skip to content

Instantly share code, notes, and snippets.

@stefanstranger
Created November 21, 2020 11:54
Show Gist options
  • Save stefanstranger/16ab9049ff095279777d7329bd9974a8 to your computer and use it in GitHub Desktop.
Save stefanstranger/16ab9049ff095279777d7329bd9974a8 to your computer and use it in GitHub Desktop.
Check Azure Container Registry Name
# Check if the name myRegistry can be used for a new Azure Registry Container.
# More info: https://docs.microsoft.com/en-us/rest/api/containerregistry/registries/checknameavailability
#region variables
$ACRName = 'myRegistry'
$ResourceType = 'Microsoft.ContainerRegistry/registries'
#endregion
#region Get AccessToken
$Context = Get-AzContext
$azureProfile = [Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureRmProfileProvider]::Instance.Profile
$profileClient = New-Object -TypeName Microsoft.Azure.Commands.ResourceManager.Common.RMProfileClient -ArgumentList ($azureProfile)
$token = $profileClient.AcquireAccessToken($Context.Subscription.TenantId)
#endregion
#region Validate ACR Name
$uri = ('https://management.azure.com/subscriptions/{0}/providers/{1}/checkNameAvailability?api-version=2019-05-01' -f $($Context.Subscription.id), $($ResourceType.Split('/')[0]))
$params = @{
'Uri' = $uri
'ContentType' = 'application/json'
'Headers' = @{
'authorization' = "Bearer $($Token.AccessToken)"
}
'Method' = 'POST'
'Body' = @{
'name' = $ACRName
'type' = $ResourceType
} | ConvertTo-Json
}
Invoke-RestMethod @params
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment