Private/Utilities.ps1

<#
.SYNOPSIS
Writes to the console.
 
.DESCRIPTION
Writes a given $message in a standardized form and allows for color overides.
Override colors (Color parameter) must be one in line with Write-Host's ForegroundColor options.
 
.EXAMPLE
Write-Console -message 'Message to write to the console.'
 
.EXAMPLE
Write-Console -message 'Message to write to the console.' -color 'Red'
 
.NOTES
Initial framework author: Cale Vernon (CAVERNON)
#>


function Write-Console
{
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $TRUE
        )][string]$message,
        [Parameter(
            Mandatory = $FALSE
        )][string]$color = 'White'
    )
    
    Write-Host '[' -ForegroundColor 'White' -NoNewline
    Write-Host 'Nanite' -ForegroundColor 'Green' -NoNewline
    Write-Host '] ' -ForegroundColor 'White' -NoNewline
    Write-Host $message -ForegroundColor $color
}

<#
.SYNOPSIS
Validates a resource URI.
 
.DESCRIPTION
Validates a resource URI by checking various pieces for consistency.
Returns a boolean ($TRUE/$FALSE) value.
 
.EXAMPLE
Test-URI -uri <ResourceURI>
 
.NOTES
Initial framework author: Cale Vernon (CAVERNON)
#>

function Test-URI {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory = $TRUE
        )][string]$uri
    )

    # Tokenize the URI.
    $tokens = $uri.Split('/')
    # Check #1: Ensure there are at least 8 slashes ("/").
    If (($tokens.Count - 1) -lt 8) {
        return $FALSE
    }
    # Check #2: Ensure the URI is of a valid format.
    ElseIf (($tokens[1] -ne 'subscriptions') -or ($tokens[3] -ne 'resourceGroups') -or ($tokens[5] -ne 'providers') -or ($tokens[7] -notin $Global:configuration.SupportedResources)) {
        return $FALSE
    }
    # Check #3: Ensure the Subscription ID is a valid GUID.
    ElseIf ($tokens[2] -notmatch '(?im)^[{(]?[0-9A-F]{8}[-]?(?:[0-9A-F]{4}[-]?){3}[0-9A-F]{12}[)}]?$') {
        return $FALSE
    }
    
    # All URI validations have passed.
    Else {
        return $TRUE
    }
}