PSGSuite.psm1

Param
(
    [parameter(Position = 0)]
    [System.Byte[]]
    $EncryptionKey = $(if (Get-Command Import-SCRTKey -ErrorAction SilentlyContinue) {
        Import-SCRTKey
    }
    else {
        $null
    }),
    [parameter(Position = 1)]
    [string]
    $ConfigName = $null
)
#Get public and private function definition files.
$Public = @(Get-ChildItem -Recurse -Path $PSScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue)
$Private = @(Get-ChildItem -Recurse -Path $PSScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue)
$ModuleRoot = $PSScriptRoot

#Execute a scriptblock to load each function instead of dot sourcing
foreach ($file in @($Public + $Private)) {
    try {
        $ExecutionContext.InvokeCommand.InvokeScript(
            $false, 
            (
                [scriptblock]::Create(
                    [io.file]::ReadAllText(
                        $file.FullName,
                        [Text.Encoding]::UTF8
                    )
                )
            ), 
            $null, 
            $null
        )
    }
    catch {
        Write-Error "[$($file.Basename)] $($_.Exception.Message.ToString())"
    }
}

Import-GoogleSDK

$aliasHash = . "$ModuleRoot\Aliases\PSGSuite.Aliases.ps1"

foreach ($key in $aliasHash.Keys) {
    try {
        Set-Alias -Name $key -Value $aliasHash[$key] -Force
    }
    catch {
        Write-Error "[ALIAS: $($key)] $($_.Exception.Message.ToString())"
    }
}

if (!(Test-Path (Join-Path "~" ".scrthq"))) {
    New-Item -Path (Join-Path "~" ".scrthq") -ItemType Directory -Force | Out-Null
}

if ($PSVersionTable.ContainsKey('PSEdition') -and $PSVersionTable.PSEdition -eq 'Core' -and !$EncryptionKey -and !$IsWindows) {
    if (!(Test-Path (Join-Path (Join-Path "~" ".scrthq") "BlockCoreCLREncryptionWarning.txt"))) {
        Write-Warning "CoreCLR does not support DPAPI encryption! Setting a basic AES key to prevent errors. Please create a unique key as soon as possible as this will only obfuscate secrets from plain text in the Configuration, the key is not secure as is. If you would like to prevent this message from displaying in the future, run the following command:`n`nBlock-CoreCLREncryptionWarning`n"
    }
    $EncryptionKey = [Byte[]]@(1..16)
    $ConfigScope = "User"
}

if ($EncryptionKey -is [System.Security.SecureString]) {
    $Method = "SecureString"
    if (!$ConfigScope) {
        $ConfigScope = "Machine"
    }
}
elseif ($EncryptionKey -is [System.Byte[]]) {
    $Method = "AES Key"
    if (!$ConfigScope) {
        $ConfigScope = "Machine"
    }
}
else {
    $Method = "DPAPI"
    $ConfigScope = "User"
}

Add-MetadataConverter -Converters @{
    [SecureString] = {
        $encParams = @{}
        if ($script:EncryptionKey -is [System.Byte[]]) {
            $encParams["Key"] = $script:EncryptionKey
        }
        elseif ($script:EncryptionKey -is [System.Security.SecureString]) {
            $encParams["SecureKey"] = $script:EncryptionKey
        }
        'Secure "{0}"' -f (ConvertFrom-SecureString $_ @encParams)
    }
    "Secure" = {
        param([string]$String)
        $encParams = @{}
        if ($script:EncryptionKey -is [System.Byte[]]) {
            $encParams["Key"] = $script:EncryptionKey
        }
        elseif ($script:EncryptionKey -is [System.Security.SecureString]) {
            $encParams["SecureKey"] = $script:EncryptionKey
        }
        ConvertTo-SecureString $String @encParams
    }
}
try {
    $confParams = @{
        Scope = $ConfigScope
    }
    if ($ConfigName) {
        $confParams["ConfigName"] = $ConfigName
        $Script:ConfigName = $ConfigName
    }
    try {
        Get-PSGSuiteConfig @confParams -ErrorAction Stop
    }
    catch {
        if (Test-Path "$ModuleRoot\$env:USERNAME-$env:COMPUTERNAME-$env:PSGSuiteDefaultDomain-PSGSuite.xml") {
            Get-PSGSuiteConfig -Path "$ModuleRoot\$env:USERNAME-$env:COMPUTERNAME-$env:PSGSuiteDefaultDomain-PSGSuite.xml" -ErrorAction Stop
            Write-Warning "No Configuration.psd1 found at scope '$ConfigScope'; falling back to legacy XML. If you would like to convert your legacy XML to the newer Configuration.psd1, run the following command:`n`nGet-PSGSuiteConfig -Path '$ModuleRoot\$env:USERNAME-$env:COMPUTERNAME-$env:PSGSuiteDefaultDomain-PSGSuite.xml' -PassThru | Set-PSGSuiteConfig`n"
        }
        else {
            Write-Warning "There was no config returned! Please make sure you are using the correct key or have a configuration already saved."
        }
    }
}
catch {
    Write-Warning "There was no config returned! Please make sure you are using the correct key or have a configuration already saved."
}
finally {
    Export-ModuleMember -Function $Public.Basename -Alias *
}