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) $ThisProfile = $PROFILE if ($IsAdmin) {# use global pwsh profile $ThisProfile = $PROFILE.AllUsersAllHosts } if (-not (Test-Path $ThisProfile)) {# create profile file New-Item $ThisProfile -ItemType File -Force } if ($ThisProfile -and $ENV:PSFREDIRECT -in ('TRUE','ENABLED')) {# add module-import to profile if (-not (Select-String $ThisProfile -Pattern 'Import-Module PSFRedirect')) {# if needed 'Import-Module PSFRedirect' | Out-File $ThisProfile -Append -Encoding utf8 } } if ($ThisProfile -and $ENV:PSFREMOTEPATH) {# add start logging to profile if (-not (Select-String $ThisProfile -Pattern 'Start-PSFRemoteLogging')) {# if needed 'Start-PSFRemoteLogging -FolderPath $ENV:PSFRemotePath' | Out-File $ThisProfile -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 %PSFREDIRECT% to 'TRUE' or 'ENABLED'. Running this as admin will enable redirection for all PowerShell user profiles. .EXAMPLE Enable-PSFRedirect Will active redirection for this sessesion. .EXAMPLE $ENV:PSFREDIRECT = 'TRUE' Import-Module PSFRedirect -Force Will activate redirection for this session and configure your $PROFILE to always pre-load the PSFRedirect module. .EXAMPLE [System.Environment]::SetEnvironmentVariable('PSFRedirect','True','Machine') Start-Process PowerShell.exe -NoNewWindow -Wait Enable-PSFRedirect Will activate redirection for the new session and configure your $PROFILE to always pre-load the PSFRedirect module. If run as admin, the global $PROFILE will be set to pre-load PSFRedirect instead. #> }#end function Enable-PSFRedirect Update-EnvironmentVariable -VariableName PSFRedirect Update-EnvironmentVariable -VariableName PSFRemotePath if ($ENV:PSFREDIRECT -in ('TRUE','ENABLED')) { Enable-PSFRedirect } #EndRegion '.\Public\Enable-PSFRedirect.ps1' 94 #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 Write-PSFMessage -Level InternalComment -Message $InputObject -Tag 'Out-Host' -Function $Caller } <# .ForwardHelpTargetName Microsoft.PowerShell.Core\Out-Host .ForwardHelpCategory Cmdlet #> }# end function #EndRegion '.\Public\Out-MyHost.ps1' 27 #Region '.\Public\Start-PSFRemoteLogging.ps1' -1 function Start-PSFRemoteLogging { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [string]$FolderPath ) if (-not (Get-Module PSFramework)) { throw 'Module PSFramework missing' } $param = @{ Name = 'logfile' InstanceName = 'Remote' FilePath = "$FolderPath/PSF_%ComputerName%_%ProcessId%.log" FileType = 'CSV' Enabled = $true Wait = $true } Set-PSFLoggingProvider @param <# ! Doesn't wait for LoggingProvider if (-not (Test-Path "$FolderPath\PSF_%ComputerName%_%ProcessId%.log")) { throw "Failed to create PSFramework log file at remote $FolderPath" } #> <# .SYNOPSIS Defines a log file for PSFramework to use at a remote location. .DESCRIPTION Defines the logfile LoggingProvider of PSFRamework when an instance named Remote with a filepath using the name PSF_%ComputerName%_%ProcessId%.log By always using PFS_*.log you can easaly set up file specific parsing rules in your favorite log viewer tool. .NOTES The account starting the PowerShell session needs write permission at the share. PSFramework do support setting up persistent log file definitions, but I didn't had the time to figure at a working syntax. .EXAMPLE Start-PSFRemoteLoging -FolderPath \\srv1\logshare\logfolder #> } #EndRegion '.\Public\Start-PSFRemoteLogging.ps1' 50 #Region '.\Public\Update-EnvironmentVariable.ps1' -1 function Update-EnvironmentVariable { [CmdletBinding()] param ( [Parameter(Mandatory, Position = 0)] [string]$VariableName ) $MachineValue = [System.Environment]::GetEnvironmentVariable($VariableName, 'Machine') $UserValue = [System.Environment]::GetEnvironmentVariable($VariableName, 'User') if ($MachineValue) { $ThisValue = $MachineValue } if ($UserValue) { $ThisValue = $UserValue } #User Overrides if ($ThisValue) { Set-Item ENV:$VariableName -Value $ThisValue } <# .SYNOPSIS Updates content of an Environment variable .DESCRIPTION Setting the machine or user envrionement variable within a PowerShell session won't update the $ENV used within that session (or even within a user session). This function iterates over both System and User environments and updates the $ENV:-value within the current session. .EXAMPLE $ENV:MyVar = 'Initial Example' [System.Environment]::SetEnvironmentVariable('MyVar', 'System Example', 'Machine') Update-EnvironmentVariable -VariableName 'MyVar' Sets $ENV:MyVar to the value of the System variable for MyVar. .EXAMPLE $ENV:MyVar = 'Initial Example' [System.Environment]::SetEnvironmentVariable('MyVar', 'User Example', 'User') [System.Environment]::SetEnvironmentVariable('MyVar', 'System Example', 'Machine') Update-EnvironmentVariable -VariableName 'MyVar' Sets $ENV:MyVar to the value of the User variable for MyVar. #> } #EndRegion '.\Public\Update-EnvironmentVariable.ps1' 47 #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 -Tag 'Write-Debug' -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 = "$ErrorRecord - $Exeption" if ($Message) { $LogMessage = "$ErrorRecord - $Exeption - $Message" } Write-PSFMessage -Level Error -Message $LogMessage -Tag 'Write-Error' -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 = "$LogName - $Source : $EntryType : $Message" Write-PSFMessage -Level VeryVerbose -Message $LogMessage -Tag 'Write-EventLog', $Source -Function $Caller <# .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 = "$Object" Write-PSFMessage -Level InternalComment -Message $LogMessage -Tag 'Write-Host' -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 { Microsoft.PowerShell.Utility\Write-Output @PSBoundParameters Write-PSFMessage -Level InternalComment -Message $MessageData -Tag 'Write-Information' -Function $Caller -Tag $Tag } <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Information .ForwardHelpCategory Cmdlet #> } #EndRegion '.\Public\Write-MyInformation.ps1' 26 #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 = "$InputObject" Microsoft.PowerShell.Utility\Write-Output @PSBoundParameters Write-PSFMessage -Level InternalComment -Message $LogMessage -Tag 'Write-Output' -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 = "$Activity - $Status" if ($PercentComplete) { $LogMessage = "$Activity - $Status - $PercentComplete" } if ($SecondsRemaining) { $LogMessage = "$Activity - $Status - $SecondsRemaining" } Write-PSFMessage -Level InternalComment -Message $LogMessage -Tag 'Write-Progress' -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 -Tag 'Write-Warning' -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 -Tag 'Write-Verbose' -Function $Caller <# .ForwardHelpTargetName Microsoft.PowerShell.Utility\Write-Verbose .ForwardHelpCategory Cmdlet #> }# end function #EndRegion '.\Public\Write-MyVerbose.ps1' 19 |