Azure strings – sanitize before passing to Azure PowerShell cmdlets

Azure PowerShell cmdlets don’t like string values with upper-case or special characters, so you need to “sanitize” them before sending them to any of Azure’s PowerShell cmdlets.

Yes, I know, there are *SOME* Azure PowerShell cmdlets that work just fine with mixed-case string values; but, ask yourself this question: do I really want to waste time figuring out which ones allow mixed-case string and which ones don’t, especially the ones used in Azure Storage cmdlets?

To make things easier, I use the following PowerShell function, which I keep inside my FpmAzure.psm1, which I automatically load via Import-Module in my profile.ps1, to “sanitize” string values before I use them in an Azure PowerShell cmdlet.

function Get-SanitizedString
.synopsis sanitizes a string by removing any upper-case or special characters.
.description the input string is scanned, removing any upper-case or special characters.
[string] $originalvalue='AbcDef$1234'
[string] $cleanvalue=Get-SanitizedString -value $originalvalue
the example above returns a string value of 'abcdef1234'
[Parameter(Mandatory=$true, Position=0)]
[string] $Value

# "sanitize" the value before using it to avoid strange error that
# does not point to the real issue: the presence of upper-case and/or
# special characters in the value.
# ATTENTION Microsoft: Please fix this! Soon!
[string] $sanitizedValue = [Regex]::Replace($Value.ToLower(), '[^(a-z0-9)]','')
Write-Output -InputObject $sanitizedValue