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=$false)]
        [ValidateSet("BT", "Beta")]
        [String]$environment = "Beta",

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

        # Select whether the current environment is a test environment.
        # A test environment is one where unit or integration tests may be run.
        # This environment variable may be queried by functions which may behave differently when
        # running in test and non-test environments, such as filtering objects returned using a
        # test run ID.
        [Parameter(Mandatory=$false)]
        [ValidateNotNull()]
        [Bool]$isTestEnvironment = $false
    )

    # 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 testing environment
    Write-Verbose "Setting IsTestEnvironment to '$($isTestEnvironment)'."
    if (!$Global:PSDefaultParameterValues.ContainsKey("*-BT_*:IsTestEnvironment")) {
        $Global:PSDefaultParameterValues.Add("*-BT_*:IsTestEnvironment", $isTestEnvironment)
    }
    else {
        $Global:PSDefaultParameterValues["*-BT_*:IsTestEnvironment"] = $isTestEnvironment
    }

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