Utils.psm1
function Invoke-Cmd { param( # Command [Parameter(Mandatory = $true)] [string] $Command ) $ErrorActionPreference = 'stop' $result = Invoke-Expression "& $Command" if ($LASTEXITCODE -ne 0) { Write-Error -Message ($result -join "`n") } return $result } function Write-ColorOutput { [CmdletBinding()] Param( [Parameter(Mandatory = $False, Position = 1, ValueFromPipeline = $True, ValueFromPipelinebyPropertyName = $True)][Object] $Object, [Parameter(Mandatory = $False, Position = 2, ValueFromPipeline = $True, ValueFromPipelinebyPropertyName = $True)][ConsoleColor] $ForegroundColor, [Parameter(Mandatory = $False, Position = 3, ValueFromPipeline = $True, ValueFromPipelinebyPropertyName = $True)][ConsoleColor] $BackgroundColor, [Switch]$NoNewline ) # Save previous colors $previousForegroundColor = $host.UI.RawUI.ForegroundColor $previousBackgroundColor = $host.UI.RawUI.BackgroundColor # Set BackgroundColor if available if ($BackgroundColor -ne $null) { $host.UI.RawUI.BackgroundColor = $BackgroundColor } # Set $ForegroundColor if available if ($ForegroundColor -ne $null) { $host.UI.RawUI.ForegroundColor = $ForegroundColor } # Always write (if we want just a NewLine) if ($Object -eq $null) { $Object = "" } if ($NoNewline) { [Console]::Write($Object) } else { Write-Output $Object } # Restore previous colors $host.UI.RawUI.ForegroundColor = $previousForegroundColor $host.UI.RawUI.BackgroundColor = $previousBackgroundColor } |