Environment.psm1

<#
.SYNOPSIS
    This function sets up the BitTitan Runbook environment.
#>

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 | Out-String
        }

        # BT environment
        if ($environment -eq "BT") {
            $modulesFolder = "$($btDir)\Runbooks\Modules"
            Write-Verbose "Setting PSModulePath to use '$($modulesFolder)'."
            $env:PSModulePath = $env:PSModulePath.Replace(";$($btDir)\BetaModules", "")
            if (!$env:PSModulePath.EndsWith(";$($modulesFolder)")) {
                $env:PSModulePath += ";$($modulesFolder)"
            }
        }

        # Beta environment
        else {
            $betaModulesFolder = "$($btDir)\Runbooks\BetaModules"
            Write-Verbose "Setting PSModulePath to use '$($betaModulesFolder)'."
            $env:PSModulePath = $env:PSModulePath.Replace(";$($btDir)\Modules", "")
            if (!$env:PSModulePath.EndsWith(";$($betaModulesFolder)")) {
                $env:PSModulePath += ";$($betaModulesFolder)"
            }
        }
    }
}

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