BitTitan.Runbooks.Modules.Beta.psm1

<#
.SYNOPSIS
    PowerShell module for managing BitTitan Runbooks modules
.NOTES
    Version: 0.2.6
    Last updated: 23 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,

        # Select whether to suppress messages.
        [Parameter(Mandatory=$false)]
        [Switch]$quiet
    )

    # Suppress messages
    $previousInformationPreference = $InformationPreference
    if ($quiet) {
        $InformationPreference = "SilentlyContinue"
    }

    # Retrieve the BitTitan Runbook Environment settings
    $environment = [PSCustomObject]@{}
    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
    }

    # Import based on the environment and whether it is running on the local machine
    try {
        # 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
                Write-Information "Installed '$($moduleName)' from the PowerShell Gallery."

                # 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 local machine."
                }
                else {
                    throw "Failed to import '$($moduleName)' from the local machine."
                }
            }
        }

        # 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

                # Remove the ".Beta from the names of the Beta module files"
                $item = Get-Item -Path "$($home)\Documents\WindowsPowerShell\Modules\$($moduleName).Beta\*\$($moduleName).Beta.psd1"
                Move-Item -Path $item.FullName -Destination ($item.FullName -Replace "$($moduleName).Beta.psd1", "$($moduleName).psd1" ) -Force
                $item = Get-Item -Path "$($home)\Documents\WindowsPowerShell\Modules\$($moduleName).Beta\*\$($moduleName).Beta.psm1"
                Move-Item -Path $item.FullName -Destination ($item.FullName -Replace "$($moduleName).Beta.psm1", "$($moduleName).psm1" ) -Force

                # Remove the ".Beta" from the name of the Beta module folder
                Move-Item -Path "$($home)\Documents\WindowsPowerShell\Modules\$($moduleName).Beta\" -Destination "$($home)\Documents\WindowsPowerShell\Modules\$($moduleName)\" -Force
                Write-Information "Installed '$($moduleName).Beta' from the PowerShell Gallery as '$($moduleName)'."

                # Import the Beta module
                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."
                }
            }
        }
    }
    catch {
        Write-Error "Exception occurred on line $($_.InvocationInfo.ScriptLineNumber): `r`n$($_.Exception.Message)"
    }
    finally {
        $InformationPreference = $previousInformationPreference
    }
}