|
#requires -Module @{ ModuleName = "Pansies"; ModuleVersion = "2.0.0" } |
|
|
|
filter ConvertOutputRecord { |
|
[CmdletBinding()] |
|
param( |
|
[Parameter(Mandatory, ValueFromPipeline)]$InputObject |
|
) |
|
if ($InputObject -is [System.Management.Automation.InformationalRecord]) { |
|
$Type = ($InputObject.PSTypeNames[0] -replace '.*\.([^\.]*)Record$', '$1').ToUpper() |
|
$InputObject = "${Type}: $InputObject".Trim() |
|
|
|
if ($AddTimestamps) { |
|
$InputObject = [DateTimeOffSet]::UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffffffZ ") + $InputObject |
|
} |
|
|
|
if ($VtColored) { |
|
$InputObject = $( |
|
# Put VT colors on each line, instead of just once, because `less` et. al. don't allow them to affect multiple lines? |
|
foreach ($line in $InputObject -split "\n") { |
|
"$(New-Text $line -BackgroundColor $Host.PrivateData."${Type}BackgroundColor" -ForegroundColor $Host.PrivateData."${Type}ForegroundColor")".Trim() |
|
} |
|
) -join "`n" |
|
} |
|
} |
|
if ($InputObject -is [System.Management.Automation.ErrorRecord]) { |
|
if ($ExtendedError) { |
|
$InputObject = $InputObject | Get-Error | Microsoft.PowerShell.Utility\Out-String -Stream | ForEach-Object TrimEnd | Where-Object { $_ } |
|
if ($AddTimestamps) { |
|
$InputObject[0] = "$(New-Text ([DateTimeOffSet]::UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffffffZ")) -BackgroundColor $Host.PrivateData.ErrorBackgroundColor -ForegroundColor $Host.PrivateData.ErrorForegroundColor) " + ($InputObject[0] -replace "Exception", "EXCEPTION") |
|
} |
|
} else { |
|
if ($AddTimestamps) { |
|
$InputObject = [DateTimeOffSet]::UtcNow.ToString("yyyy-MM-ddThh:mm:ss.fffffffZ") + " ERROR: " + $InputObject |
|
} else { |
|
$InputObject = "ERROR: " + "$InputObject".Trim() |
|
} |
|
$InputObject = "$(New-Text $InputObject -BackgroundColor $Host.PrivateData.ErrorBackgroundColor -ForegroundColor $Host.PrivateData.ErrorForegroundColor)".Trim() |
|
} |
|
|
|
} |
|
$InputObject |
|
} |
|
|
|
function Out-String { |
|
[CmdletBinding(DefaultParameterSetName = 'NoNewLineFormatting', HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2097024', RemotingCapability = 'None')] |
|
param( |
|
[Parameter(ParameterSetName = 'StreamFormatting')] |
|
[switch] |
|
${Stream}, |
|
|
|
[ValidateRange(2, 2147483647)] |
|
[int] |
|
${Width}, |
|
|
|
[Parameter(ParameterSetName = 'NoNewLineFormatting')] |
|
[switch] |
|
${NoNewline}, |
|
|
|
# Adds VT Escape Sequences to color VERBOSE, DEBUG, and WARNING messages |
|
[Parameter()] |
|
[switch] |
|
${VtColored}, |
|
|
|
# Forces Errors to display in #PSExtendedError format (as seen in Get-Error) |
|
[Parameter()] |
|
[switch] |
|
${ExtendedError}, |
|
|
|
[Parameter()] |
|
[switch] |
|
${AddTimestamps}, |
|
|
|
[Parameter(ValueFromPipeline = $true)] |
|
[psobject] |
|
${InputObject} |
|
) |
|
|
|
begin { |
|
$null = $PSBoundParameters.Remove("VtColored") |
|
$null = $PSBoundParameters.Remove("ExtendedError") |
|
$null = $PSBoundParameters.Remove("AddTimestamps") |
|
try { |
|
$outBuffer = $null |
|
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) { |
|
$PSBoundParameters['OutBuffer'] = 1 |
|
} |
|
|
|
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Out-String', [System.Management.Automation.CommandTypes]::Cmdlet) |
|
$scriptCmd = { & $wrappedCmd @PSBoundParameters } |
|
|
|
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) |
|
$steppablePipeline.Begin($PSCmdlet) |
|
} catch { |
|
throw |
|
} |
|
} |
|
|
|
process { |
|
try { |
|
$steppablePipeline.Process((ConvertOutputRecord $_)) |
|
} catch { |
|
throw |
|
} |
|
} |
|
|
|
end { |
|
try { |
|
$steppablePipeline.End() |
|
} catch { |
|
throw |
|
} |
|
} |
|
<# |
|
|
|
.ForwardHelpTargetName Microsoft.PowerShell.Utility\Out-String |
|
.ForwardHelpCategory Cmdlet |
|
|
|
#> |
|
} |
|
|
|
function Out-File { |
|
[CmdletBinding(DefaultParameterSetName = 'ByPath', SupportsShouldProcess = $true, ConfirmImpact = 'Medium', HelpUri = 'https://go.microsoft.com/fwlink/?LinkID=2096621')] |
|
param( |
|
[Parameter(ParameterSetName = 'ByPath', Mandatory = $true, Position = 0)] |
|
[Alias('Path')] |
|
[string] |
|
${FilePath}, |
|
|
|
[Parameter(ParameterSetName = 'ByLiteralPath', Mandatory = $true, ValueFromPipelineByPropertyName = $true)] |
|
[Alias('PSPath', 'LP')] |
|
[string] |
|
${LiteralPath}, |
|
|
|
[Parameter(Position = 1)] |
|
[ValidateNotNullOrEmpty()] |
|
[System.Text.Encoding] |
|
${Encoding}, |
|
|
|
[switch] |
|
${Append}, |
|
|
|
[switch] |
|
${Force}, |
|
|
|
[Alias('NoOverwrite')] |
|
[switch] |
|
${NoClobber}, |
|
|
|
[ValidateRange(2, 2147483647)] |
|
[int] |
|
${Width}, |
|
|
|
[switch] |
|
${NoNewline}, |
|
|
|
# Adds VT Escape Sequences to color VERBOSE, DEBUG, and WARNING messages |
|
[Parameter()] |
|
[switch] |
|
${VtColored}, |
|
|
|
# Forces Errors to display in #PSExtendedError format (as seen in Get-Error) |
|
[Parameter()] |
|
[switch] |
|
${ExtendedError}, |
|
|
|
[Parameter()] |
|
[switch] |
|
${AddTimestamps}, |
|
|
|
[Parameter(ValueFromPipeline = $true)] |
|
[psobject] |
|
${InputObject}) |
|
|
|
begin { |
|
$null = $PSBoundParameters.Remove("VtColored") |
|
$null = $PSBoundParameters.Remove("ExtendedError") |
|
$null = $PSBoundParameters.Remove("AddTimestamps") |
|
try { |
|
$outBuffer = $null |
|
if ($PSBoundParameters.TryGetValue('OutBuffer', [ref]$outBuffer)) { |
|
$PSBoundParameters['OutBuffer'] = 1 |
|
} |
|
|
|
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand('Microsoft.PowerShell.Utility\Out-File', [System.Management.Automation.CommandTypes]::Cmdlet) |
|
$scriptCmd = { & $wrappedCmd @PSBoundParameters } |
|
|
|
$steppablePipeline = $scriptCmd.GetSteppablePipeline($myInvocation.CommandOrigin) |
|
$steppablePipeline.Begin($PSCmdlet) |
|
} catch { |
|
throw |
|
} |
|
} |
|
|
|
process { |
|
try { |
|
$steppablePipeline.Process((ConvertOutputRecord $_)) |
|
} catch { |
|
throw |
|
} |
|
} |
|
|
|
end { |
|
try { |
|
$steppablePipeline.End() |
|
} catch { |
|
throw |
|
} |
|
} |
|
<# |
|
|
|
.ForwardHelpTargetName Microsoft.PowerShell.Utility\Out-File |
|
.ForwardHelpCategory Cmdlet |
|
|
|
#> |
|
|
|
} |
|
|
|
Export-ModuleMember -Function *-* |