Export/Private/Test-Parameters.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
function Global:Test-Parameters {
    [CmdletBinding()]
    param(
        $ResourceGroupName,
        $ObjectID,
        $SubscriptionName,
        $KeyVaultName,
        $StorageAccountName
    )
    process {
        $somethingmissing = $false
        $optionalMissing = $false
        Write-Verbose "Validating Parameters"
        if (-not($ObjectID)) {
            Write-Verbose "Missing Parameter: `$ObjectID"
            Write-Verbose " Obtain it by using Get-AzADUser and search for your User"
            $somethingmissing = $true
        }
        if (-not($KeyVaultName)) {
            Write-Verbose "Missing Parameter: `$KeyVaultName"            
            $somethingmissing = $true
        }
        else {
            $keyVault = Get-AzKeyVault -ResourceGroupName $ResourceGroupName -VaultName $KeyVaultName -ErrorAction SilentlyContinue
            if (-not($keyVault)) {
                $StatusCode = $null
                $uri = "https://$($KeyVaultName).vault.azure.net"
                try {
                    $response = invoke-WebRequest -Uri $Uri -ErrorAction Stop
                    $StatusCode = $Response.StatusCode
                }
                catch [System.Net.WebException] { 
                    $StatusCode = $_.Exception.Response.StatusCode.value__    
                }
                if ($null -ne $StatusCode) {
                    throw "KeyVaultName $KeyVaultName is already in use."
                }
            }
        }
        if (-not($StorageAccountName)) {
            Write-Verbose "Missing Parameter: `$StorageAccountName"            
            $somethingmissing = $true
        }
        else {
            $storageAccount = Get-AzStorageAccount -ResourceGroupName $ResourceGroupName -Name $StorageAccountName -ErrorAction SilentlyContinue
            if (-not($storageAccount)) {
                $StatusCode = $null
                $uri = "https://$($StorageAccountName).blob.core.windows.net/"
                try {
                    $response = invoke-WebRequest -Uri $Uri -ErrorAction Stop
                    $StatusCode = $Response.StatusCode
                }
                catch [System.Net.WebException] { 
                    $StatusCode = $_.Exception.Response.StatusCode.value__    
                }
                if ($null -ne $StatusCode) {
                    throw "StorageAccount name $StorageAccountName is already in use."
                }
            }
        }
        if (-not($SubscriptionName)) {
            Write-Verbose "Missing Parameter: `$SubscriptionName"
            Write-Verbose " This one is optional, but make sure that you're connected to the correct subscription"
            Write-Verbose " Currently used subscription is: $((Get-AzContext).Subscription.Name)"
            $optionalMissing = $true
        }
        Write-Verbose "Parameters validated."
        if ($somethingmissing) {
            return $false
        }
        else {
            return $true
        }
    }    
}