BitTitan.Runbooks.Modules.Beta.psm1

<#
.SYNOPSIS
    PowerShell module for managing BitTitan Runbooks modules
.NOTES
    Version: 0.2.0
    Last updated: 22 October 2018
 
    Copyright (c) BitTitan, Inc. All rights reserved.
    Licensed under the MIT License.
#>


<#
.SYNOPSIS
    This function installs and imports the correct edition (Beta/Prod) of a BitTitan.Runbooks module.
#>

function Import-BT_Module {
    [CmdletBinding(PositionalBinding=$true)]
    param (
        # The name of the module to import
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$moduleName
    )

    <#
    .SYNOPSIS
        This function retrieves the current BitTitan Runbook environment.
    #>

    function Get-BT_RunbookEnvironment {
        # Create an object to store the settings
        $environment = [PSCustomObject]@{}

        # Retrieve the environment settings
        if ($Global:PSDefaultParameterValues.ContainsKey("*-BT_*:Environment")) {
            $environment | Add-Member -NotePropertyName "Environment" -NotePropertyValue $Global:PSDefaultParameterValues["*-BT_*:Environment"]
        }
        else {
            $environment | Add-Member -NotePropertyName "Environment" -NotePropertyValue "Beta"
        }
        if ($Global:PSDefaultParameterValues.ContainsKey("*-BT_*:IsRunningOnLocalMachine")) {
            $environment | Add-Member -NotePropertyName "IsRunningOnLocalMachine" -NotePropertyValue $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"]
        }
        else {
            $environment | Add-Member -NotePropertyName "IsRunningOnLocalMachine" -NotePropertyValue $false
        }

        # Return the object containing the settings
        return $environment
    }

    # Retrieve the BT Runbook Environment
    $environment = Get-BT_RunbookEnvironment

    # Running in Prod environment
    if ($environment.Environment -eq "BT") {
        # Running on the local machine
        if ($environment.IsRunningOnLocalMachine) {
            Import-Module -Name $moduleName -Force -Global

            # Verify imported module
            $importedModule = Get-Module -Name $moduleName -ErrorAction SilentlyContinue
            if ($importedModule) {
                Write-Information "Imported '$($moduleName)' (Prod) from the local machine."
            }
            else {
                throw "Failed to import '$($moduleName)' (Prod) from the local machine."
            }
        }

        # Running on the Prod platform
        else {
            # Uninstall the Beta module if present
            $betaModule = Get-Module -ListAvailable -Name "$($moduleName).Beta"
            if ($betaModule) {
                Uninstall-Module -Name "$($moduleName).Beta" -Force -Confirm:$false
            }

            # Install the Prod module
            Install-Module -Name $moduleName -Scope CurrentUser -AllowClobber

            # Import the Prod module
            Import-Module -Name $moduleName -Force -Global

            # Verify imported module
            $importedModule = Get-Module -Name $moduleName -ErrorAction SilentlyContinue
            if ($importedModule) {
                Write-Information "Imported '$($moduleName)' from the PowerShell Gallery."
            }
            else {
                throw "Failed to import '$($moduleName)' from the PowerShell Gallery."
            }
        }
    }

    # Running in Beta environment
    else {
        # Running on the local machine
        if ($environment.IsRunningOnLocalMachine) {
            Import-Module -Name "$($moduleName)" -Force -Global

            # Verify imported module
            $importedModule = Get-Module -Name $moduleName -ErrorAction SilentlyContinue
            if ($importedModule) {
                Write-Information "Imported '$($moduleName)' (Beta) from the local machine."
            }
            else {
                throw "Failed to import '$($moduleName)' (Beta) from the local machine."
            }
        }

        # Running on the Beta platform
        else {
            # Uninstall the Prod module if present
            $prodModule = Get-Module -ListAvailable -Name $moduleName
            if ($prodModule) {
                Uninstall-Module -Name $moduleName -Force -Confirm:$false
            }

            # Install the Beta module
            Install-Module -Name "$($moduleName).Beta" -Scope CurrentUser -AllowClobber

            # Import the Beta module
            Import-Module -Name "$($moduleName).Beta" -Force -Global
            Write-Information "Installed and imported '$($moduleName).Beta' from the PowerShell Gallery."

            # Verify imported module
            $importedModule = Get-Module -Name "$($moduleName).Beta" -ErrorAction SilentlyContinue
            if ($importedModule) {
                Write-Information "Imported '$($moduleName).Beta' from the PowerShell Gallery."
            }
            else {
                throw "Failed to import '$($moduleName).Beta' from the PowerShell Gallery."
            }
        }
    }
}