AzureStorageTools.psm1

function Get-AzureStorageTools
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$false, Position=0)]
        [Path] $Path = $Env:Temp
    )

    $uri = "http://aka.ms/downloadazcopy"
    
    if(-not(Test-AzureStorageTools))
    {

        "Downloading azure storage tools to $Path" | Write-Verbose

        $msiFile = Join-Path $Path "MicrosoftAzureStorageTools.msi"
        Invoke-WebRequest -Uri $uri -OutFile $msiFile

        "Installing azure storage tools" | Write-Verbose

        $arguments = @(
                        "/i"
                        "`"$msiFile`""
                        "/qn"
                        "/norestart"
                    )

        $proc = Start-Process -FilePath msiexec.exe -ArgumentList $arguments -Wait -PassThru
        if($proc.ExitCode -ne 0) 
        {
            throw "Unexpected error installing azure storage tools"
        }
    }
    else
    {
        "Azure storage tools already installed on your machine" | Write-Verbose
    }
}

function Test-AzureStorageTools
{
    $path = Join-Path $env:ProgramFiles "Microsoft SDKs\Azure\AzCopy"

    if(Test-Path $path -ErrorAction SilentlyContinue)
    {
        return $true
    }
    $path = Join-Path ${env:ProgramFiles(x86)} "Microsoft SDKs\Azure\AzCopy"
    if(Test-Path $path -ErrorAction SilentlyContinue)
    {
        return $true
    }

    return $false
}


Export-ModuleMember *AzureStorageTools