Write.Extensions.psm1
#Requires -Version 5.0; function Get-HR { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $pattern ) [String] $hr = ""; while ($hr.Length -lt ($Host.UI.RawUI.WindowSize.Width - 7)) { $hr += $pattern; } return $hr; } function Write-HR { [CmdletBinding()] Param ( [String] $pattern = "-" ) Write-White "$(Get-Hr $pattern)"; } function Get-ErrorAsString { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $id ) [String] $message = "{ $id }`n"; $message += $(Get-HR "-"); $message += "`n"; if ($null -eq $Error) { $message += "`$Error -eq `$null"; } elseif (0 -eq $Error.Count) { $message += "`$Error.Count -eq 0"; } elseif ($null -eq $Error[0]) { $message += "`$Error[0] -eq `$null"; } else { [System.Management.Automation.ErrorRecord] $err = $Error[0]; $message += $err.Exception.GetType(); $message += "`n"; $message += $err.Exception.Message.Trim(); $message += "`n`n"; $message += $err.ScriptStackTrace; $message += "`n"; } $message += $(Get-Hr "-"); $message += "`n`n"; return $message; } function Write-Exception { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $id ) Write-Host $(Get-ErrorAsString $id) -ForegroundColor ([System.ConsoleColor]::Red); } function Write-ErrorAsVerbose { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $id ) Write-Verbose $(Get-ErrorAsString $id); } function Write-ErrorAsWarning { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $id ) Write-Warning $(Get-ErrorAsString $id); } function Write-Color { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine, [Parameter()] [System.ConsoleColor] $ForegroundColor, [Parameter()] [System.ConsoleColor] $BackgroundColor ) Write-Host -Object $Object -Separator $Separator -NoNewLine:$NoNewLine -ForegroundColor $ForegroundColor -BackgroundColor $BackgroundColor; } function Write-White { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::White)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Gray { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Gray)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkGray { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkGray)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Black { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Black)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Yellow { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Yellow)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkYellow { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkYellow)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Green { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Green)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkGreen { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkGreen)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Red { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Red)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkRed { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkRed)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Blue { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Blue)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkBlue { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkBlue)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Magenta { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Magenta)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkMagenta { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkMagenta)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-Cyan { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::Cyan)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-DarkCyan { [CmdletBinding()] Param ( [Parameter()] [Object] $Object, [Parameter()] [Object] $Separator, [Parameter()] [Switch] $NoNewLine ) Write-Color -ForegroundColor "$([System.ConsoleColor]::DarkCyan)" -BackgroundColor "$([System.Console]::BackgroundColor)" -Object $Object -Separator $Separator -NoNewLine:$NoNewLine; } function Write-KeyValue { [CmdletBinding()] Param ( [Parameter(Mandatory)] [String] $key, [Parameter(Mandatory)] [String] $value, [Parameter()] [String] $style = "style-01" ) switch ($style) { "style-01" { Write-DarkCyan "> " -NoNewLine; Write-Cyan $key; Write-DarkYellow " > " -NoNewLine; Write-Yellow $value; break; } default { throw "Unknown style"; } } } |