Functions/Set-BT_RunbookEnvironment.ps1

<#
.SYNOPSIS
    This function sets up the BitTitan Runbook environment.
.DESCRIPTION
    This function sets up the BitTitan Runbook environment.
    It does so by setting default values for the Environment and IsRunningOnLocalMachine parameters
    when invoking *-BT_* cmdlets/functions.
#>

function Set-BT_RunbookEnvironment {
    [CmdletBinding(PositionalBinding=$false)]
    param (
        # The BitTitan environment.
        [Parameter(Mandatory=$true)]
        [ValidateSet("BT", "Beta")]
        [String]$environment,

        # Select whether the current environment is on a local machine or on MSPComplete.
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [Bool]$isRunningOnLocalMachine
    )

    # Set the BT environment
    Write-Verbose "Setting BT Environment to '$($environment)'."
    if (!$Global:PSDefaultParameterValues.ContainsKey("*-BT_*:Environment")) {
        $Global:PSDefaultParameterValues.Add("*-BT_*:Environment", $environment)
    }
    else {
        $Global:PSDefaultParameterValues["*-BT_*:Environment"] = $environment
    }

    # Set the local machine setting
    Write-Verbose "Setting IsRunningOnLocalMachine to '$($isRunningOnLocalMachine)'."
    if (!$Global:PSDefaultParameterValues.ContainsKey("*-BT_*:IsRunningOnLocalMachine")) {
        $Global:PSDefaultParameterValues.Add("*-BT_*:IsRunningOnLocalMachine", $isRunningOnLocalMachine)
    }
    else {
        $Global:PSDefaultParameterValues["*-BT_*:IsRunningOnLocalMachine"] = $isRunningOnLocalMachine
    }

    # Set the PSModulePath if running on the local machine
    if ($isRunningOnLocalMachine) {
        # Retrieve the BtEnvGitHome if available
        if (![String]::IsNullOrWhiteSpace($env:BtEnvGitHome)) {
            $btDir = $env:BtEnvGitHome
        }

        # Check in the current directory
        else {
            $btDir = Get-Location | Split-Path -Parent
        }
        $modulesFolder = "$($btDir)\Runbooks\Modules"
        $betaModulesFolder = "$($btDir)\Runbooks\BetaModules"
        $modulesFolderRegex = [System.Text.RegularExpressions.Regex]::Escape($modulesFolder)
        $betaModulesFolderRegex = [System.Text.RegularExpressions.Regex]::Escape($betaModulesFolder)

        # BT environment
        if ($environment -eq "BT") {
            Write-Verbose "Setting PSModulePath to use '$($modulesFolder)'."
            if ($env:PSModulePath -match ";$($betaModulesFolderRegex)") {
                $env:PSModulePath = $env:PSModulePath -replace ";$($betaModulesFolderRegex)", ""
            }
            if ($env:PSModulePath -notMatch "$($modulesFolderRegex)") {
                $env:PSModulePath += ";$($modulesFolder)"
            }
        }

        # Beta environment
        else {
            Write-Verbose "Setting PSModulePath to use '$($betaModulesFolder)'."
            if ($env:PSModulePath -match ";$($modulesFolderRegex)") {
                $env:PSModulePath = $env:PSModulePath -replace ";$($modulesFolderRegex)", ""
            }
            if ($env:PSModulePath -notMatch "$($betaModulesFolderRegex)") {
                $env:PSModulePath += ";$($betaModulesFolder)"
            }
        }
    }
}