-
Notifications
You must be signed in to change notification settings - Fork 0
Examples
Before you do anything, install PSWriteLog.
Create a new script, mine will be called foo.ps1
and will contain the following lines:
#Requires -Modules PSWriteLog
Write-Host 'Hello World!'
I love how clean and simple that is!
However, the #Requires
statement will terminate if you don't have PSWriteLog installed.
ℹ: You'll notice that the
Hello World!
message did output to the console as expected.
Because nothing was configured, you can find the log in the default location:
%TEMP%\PowerShell Desktop 5.1.19041.1682 Internal.log
If you open that file, you can see that the log appears in CMTrace format:
<![LOG[Info: Hello World!]LOG]!><time="22:32:05.575-360" date="01-05-2023" component="foo.ps1 {}" context="TEST\VertigoRay" type="6" thread="15" file="foo.ps1:2">
Create a new script, mine will be called foo.ps1
and will contain the following lines:
if (Get-Module 'PSWriteLog' -ListAvailable) {
Import-Module PSWriteLog
}
Write-Host 'Hello World!'
Unlike the previous example, this one ensures there are no errors if you share your script with someone that doesn't have PSWriteLog installed. Doesn't that make it better? It depends, decisions like that are best left to the developer and his/her understanding of the final implementation of the code.
ℹ: You'll notice that the
Hello World!
message did output to the console as expected.
Because nothing was configured, you can find the log in the default location:
%TEMP%\PowerShell Desktop 5.1.19041.1682 Internal.log
If you open that file, you can see that the log appears in CMTrace format:
<![LOG[Info: Hello World!]LOG]!><time="22:32:05.575-360" date="01-05-2023" component="foo.ps1 {}" context="TEST\VertigoRay" type="6" thread="15" file="foo.ps1:2">
#Requires -Modules PSWriteLog
$env:PSWriteLogFilePath = "${env:SystemRoot}\Logs\foo.log"
Write-Host 'Hello World!'
ℹ: You'll notice that the
Hello World!
message did output to the console as expected.
The log location changed to:
%SYSTEMROOT%\Logs\foo.log
If you open that file, you can see that the log appears in CMTrace format:
<![LOG[Info: Hello World!]LOG]!><time="22:32:05.575-360" date="01-05-2023" component="foo.ps1 {}" context="TEST\VertigoRay" type="6" thread="15" file="foo.ps1:2">
#Requires -Modules @{ModuleName = 'PSWriteLog'; RequiredVersion = '2023.1.12.30079'}
Write-Host 'Hello World!'
ℹ: You'll notice that the
Hello World!
message did output to the console as expected.
Because nothing was configured, you can find the log in the default location:
%TEMP%\PowerShell Desktop 5.1.19041.1682 Internal.log
If you open that file, you can see that the log appears in CMTrace format:
<![LOG[Info: Hello World!]LOG]!><time="22:32:05.575-360" date="01-05-2023" component="foo.ps1 {}" context="TEST\VertigoRay" type="6" thread="15" file="foo.ps1:2">