BitTitan.Runbooks.Modules.Beta.psm1
<#
.SYNOPSIS PowerShell module for managing BitTitan Runbooks modules .NOTES Version: 0.1 Last updated: 16 October 2018 Copyright (c) BitTitan, Inc. All rights reserved. Licensed under the MIT License. #> <# .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 ) <# .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 } # Retrieve the BT Runbook Environment $environment = Get-BT_RunbookEnvironment # Running in Prod environment if ($environment.Environment -eq "BT") { # Running on the local machine if ($environment.IsRunningOnLocalMachine) { Import-Module -Name $name -Force -Global # Verify imported module $importedModule = Get-Module -Name $name -ErrorAction SilentlyContinue if ($importedModule) { Write-Information "Imported '$($name)' (Prod) from the local machine." } else { throw "Failed to import '$($name)' (Prod) from the local machine." } } # Running on the Prod platform else { # Uninstall the Beta module if present $betaModule = Get-Module -ListAvailable -Name "$($name).Beta" if ($betaModule) { Uninstall-Module -Name "$($name).Beta" -Force -Confirm:$false } # Install the Prod module Install-Module -Name $name -Scope CurrentUser -AllowClobber # Import the Prod module Import-Module -Name $name -Force -Global # Verify imported module $importedModule = Get-Module -Name $name -ErrorAction SilentlyContinue if ($importedModule) { Write-Information "Imported '$($name)' from the PowerShell Gallery." } else { throw "Failed to import '$($name)' from the PowerShell Gallery." } } } # Running in Beta environment else { # Running on the local machine if ($environment.IsRunningOnLocalMachine) { Import-Module -Name "$($name)" -Force -Global # Verify imported module $importedModule = Get-Module -Name $name -ErrorAction SilentlyContinue if ($importedModule) { Write-Information "Imported '$($name)' (Beta) from the local machine." } else { throw "Failed to import '$($name)' (Beta) from the local machine." } } # Running on the Beta platform else { # Uninstall the Prod module if present $prodModule = Get-Module -ListAvailable -Name $name if ($prodModule) { Uninstall-Module -Name $name -Force -Confirm:$false } # Install the Beta module Install-Module -Name "$($name).Beta" -Scope CurrentUser -AllowClobber # Import the Beta module Import-Module -Name "$($name).Beta" -Force -Global Write-Information "Installed and imported '$($name).Beta' from the PowerShell Gallery." # Verify imported module $importedModule = Get-Module -Name "$($name).Beta" -ErrorAction SilentlyContinue if ($importedModule) { Write-Information "Imported '$($name).Beta' from the PowerShell Gallery." } else { throw "Failed to import '$($name).Beta' from the PowerShell Gallery." } } } } |