BitTitan.Runbooks.Modules.psm1

<#
.SYNOPSIS
    PowerShell module for managing BitTitan Runbooks modules
.NOTES
    Version: 0.2.5
    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
    )

    # Check if the module has already been imported
    if ($moduleName -in (Get-Module).Name) {
        return
    }

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

            # 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

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

            # 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."
            }
        }
    }
}