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 information messages.
        [Parameter(Mandatory=$false)]
        [Switch]$quiet
    )

    # 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) {
                if (!$quiet) {
                    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") {
                # Retrieve the currently installed modules
                $currentlyInstalledModules = Get-ChildItem $userModulesPath -Directory | Where-Object { $_.FullName -like "*$($moduleName)*" }

                # The module is currently installed
                if ($currentlyInstalledModules) {
                    # Retrieve the versions of the currently installed module and the latest module
                    $installedModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                    $latestModuleVersion = (Find-Module $moduleName).Version

                    # The currently installed module is not the latest version, install the latest version
                    if ($installedModuleVersion -lt $latestModuleVersion) {
                        Install-Module -Name $moduleName -Scope CurrentUser -AllowClobber -Force
                        if (!$quiet) {
                            # Retrieve the version which was just installed
                            $newlyInstalledModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                            Write-Information "Installed '$($moduleName)' version $($newlyInstalledModuleVersion) to replace version $($installedModuleVersion) from the PowerShell Gallery."
                        }
                    }
                }

                # The module is not currently installed, install the module
                else {
                    Install-Module -Name $moduleName -Scope CurrentUser -AllowClobber -Force
                    if (!$quiet) {
                        # Retrieve the version which was installed
                        # Retrieve the highest version currently installed if there is more than one present
                        $installedModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                        Write-Information "Installed '$($moduleName)' version $($installedModuleVersion) from the PowerShell Gallery."
                    }
                }

                # Import the module
                Import-Module -Name "$($userModulesPath)\$($moduleName)" -Force -Global

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

            # Running on the Beta platform
            else {
                # Retrieve the currently installed modules
                $currentlyInstalledModules = Get-ChildItem $userModulesPath -Directory | Where-Object { $_.FullName -like "*$($moduleName)*" }

                # The module is already installed
                if ($currentlyInstalledModules) {
                    # Retrieve the versions of the currently installed module and the latest module
                    $installedModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                    $latestModuleVersion = (Find-Module $moduleName).Version

                    # The currently installed module is not the latest version, install the latest version
                    if ($installedModuleVersion -lt $latestModuleVersion) {
                        Install-Module -Name "$($moduleName).Beta" -Scope CurrentUser -AllowClobber -Force

                        # 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
                        if (!$quiet) {
                            # Retrieve the version which was just installed
                            $newlyInstalledModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                            Write-Information "Installed '$($moduleName).Beta' version $($newlyInstalledModuleVersion) to replace version $($installedModuleVersion) from the PowerShell Gallery as '$($moduleName)'."
                        }
                    }
                }

                # The module is not currently installed, install the module
                else {
                    Install-Module -Name "$($moduleName).Beta" -Scope CurrentUser -AllowClobber -Force

                    # 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
                    if (!$quiet) {
                        # Retrieve the version which was just installed
                        $newlyInstalledModuleVersion = (Get-ChildItem "$($userModulesPath)\$($moduleName)" -Directory | Sort-Object "Name" -Descending)[0].Name
                        Write-Information "Installed '$($moduleName).Beta' version $($newlyInstalledModuleVersion) 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) {
                    if (!$quiet) {
                        Write-Information "Imported '$($moduleName)' (Beta) version $($importedModule.Version) 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)"
    }
}