sitecore-azure.ps1

#
# 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