From 3d9bac759c82944c67f32f52a172b02505a0d5c6 Mon Sep 17 00:00:00 2001 From: Raymond Piller Date: Thu, 12 Jan 2023 12:26:24 -0600 Subject: [PATCH] Converting to Environment Variables --- PSWriteLog/Private/Write-InvocationHeader.ps1 | 2 +- PSWriteLog/Private/Write-Log.ps1 | 20 +++++++++---------- README.md | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/PSWriteLog/Private/Write-InvocationHeader.ps1 b/PSWriteLog/Private/Write-InvocationHeader.ps1 index 0571572..edcae05 100644 --- a/PSWriteLog/Private/Write-InvocationHeader.ps1 +++ b/PSWriteLog/Private/Write-InvocationHeader.ps1 @@ -58,5 +58,5 @@ function Write-InvocationHeader { $tmp.FullName | Remove-Item -ErrorAction 'SilentlyContinue' -Force Write-Log -Message $header - $env:PSWriteLogIncludedInvocationHeader = $true + $env:PSWriteLogIncludeInvocationHeader = $null } diff --git a/PSWriteLog/Private/Write-Log.ps1 b/PSWriteLog/Private/Write-Log.ps1 index ef36682..cc01f56 100644 --- a/PSWriteLog/Private/Write-Log.ps1 +++ b/PSWriteLog/Private/Write-Log.ps1 @@ -67,34 +67,34 @@ function global:Write-Log { [Parameter(Mandatory = $false, Position = 4)] [ValidateSet('CMTrace', 'Legacy')] [string] - $LogType = 'CMTrace', + $LogType = $(if ($env:PSWriteLogType) { $env:PSWriteLogType } else { 'CMTrace' }), [Parameter(Mandatory = $false, Position = 5)] [ValidateNotNullorEmpty()] [IO.FileInfo] - $FilePath = [IO.Path]::Combine($env:Temp, ('PowerShell {0} {1} {2}.log' -f $PSVersionTable.PSEdition, $PSVersionTable.PSVersion, $MyInvocation.CommandOrigin)), + $FilePath = $(if ($env:PSWriteLogFilePath) { $env:PSWriteLogFilePath } else { [IO.Path]::Combine($env:Temp, ('PowerShell {0} {1} {2}.log' -f $PSVersionTable.PSEdition, $PSVersionTable.PSVersion, $MyInvocation.CommandOrigin)) }), [Parameter(Mandatory = $false, Position = 6)] [ValidateNotNullorEmpty()] [decimal] - $MaxLogFileSizeMB = 10, + $MaxLogFileSizeMB = $(if ($env:PSWriteLogMaxLogFileSizeMB) { $env:PSWriteLogMaxLogFileSizeMB -as [decimal] } else { 10 }), [Parameter(Mandatory = $false)] - [switch] - $ContinueOnError, + [bool] + $ContinueOnError = $(if ($env:PSWriteLogContinueOnError) { $env:PSWriteLogContinueOnError -as [bool] } else { $false }), [Parameter(Mandatory = $false)] - [switch] - $DisableLogging, + [bool] + $DisableLogging = $(if ($env:PSWriteLogDisableLogging) { $env:PSWriteLogDisableLogging -as [bool] } else { $false }), [Parameter(Mandatory = $false)] - [switch] - $IncludeInvocationHeader + [bool] + $IncludeInvocationHeader = $(if ($env:PSWriteLogIncludeInvocationHeader) { $env:PSWriteLogIncludeInvocationHeader -as [bool] } else { $false }) ) begin { # Microsoft.PowerShell.Utility\Write-Information "[Write-Log] BoundParameters: $($MyInvocation.BoundParameters | Out-String)" -Tags 'VertigoRay\PSWriteLog','Write-Log' - if ($IncludeInvocationHeader.IsPresent -and -Not $env:PSWriteLogIncludedInvocationHeader) { + if ($IncludeInvocationHeader) { Write-InvocationHeader } diff --git a/README.md b/README.md index 5b8d743..4577301 100644 --- a/README.md +++ b/README.md @@ -45,12 +45,12 @@ Instead, we've created [proxy functions](https://learn.microsoft.com/en-us/dotne These proxy functions keep the original functionality of the function intact, while also sending the outputted message to the `Write-Log` function. By default, `Write-Log` will write messages to a log file in [CMTrace](https://learn.microsoft.com/en-us/mem/configmgr/core/support/cmtrace) compatible format, but Legacy (plain-text) file format is also available. To configure *PSWriteLog*, what you need to do is change the default actions of the `Write-Log` parameters. -You can specify default parameters using the built-in [`$PSDefaultParameterValues`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_parameters_default_values?view=powershell-5.1) variable. +You can specify default parameters using environment variables. Here's an example of specifying the Log file path and log type globally: ```powershell -$PSDefaultParameterValues.Add('Write-Log:FilePath', "${env:SystemRoot}\Logs\MyApp.log") -$PSDefaultParameterValues.Add('Write-Log:LogType', 'Legacy') +$env:PSWriteLogFilePath = "${env:SystemRoot}\Logs\MyApp.log" +$env:PSWriteLogType = 'Legacy' ``` > ℹ: For more details, [check out the wiki](/VertigoRay/PSWriteLog/wiki)! @@ -119,7 +119,7 @@ For me, when I'm working on *PSWriteLog* and I want to see just those messages, $InformationPreference = 'Continue' $PSDefaultParameterValues.Set_Item('Write-Log:InformationVariable', 'foo') -Write-Host 'Testing a thing' +Write-Host 'Testing a thing' -InformationVariable 'foo' $foo | ?{ $_.Tags -eq 'Write-Log' } ```