New-AzureStorageAccount : Specified argument was out of the range of valid values. Parameter name: parameters.Name

Recently I have been working on automating deploying an IaaS infrastructure on Azure.. Part of this required me creating my storage accounts.  The automation would allow users to add or remove storage accounts in a list and create them all in one bunch.  I am doing this through a XAML form.  What I found is people would enter in capitol letters.  If you are not familiar with creating storage accounts, when you create them in the portal you can only specify lowercase letters and numbers, this is the same with the New-AzureStorageAccount cmdlet.

image

image

To get around this, in your script you can use Regex Replace and .ToLower to convert the value to lowercase and numbers and letters only.  Where below $sa is an object that I store my values like StorageAccountName, Label, Description and AffinityGroup.

$StorageAccountName = [Regex]::Replace($sa.Name.ToLower(), '[^(a-z0-9)]', '')