AzRmInPeace.psm1

<#
.Synopsis
   create PowerShell shortcut for Arip
#>

function New-AripShortcut
{
    [CmdletBinding()]
    param(
    [string]$Name
    )
    # Copy Windows PowerShell shortcut.
    $winPsLinkDir = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\Windows PowerShell"
    $WinPsLink = "$winPsLinkDir\Windows PowerShell.lnk"
    $aripLink = "$winPsLinkDir\Arip-$Name.lnk"
    Copy-Item $WinPsLink $aripLink | Out-Null

    # Update the start argument.
    $wshShell = New-Object -comObject WScript.Shell
    $shortcut = $wshShell.CreateShortcut($aripLink)
    $shortcut.Arguments = '-NoExit -Command "& {$env:PSModulePath=$env:PSModulePath + '+ `
        ("';$ModuleRootDir';'Welcome to Arip (Az Rm In Peace)';'The module [$Name] is ready.'") + `
        ' }"'
    $shortcut.Save()
}

<#
.Synopsis
   Install specific module for Arip
#>

function Install-AripModule
{
    [CmdletBinding()]
    param(
    $Name
    )
    # Create module root directory
    $ModuleRootDir = Join-Path $script:AzripRootDir $Name
    if(-not (Test-Path $ModuleRootDir)){
        mkdir $ModuleRootDir | Out-Null
    }

    # Install module
    Write-Host "Searching module [$Name]..."
    $module = Find-Module $Name
    Write-Host "Install module [$Name($($module.version))] started..."
    Save-Module $Name -Path $ModuleRootDir

    # Create shotcuts
    New-AripShortcut -Name $Name
    Write-Host "Install module [$Name($($module.version))] done."
}

<#
.Synopsis
   Install Arip
#>

function Install-Arip
{
    Write-Host "Install Arip (Az Rm In Peace) started..."
    foreach($name in @('AzureRM','Az'))
    {
        $module = Get-Module -Name $name

        if(-not $module){
            Install-AripModule -Name $name
        }
        elseif($module -and (-not $module.ModuleBase.Contains('AzRmInPeace'))){
            throw "Please uninstall $name from $($module.ModuleBase) firstly,then Install-Arip again."
        }
    }

    Write-Host "Install Arip (Az Rm In Peace) done."
}

<#
.Synopsis
   Uninstall Arip
#>

function Uninstall-Arip
{
    Write-Host "Uninstall Arip (Az Rm In Peace) started..."

    # Remove modules location
    if(Test-Path $script:AzripRootDir){
        Remove-Item $script:AzripRootDir -Force -Recurse
    }

    # Remove shortcut
    $winPsLinkDir = Join-Path $env:APPDATA "Microsoft\Windows\Start Menu\Programs\Windows PowerShell"
    foreach($name in @('AzureRm','Az'))
    {
        $aripLink = "$winPsLinkDir\Arip-$name.lnk"
        if(Test-Path $aripLink) {
            Remove-Item $aripLink
        }
    }

    Write-Host "Uninstall Arip (Az Rm In Peace) done."
}


<#
.Synopsis
   Import specific modulefrom Arip
#>

function Import-AripModule
{
    [CmdletBinding()]
    param(
    $Name
    )
    # Append Arip module location to $env:PSModulePath
    $mPath = Join-Path $script:AzripRootDir $Name
    $paths = $env:PSModulePath -split ';'
    if($paths -notcontains $mPath){
        $env:PSModulePath = $env:PSModulePath + ";$mPath"
    }
    Import-Module $Name -ErrorAction SilentlyContinue
    if(-not $?){
        throw "Arip has not installed, you can install it with command 'Install-Arip'"
    }

    $module = Get-Module -Name $Name
    if( -not $module.ModuleBase.Contains('AzRmInPeace'))
    {
        throw "Arip has not installed, you can install it with command 'Install-Arip'"
    }
}

<#
.Synopsis
   Import module Az from Arip
#>

function Import-AripAz
{
    [CmdletBinding()]
    Param()

    if(Get-Command 'Connect-AzureRmAccount' -ErrorAction SilentlyContinue){
        throw "The module Az import failed, as AzureRm module has imported, Az and AzureRM modules cannot be imported in the same session or used in the same script"
    }
    Import-AripModule -Name 'Az'
}

<#
.Synopsis
   Import module AzureRm from Arip
#>

function Import-AripAzureRm
{
    [CmdletBinding()]
    Param()

    if(Get-Command 'Connect-AzAccount' -ErrorAction SilentlyContinue){
        throw "The module AzureRM import failed, as Az module has imported, Az and AzureRM modules cannot be imported in the same session or used in the same script"
    }

    Import-AripModule -Name 'AzureRM'
}

# Module installation location for Arip
$script:AzripRootDir = Join-Path $env:LOCALAPPDATA 'AzRmInPeace'