Private/Utilities.ps1
# Create a 3 digit random number to append to the Faraday Cage resources name function New-Instance { [string]$Random = Get-Random -Maximum 999 return $Random.PadLeft(3,"0") } # This function returns the location of where the FaradayCage will be # The user can pass in the display name or the programmatic value of a location # This code will return the programmatic value for the location provided function Get-Location { [CmdletBinding()] param( [string]$Location ) $GetAzLocation = Get-AzLocation $AzLocation = ($GetAzLocation | Where-Object{$_.Location -eq $Location}) #Tenary, checks for Location vs. DisplayName ex: "East US" vs eastus $AzLocation.Location -eq $Location ? $AzLocation.Location : ($GetAzLocation | Where-Object{$_.DisplayName -like $Location}).Location } # Create the naming suffix for the Faraday Cage resources function Build-Suffix { [CmdletBinding()] param( [string]$Name, [string]$Location ) $Instance = New-Instance $AzLocation = Get-Location -Location $Location $Name + "-" + $AzLocation + "-" + $Instance } # Validate that the user entered in an acceptable faraday cage name that will comply # with azure resource naming conventions. function Test-UserInput { [CmdletBinding()] param( [string]$FCageName ) #regex for characters that are allowed in vm and resource group name $Characters_Allowed = "^[a-zA-Z0-9-]+$" #throw an error if name is empty, contains special characters, or ends with a hyphen if ($FCageName -notmatch $Characters_Allowed){ throw "The name '$FCageName' cannot be empty or contain special characters. Exiting." } if ($FCageName.EndsWith("-")){ throw "The name '$FCageName' cannot end with a hyphen. Exiting." } else{ Write-Host "The name $FCageName meets requirements." } } |