Private/Utilities.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<#
.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
    }
}