sitecore-azure.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#
# sitecore_azure.ps1
#

function Get-DefaultCredentialStore()
{
    return Join-Path $env:USERPROFILE -ChildPath 'azure.csv'
}

<#.Synopsis
Use this function to unattendant login to Azure acount.
 
.DESCRIPTION
This function will store credentials to Azure account in encrypted file. Next credantials are used to silent login to Azure account.
Note: If this function is used for a first time then you will be prompted for credentials.
 
.EXAMPLE
    Login-AzureAccountSilent
    Login to Azure account with a default settings - credentials will be stored in file $env:USERPROFILE\azure.csv
 
.EXAMPLE
    Login-AzureAccountSilent -KeyName 'MyPrivateAzureAccount' -CredentialsPath 'C:\MyFavoriteFolder\azurelogin.csv'
#>

function Login-AzureAccountSilent
{
    [CmdletBinding()]
    param
    (
        # KeyName - Name of the key under which the credentials will be stored
        [Parameter()]
        $KeyName = 'AzureLogin',
        # CredentialsPath - Path to the credentials store file
        [parameter()]
        [string]$CredentialsPath = (Get-DefaultCredentialStore)      
    )

    Write-Verbose "Import credentials from $CredentialsPath"
    Import-CredentialStore -Path $CredentialsPath 

    if( -not (Test-StoredCredential -Key $KeyName) )
    {
        Write-Verbose "Credentials with key $KeyName not exist in $CredentialsPath"

        New-StoredCredential -Key $KeyName -Message "Please enter credentials to Azure account"

        Export-CredentialStore -Path $CredentialsPath
    }    


    if( (Get-AzureRmContext).Account -eq $null )
    {
        Login-AzureRmAccount -Credential (Get-StoredPSCredential -Key $KeyName) | Out-Null
    }
    else
    {
        Write-Verbose "Already login to Azure account $($azAccout.Id)"
    }
}


<#
.Synopsis
 Check if all resources required to deploy Sitecore on Azure are supported in Location region
 
.DESCRIPTION
Check if all resources required to deploy Sitecore on Azure are supported in Location region
 
.EXAMPLE
    Test-SitecoreAzureRegion -Location 'West Europe'
 
#>

function Test-SitecoreAzureRegion
{ 
    [CmdletBinding()]
    Param
    (
        # Location name of Azure region
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        $Location
    )

    $resources = @(
        "Microsoft.Cache/Redis";
        "microsoft.insights/alertrules";
        "Microsoft.Insights/components";
        "Microsoft.Search/searchServices";
        "Microsoft.Sql/servers"
        "Microsoft.Sql/servers/databases";
        "Microsoft.Web/serverFarms";
        "Microsoft.Web/sites"
    )

    $unsupportedResources = $false

    foreach( $resource in $resources )
    {
        $splited = $resource -split "/",2

        $isSupported = ((Get-AzureRmResourceProvider -ProviderNamespace $($splited[0])).ResourceTypes | Where-Object ResourceTypeName -eq $($splited[1])).Locations.Contains($location)

        if( $isSupported -eq $false )
        {
            $unsupportedResources = $true
            Write-Verbose "Unsupported resource '$resource' in '$location'"
        }
    }

    if( $unsupportedResources -eq $true )
    {
        Write-Warning "Please check 'https://kb.sitecore.net/articles/617478'"
    }

    return -not $unsupportedResources
}

Export-ModuleMember -Function Test-SitecoreAzureRegion
Export-ModuleMember -Function Login-AzureAccountSilent