< Back
Enable-LogFile
Post
NAME Enable-LogFile
SYNOPSIS
Creates and attaches a new LogFile object, or attaches an existing LogFile object to the host interceptor.
SYNTAX
Enable-LogFile [-Path] <String> [-StreamType <StreamType>] [-DateTimeFormat <String>] [-OnError <ScriptBlock>]
[<CommonParameters>]
Enable-LogFile [-InputObject] <LogFile> [<CommonParameters>]
DESCRIPTION
This cmdlet creates a new LogFile object and attaches it to the current session's host interceptor, or attaches an
existing LogFile object to the interceptor. When creating a new LogFile, the calling script MUST maintain a
reference to the LogFile object in a variable to prevent it from being garbage collected and detached from the
interceptor.
All host output in streams specified by the -StreamType parameter is sent to the log file in addition to the
console. By default, non-blank lines are prepended with the current date and time in a culture-invariant format
(Get-Date -Format 'r'). Log file output from the Error, Warning, Verbose and Debug streams are also prepended
with [E], [W], [V] and [D], respectively.
PARAMETERS
-Path <String>
Specifies the path to the desired log file.
Required? true
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters? false
-StreamType <StreamType>
Specifies which streams should be logged to this file. Valid options are 'Output', 'Verbose', 'Warning'
'Error', 'Debug', and 'All'. You can specify multiple streams in a single comma-separated string, such as
'Output, Error, Warning'.
Required? false
Position? named
Default value All
Accept pipeline input? false
Accept wildcard characters? false
-DateTimeFormat <String>
Specifies the format of timestamps that should be prepended to lines in the log file. Default is 'r'.
Required? false
Position? named
Default value r
Accept pipeline input? false
Accept wildcard characters? false
-OnError <ScriptBlock>
Specifies a script block to be executed if the PSLogFile object encounters any exceptions when attempting to
append to the specified log file. Arguments to this script block are a reference to the PSLogFile object
($args[0]) and the exception that was thrown ($args[1]). The Host Interceptor temporarily suspends
redirection while this Script Block is executing, so you don't have to worry about console output produced by
the OnError script block causing infinite recursion.
While not required, it is recommended that the OnError script block disable this log file by calling $args[0]
| Disable-LogFile , so the OnError block doesn't have to be called repeatedly for every line of output.
Required? false
Position? named
Default value null
Accept pipeline input? false
Accept wildcard characters? false
-InputObject <LogFile>
The existing LogFile object to be attached to the host interceptor.
Required? true
Position? 0
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
PowerShellLogging.LogFile (if attaching an existing object)
OUTPUTS
PowerShellLogging.LogFile (If creating a new object)
None (If attaching an existing object)
NOTES
RELATED LINKS
Disable-LogFile
Get-LogFile
Suspend-Logging
Resume-Logging
about_PowerShellLogging
SYNOPSIS
Creates and attaches a new LogFile object, or attaches an existing LogFile object to the host interceptor.
SYNTAX
Enable-LogFile [-Path] <String> [-StreamType <StreamType>] [-DateTimeFormat <String>] [-OnError <ScriptBlock>]
[<CommonParameters>]
Enable-LogFile [-InputObject] <LogFile> [<CommonParameters>]
DESCRIPTION
This cmdlet creates a new LogFile object and attaches it to the current session's host interceptor, or attaches an
existing LogFile object to the interceptor. When creating a new LogFile, the calling script MUST maintain a
reference to the LogFile object in a variable to prevent it from being garbage collected and detached from the
interceptor.
All host output in streams specified by the -StreamType parameter is sent to the log file in addition to the
console. By default, non-blank lines are prepended with the current date and time in a culture-invariant format
(Get-Date -Format 'r'). Log file output from the Error, Warning, Verbose and Debug streams are also prepended
with [E], [W], [V] and [D], respectively.
PARAMETERS
-Path <String>
Specifies the path to the desired log file.
Required? true
Position? 0
Default value
Accept pipeline input? false
Accept wildcard characters? false
-StreamType <StreamType>
Specifies which streams should be logged to this file. Valid options are 'Output', 'Verbose', 'Warning'
'Error', 'Debug', and 'All'. You can specify multiple streams in a single comma-separated string, such as
'Output, Error, Warning'.
Required? false
Position? named
Default value All
Accept pipeline input? false
Accept wildcard characters? false
-DateTimeFormat <String>
Specifies the format of timestamps that should be prepended to lines in the log file. Default is 'r'.
Required? false
Position? named
Default value r
Accept pipeline input? false
Accept wildcard characters? false
-OnError <ScriptBlock>
Specifies a script block to be executed if the PSLogFile object encounters any exceptions when attempting to
append to the specified log file. Arguments to this script block are a reference to the PSLogFile object
($args[0]) and the exception that was thrown ($args[1]). The Host Interceptor temporarily suspends
redirection while this Script Block is executing, so you don't have to worry about console output produced by
the OnError script block causing infinite recursion.
While not required, it is recommended that the OnError script block disable this log file by calling $args[0]
| Disable-LogFile , so the OnError block doesn't have to be called repeatedly for every line of output.
Required? false
Position? named
Default value null
Accept pipeline input? false
Accept wildcard characters? false
-InputObject <LogFile>
The existing LogFile object to be attached to the host interceptor.
Required? true
Position? 0
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable. For more information, see
about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).
INPUTS
PowerShellLogging.LogFile (if attaching an existing object)
OUTPUTS
PowerShellLogging.LogFile (If creating a new object)
None (If attaching an existing object)
NOTES
RELATED LINKS
Disable-LogFile
Get-LogFile
Suspend-Logging
Resume-Logging
about_PowerShellLogging