Skip to main content

Example Script

Synopsis

When looking up help on a command, the first thing we tend to look at are the examples. While the rest of the documentation strives to explain all the details of how to use the PSFramework logging and just how it all ties together, here is a quick example script implementing PSFramework logging.

See below the example for further resources.

Example

[CmdletBinding()]
param (
[string]
$LogRoot = 'C:\Logs'
)

# Error Handling
$ErrorActionPreference = 'Stop'
trap {
Write-PSFMessage -Level Warning -Message "Script failed" -ErrorRecord $_
Disable-PSFLoggingProvider -Name logfile -InstanceName $script:_ScriptName
New-PSFSupportPackage -TaskName $script:_ScriptName
throw $_
}

# Logging
$script:_ScriptName = 'MyTask'
$paramSetPSFLoggingProvider = @{
Name = 'logfile'
InstanceName = $script:_ScriptName
FilePath = "$($LogRoot)\$($script:_ScriptName)-%Date%.csv"
Enabled = $true
Wait = $true
}
Set-PSFLoggingProvider @paramSetPSFLoggingProvider

#region Functions
# To Do: Add implementing functions
#endregion Functions

Write-PSFMessage -Message "Starting Script"

# Main

# To Do: Add main logic

Write-PSFMessage -Message "Script completed successfully"
Disable-PSFLoggingProvider -Name logfile -InstanceName $script:_ScriptName

Explanation

  1. The trap statement catches all errors that happen later in the script. This ensures that even if the script fails in some unmanaged manner, we can still wrap up the logging. The support package command will also generate a debug dump (and rotate old dumps, if the same script fails too often).
  2. Then we define the logging as the first thing.
  3. The script does its thing
  4. Finally, we disable the logging as the last line, ensuring that the logging concludes if nothing went wrong.

Resources