Functions/Import-BT_Module.ps1

<#
.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
    if ($Global:PSDefaultParameterValues.ContainsKey("*-BT_*:Environment")) {
        $environment = $Global:PSDefaultParameterValues["*-BT_*:Environment"]
    }
    else {
        $environment = "Beta"
    }
    if ($Global:PSDefaultParameterValues.ContainsKey("*-BT_*:IsRunningOnLocalMachine")) {
        $isRunningOnLocalMachine = $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"]
    }
    else {
        $isRunningOnLocalMachine = $false
    }

    # Import based on the environment and whether it is running on the local machine
    try {
        if ($isRunningOnLocalMachine) {
            # Import the module
            Import-Module -Name $moduleName -Force -Global

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

        # Running on the platform
        else {
            $userModulesPath = "$($env:USERPROFILE)\Documents\WindowsPowerShell\Modules"

            # Running on the Prod platform
            if ($environment -eq "BT") {
                # Install the Prod module
                if (!(Get-ChildItem -Path "$($userModulesPath)\$($moduleName)" -ErrorAction SilentlyContinue)) {
                    Install-Module -Name $moduleName -Scope CurrentUser -AllowClobber
                    Write-Information "Installed '$($moduleName)' from the PowerShell Gallery."
                }

                # Import the Prod module
                Import-Module -Name "$($userModulesPath)\$($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 on the Beta platform
            else {
                # Install the Beta module
                if (!(Get-ChildItem -Path "$($userModulesPath)\$($moduleName)" -ErrorAction SilentlyContinue)) {
                    Install-Module -Name "$($moduleName).Beta" -Scope CurrentUser -AllowClobber

                    # Remove the ".Beta from the names of the Beta module files"
                    $item = Get-Item -Path "$($userModulesPath)\$($moduleName).Beta\*\$($moduleName).Beta.psd1"
                    Move-Item -Path $item.FullName -Destination ($item.FullName -Replace "$($moduleName).Beta.psd1", "$($moduleName).psd1" ) -Force
                    $item = Get-Item -Path "$($userModulesPath)\$($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 "$($userModulesPath)\$($moduleName).Beta\" `
                        -Destination "$($userModulesPath)\$($moduleName)\" -Force
                    Write-Information "Installed '$($moduleName).Beta' from the PowerShell Gallery as '$($moduleName)'."
                }

                # Import the Beta module
                Import-Module -Name "$($userModulesPath)\$($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
    }
}