functions/private/Test-AzureResourceLocation.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
function Test-AzureResourceLocation {
    [OutputType([System.Collections.ArrayList])]
    [CmdletBinding(DefaultParameterSetName="Type")]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string] $Location,
        [Parameter(Mandatory=$true,ParameterSetName="Type")]
        [ValidateNotNullOrEmpty()]
        [string[]] $ResourceType,
        [Parameter(Mandatory=$true,ParameterSetName="File")]
        [ValidateNotNullOrEmpty()]
        [string] $ResourceFile

    )
    
    begin {
        $ErrorString = "Error while checking Azure location compatibility: "
        try{
            $Resources = @()
            $InvalidResources = new-object System.Collections.ArrayList<String>
            $CompatibleResources = (Get-AzureRMLocation)|where{$_.Location -eq $Location}
            write-verbose "Found Location: $CompatibleResources"
            if(!$Location){
                throw "Invalid Azure location provided"
            }
            switch($PSCmdlet.ParameterSetName){
                "Type" {
                    foreach($String in $ResourceType){
                        $Resources += $String.Split('/')[0]
                    }
                }

                "File" {
                    foreach($String in (get-content $ResourceFile)){
                        $Resources += $String.Split('/')[0]
                    }
                }
            }
            $Resources = $Resources|select -Unique
        }
        catch{
            throw "$($ErrorString): $_"
        }
    }
    
    process {
        try{
            foreach($Resource in $Resources){
                if($CompatibleResources.Providers -contains $Resource){
                    write-verbose "$Resource OK"
                } else {
                    $InvalidResources.Add($Resource)
                    write-verbose "$Resource NOK"
                }
            }
        }
        catch {
            throw "$($ErrorString): $_"
        }

    }
    
    end {
        return $InvalidResources
    }
    
    
}