Token.ps1

<#
    .SYNOPSIS
        Replace *.template.* files tokens with their defined values. Parses a Tokens.MachineName.ps1
        to source tokens name.
    .DESCRIPTION
        Replace *.template.* files tokens with their defined values.
        Creates a new file with the replaced tokens that can be used as is within a deployment context.
 
        A token is typically defined as a variable in a separate Configuration file and follow the
        $TKN_MyToken = "Value"
         
        Then, the token can be invoked within a script following the [[TKN_MyToken]] convention.
    .LINK
        Nexus Innovations : http://www.nexusinno.com
    --------------------------------------------------------------------------------------
    Module 'Nexus.PSToolkit'
    by: Nexus Innovations.
    --------------------------------------------------------------------------------------
#>

function global:Update-NexusTokens {
    Param (
        [Parameter(Mandatory=$false, ValueFromPipeline=$true)]
        [ValidateScript({Test-Path $_})]
        [string]$Path = (Get-Location),

        [Parameter(Mandatory=$false)]
        [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]$Encoding = 
        [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]::UTF8,

        [ValidateScript({Test-Path $_})]
        [string]$TemplatePath = (Get-Location),

        [Parameter(Mandatory=$false)]
        [string]$Environment = $env:COMPUTERNAME
    )

    $Path = Resolve-Path $Path
    $TemplatePath = Resolve-Path $TemplatePath
    $tokenPath = (Get-ChildItem -Path $Path -Filter "Tokens.$Environment.ps1")

    if([string]::IsNullOrEmpty($tokenPath)) {
        throw [System.IO.FileNotFoundException] "File 'Tokens.$Environment.ps1' not found!"
    } else {
        if(Test-Path $tokenPath) {
            Write-Verbose "Found token file at : $tokenPath"
            Resolve-Tokens -Path $TemplatePath -TokenPath $tokenPath -Encoding $Encoding
        } else {
            throw [System.IO.FileNotFoundException] "File 'Tokens.$Environment.ps1' not found!"
        }
    }
}

function global:Resolve-Tokens {
    Param (
        [string]$Path,
        [string]$TokenPath,
        [Microsoft.PowerShell.Commands.FileSystemCmdletProviderEncoding]$Encoding
    )

    Write-Verbose "Resolving tokens within $TokenPath"

    # Source tokens
    . ".\$TokenPath"
    $tokens = Get-Variable -Include "TKN_*"
    
    Get-ChildItem -Path $Path -Include "*.template.*" -Recurse | ForEach-Object {
        Write-Verbose "Replacing tokens in file '$_' ..."

        try {
            # Fetch the content of the template file
            $content = Get-Content $_ -Encoding $Encoding -ErrorAction Stop

            # Replace each token found in the content of the file with the value of
            # the token defined in the template file
            $tokens | ForEach-Object {
                $content = $content -replace "\[\[$($_.Name)\]\]", $_.Value
            }

            # Write the updated file content to a new file overriding any existant file ensuring
            # the latest version of the tokens within the scripts
            Set-Content -Encoding $Encoding -Value $content -Path $_.FullName.Replace(".template", [string]::Empty) -Force -ErrorAction Stop

        } catch {
            Write-Error "Failed - $_"
        }

        Write-Verbose "Success!"
    }
}