functions/ui/Write-HostColored.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function Write-HostColored() {
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true, 
            ValueFromPipeline = $true)]
        [string]$Format,
        [parameter(Mandatory = $false)]
        [ConsoleColor]$ForegroundColor = [System.ConsoleColor]::White,
        [parameter(Mandatory = $false)]
        [ConsoleColor]$HighlightColor = [System.ConsoleColor]::Yellow,
        [parameter(Mandatory = $false)]
        [switch]$PassThru = $false,
        [parameter(Mandatory = $false)]
        [switch]$AddTimestamp = $false
    )

    if ($AddTimestamp) {
        Add-TimeStamp
    }

    $index = 0
    $results = $Format | Select-String '\{\{.+?\}\}' -AllMatches 

    $Format -split '\{\{.+?\}\}' | ForEach-Object { 
        Write-Host $_ -NoNewline -ForegroundColor $ForegroundColor
        $word = ($results.Matches[$index++].Value -replace "{{", "") -replace "}}", ""
        Write-Host $word -ForegroundColor $HighlightColor -NoNewline
    }
    Write-Host ""

    if ($PassThru -eq $true) {
        return ($Format -replace "{{", "") -replace "}}", ""
    }
}