PSFRedirect.psm1
#Region '.\Public\Disable-PFSRedirect.ps1' -1 # if ($ENV:PSFREDIRECT -in ('FALSE','DISABLED')) { # Remove-Item Alias:'Write-Verbose' # Remove-Item Alias:'Write-Debug' # Remove-Item Alias:'Write-Warning' # Remove-Item Alias:'Write-Error' # Remove-Item Alias:'Write-Host' # Remove-Item Alias:'Write-Output' # Remove-Item Alias:'Write-Progress' # Remove-Item Alias:'Out-Host' # } #EndRegion '.\Public\Disable-PFSRedirect.ps1' 12 #Region '.\Public\Enable-PSFRedirect.ps1' -1 function Enable-PSFRedirect { if (-not (Get-Alias Write-Warning -ErrorAction SilentlyContinue)) { Write-Warning -Message 'All Write commands are being redirected to Write-PSFMessage' Set-Alias Write-Information -Value Write-MyInformation -Scope Global Set-Alias Write-Verbose -Value Write-MyVerbose -Scope Global Set-Alias Write-Debug -Value Write-MyDebug -Scope Global Set-Alias Write-Warning -Value Write-MyWarning -Scope Global Set-Alias Write-Error -Value Write-MyError -Scope Global Set-Alias Write-Host -Value Write-MyHost -Scope Global Set-Alias Write-Output -Value Write-MyOutput -Scope Global Set-Alias Write-Progress -Value Write-MyProgress -Scope Global Set-Alias Write-Eventlog -Value Write-MyEventlog -Scope Global Set-Alias Out-Host -Value Out-MyHost -Scope Global }# end if module exists $CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent() $Principal = New-Object System.Security.Principal.WindowsPrincipal($CurrentUser) $IsAdmin = $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator) $MyProfile = $PROFILE $MachinePSFRedirect = [System.Environment]::GetEnvironmentVariable('PSFRedirect', 'Machine') if ($IsAdmin -and $MachinePSFRedirect -in ('TRUE','ENABLED') ) {# use global pwsh profile $MyProfile = $PROFILE.AllUsersAllHosts } if (-not (Test-Path $MyProfile)) {# create profile file New-Item $MyProfile -ItemType File -Force } if ($MyProfile -and $ENV:PSFREDIRECT -in ('TRUE','ENABLED')) {# add module-import to profile if (-not (Select-String $Profile -Pattern 'Import-Module PSFRedirect')) {# if needed 'Import-Module PSFRedirect' | Out-File $MyProfile -Append -Encoding utf8 } } <# .SYNOPSIS Enables redirection of all Write-commands of PowerShell to use Write-PSFMessage. .DESCRIPTION Enables redirecting all Write-commands of PowerShell to use Write-PSFMessage of the module PSFramework such as Write-Verbose, Write-Host, Write-Output etc. .NOTES To configure your environment to always redirect Write-messages to Write-PSFMessage, set the machine environment variable %PFSREDIRECT% to 'TRUE' or 'ENABLED'. Running this as admin will enable redirection for all PowerShell user profiles. .EXAMPLE Enable-PSFRedirect Write-Warning 'Hello world' WARNING: [20:31:05][<ScriptBlock>] Hello world Get-PSFMessage Timestamp FunctionName Line Level TargetObject Message --------- ------------ ---- ----- ------------ ------- 2025-01-22 20:31:05 <ScriptBlock> 50 Warning Hello world .EXAMPLE $ENV:PSFREDIRECT = 'TRUE' Import-Module PFSRedirect -Force Write-Warning 'Hello world' WARNING: [20:37:17][<ScriptBlock>] Hello world Get-PSFMessage Timestamp FunctionName Line Level TargetObject Message --------- ------------ ---- ----- ------------ ------- 2025-01-22 20:37:17 <ScriptBlock> 50 Warning Hello world #> }#end function Enable-PSFRedirect if ($ENV:PSFREDIRECT -in ('TRUE','ENABLED')) { Enable-PSFRedirect } #EndRegion '.\Public\Enable-PSFRedirect.ps1' 88 #Region '.\Public\Out-MyHost.ps1' -1 function Out-MyHost { [CmdletBinding()] param ( [Parameter(ValueFromPipeline)] [psobject[]]$InputObject, [switch]$Paging ) begin { $Caller = (Get-PSCallStack)[1].Command } process { Microsoft.PowerShell.Core\Out-Host @PSBoundParameters $LogMessage = "OUT: $InputObject" Write-PSFMessage -Level InternalComment -Message $LogMessage -Function $Caller } <# .ForwardHelpTargetName Microsoft.PowerShell.Core\Out-Host .ForwardHelpCategory Cmdlet #> }# end function #EndRegion '.\Public\Out-MyHost.ps1' 29 #Region '.\Public\Write-MyDebug.ps1' -1 function Write-MyDebug { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [String]$Message ) $Caller = (Get-PSCallStack)[1].Command Write-PSFMessage -Level Debug -Message $Message -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Debug .ForwardHelpCategory Cmdlet #> }# end function Write-MyDebug #EndRegion '.\Public\Write-MyDebug.ps1' 19 #Region '.\Public\Write-MyError.ps1' -1 function Write-MyError { [CmdletBinding(DefaultParameterSetName = 'Message')] param ( [Parameter(ParameterSetName = 'Message')] [System.Management.Automation.ErrorCategory]$Category, [string]$CategoryActivity, [string]$CategoryReason, [string]$CategoryTargetName, [string]$CategoryTargetType, [Parameter(ParameterSetName = 'Message')] [Parameter(ParameterSetName = 'Exception')] [string]$ErrorId, [Parameter(ParameterSetName = 'ErrorRecord', Mandatory)] [System.Management.Automation.ErrorRecord]$ErrorRecord, [Parameter(ParameterSetName = 'Exception', Mandatory)] [Exception]$Exception, [Parameter(ParameterSetName = 'Exception')] [Parameter(ParameterSetName = 'Message', Position = 0)] [string]$Message, [string]$RecommendedAction, [Parameter(ParameterSetName = 'Exception')] [Parameter(ParameterSetName = 'Message')] [Object]$TargetObject ) $Caller = (Get-PSCallStack)[1].Command Microsoft.PowerShell.Utility\Write-Error @PSBoundParameters $LogMessage = "ERROR: $ErrorRecord - $Exeption" if ($Message) { $LogMessage = "ERROR: $ErrorRecord - $Exeption - $Message" } Write-PSFMessage -Level Error -Message $LogMessage -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Error .ForwardHelpCategory Cmdlet #> }#end function Write-MyError #EndRegion '.\Public\Write-MyError.ps1' 53 #Region '.\Public\Write-MyEventlog.ps1' -1 function Write-MyEvenlog { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [string]$LogName, [Parameter(Mandatory, Position = 1)] [string]$Source, [Parameter(Mandatory, Position = 2)] [int]$EventId, [Parameter(Position = 3)] [EventEntryType]$EntryType, [Parameter(Mandatory, Position = 4)] [string]$Message, [int16]$Category, [byte[]]$Rawdata, [string]$ComputerName ) $Caller = (Get-PSCallStack)[1].Command Microsoft.PowerShell.Utility\Write-Eventlog @PSBoundParameters $LogMessage = "EVENTLOG: $LogName - $Source : $EntryType : $Message" Write-PSFMessage -Level VeryVerbose -Message $LogMessage -Function $Caller -Tag $Source <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Eventlog .ForwardHelpCategory Cmdlet #> }# end function Write-MyProgress #EndRegion '.\Public\Write-MyEventlog.ps1' 41 #Region '.\Public\Write-MyHost.ps1' -1 function Write-MyHost { [CmdletBinding()] param ( [ConsoleColor]$BackgroundColor, [ConsoleColor]$ForeGroundColor, [switch]$NoNewline, [Parameter(Position = 0)] $Object, $Separator ) $Caller = (Get-PSCallStack)[1].Command Microsoft.PowerShell.Utility\Write-Host @PSBoundParameters $LogMessage = "HOST: $Object" Write-PSFMessage -Level InternalComment -Message $LogMessage -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Host .ForwardHelpCategory Cmdlet #> }# end function Write-MyHost #EndRegion '.\Public\Write-MyHost.ps1' 27 #Region '.\Public\Write-MyInformation.ps1' -1 function Write-MyOutput { [CmdletBinding()] param ( [Parameter(Mandatory,Position = 0)] [psobject]$MessageData, [Parameter(Position = 1)] [string[]]$Tag ) begin { $Caller = (Get-PSCallStack)[1].Command } process { $LogMessage = "$MessageData" Microsoft.PowerShell.Utility\Write-Output @PSBoundParameters Write-PSFMessage -Level InternalComment -Message $LogMessage -Function $Caller -Tag $Tag } <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Information .ForwardHelpCategory Cmdlet #> } #EndRegion '.\Public\Write-MyInformation.ps1' 28 #Region '.\Public\Write-MyOutput.ps1' -1 function Write-MyOutput { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline,Position = 0)] [psobject[]]$InputObject, [switch]$NoEnumerate ) begin { $Caller = (Get-PSCallStack)[1].Command } process { $LogMessage = "OUTPUT: $InputObject" Microsoft.PowerShell.Utility\Write-Output @PSBoundParameters Write-PSFMessage -Level InternalComment -Message $LogMessage -Function $Caller } <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Output .ForwardHelpCategory Cmdlet #> } #EndRegion '.\Public\Write-MyOutput.ps1' 27 #Region '.\Public\Write-MyProgress.ps1' -1 function Write-MyProgress { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [string]$Activity, $Completed, [string]$CurrentOperation, [int]$Id, [int]$ParentId, [int]$PercentComplete, [int]$SecondsRemaining, [int]$SourceId, [string]$Status = 'Processing' ) $Caller = (Get-PSCallStack)[1].Command Microsoft.PowerShell.Utility\Write-Progress @PSBoundParameters $LogMessage = "PROGRESS: $Activity - $Status" if ($PercentComplete) { $LogMessage = "PROGRESS: $Activity - $Status - $PercentComplete" } if ($SecondsRemaining) { $LogMessage = "PROGRESS: $Activity - $Status - $SecondsRemaining" } Write-PSFMessage -Level InternalComment -Message $LogMessage -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Progress .ForwardHelpCategory Cmdlet #> }# end function Write-MyProgress #EndRegion '.\Public\Write-MyProgress.ps1' 37 #Region '.\Public\Write-MyWarning.ps1' -1 function Write-MyWarning { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [String]$Message ) $Caller = (Get-PSCallStack)[1].Command Write-PSFMessage -Level Warning -Message $Message -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Warning .ForwardHelpCategory Cmdlet #> }# end function Write-MyWarning #EndRegion '.\Public\Write-MyWarning.ps1' 19 #Region '.\Public\Write-MyVerbose.ps1' -1 function Write-MyVerbose { [CmdletBinding()] param ( [Parameter(Mandatory)] [String]$Message ) $Caller = (Get-PSCallStack)[1].Command Write-PSFMessage -Level Verbose -Message $Message -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Verbose .ForwardHelpCategory Cmdlet #> }# end function #EndRegion '.\Public\Write-MyVerbose.ps1' 19 |