1
votes

I have an Azure-hosted service which is automatically deployed (in our build process) using ARM templates, with multiple instances of the service in different resource groups. Several of the resources require globally unique names, and to support this I'm using the uniquestring function (typically against the resource group ID) to generate part of those resource names. Names follow a templated pattern, e.g. Instance Name + Component Name + Unique String = "Example.WebAPI.abcd1234efgh5"

My problem arises later when using Powershell scripts to perform maintenance on those resources - I need the resource name, which means using the same template to generate the name from the instance, component and resource group IDs. However, there is no function in Powershell that returns the same value as the ARM uniquestring function for the same input.

As far as I'm aware, uniquestring is simply generating a hash of the input string and returning it as a 13-character representation; does anyone know the specifics of this hash process so it can be duplicated in Powershell, or if it has already been done?

I've looked at the answer for Can I call ARM template functions outside the JSON deployment template?, but this requires the use of output parameters in the initial ARM template and assumes the value is used immediately; whereas in my use case I need the resource name some weeks or months after the initial deploy.

2

2 Answers

1
votes

The function is not documented anywhere, but its deterministic, meaning you always get the same output if you give the same input, so nothing to worry about.

In case you need to identify the resource afterwards you can come up with a bunch of workaround, you can always get the output of the deployment using Get-AzureRmResourceGroupDeployment or you can tag the resource with something specific what you can infer dynamically or you could filter the resource out using things like time of creation and other indirect factors

0
votes

You can take a look here: https://docs.microsoft.com/en-us/archive/blogs/389thoughts/get-uniquestring-generate-unique-id-for-azure-deployments

#!/usr/bin/env pwsh

function Get-UniqueString ([string]$id, $length=13)
{
    $hashArray = (new-object System.Security.Cryptography.SHA512Managed).ComputeHash($id.ToCharArray())
        -join ($hashArray[1..$length] | ForEach-Object { [char]($_ % 26 + [byte][char]'a') })
}

Get-UniqueString "foobar"