< Back

New-Log

Mon Jan 13, 2020 4:06 am

NAME New-Log



SYNOPSIS

Create a new log.





SYNTAX

New-Log [-Path] <String> [-Header <String>] [-Append] [-MaxLogSize <Int64>] [-MaxLogFiles <Int32>] [-Format

<String>] [-UseGlobalVariable] [<CommonParameters>]



New-Log -EventLogName <String> -EventLogSource <String> [-DefaultEventID <String>] [-UseGlobalVariable]

[<CommonParameters>]





DESCRIPTION

The New-Log function is used to create a new log file or Windows Event log. A log object is also created

and either saved in the global PSLOG variable (default) or sent to the pipeline. The latter is useful if

you need to write to different log files in the same script/function.





PARAMETERS

-Path <String>

Path to log file.



Required? true

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Header <String>

Optionally define a header to be added when a new empty log file is created.



Required? false

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-Append [<SwitchParameter>]

If log file already exist, append instead of creating a new empty log file.



Required? false

Position? named

Default value False

Accept pipeline input? false

Accept wildcard characters? false



-MaxLogSize <Int64>

Maximum size of log file.



Required? false

Position? named

Default value 1048576

Accept pipeline input? false

Accept wildcard characters? false



-MaxLogFiles <Int32>

Maximum number of log files to keep. Default is 3. Setting MaxLogFiles to 0 will keep all log files.



Required? false

Position? named

Default value 3

Accept pipeline input? false

Accept wildcard characters? false



-Format <String>

The format of the log file. Valid choices are 'Minimal', 'PlainText' and 'CMTrace'.

The 'Minimal' format will just pass the log entry to the log file, while the 'PlainText' includes meta-data.

CMTrace format are viewable using the CMTrace.exe tool.



Required? false

Position? named

Default value PlainText

Accept pipeline input? false

Accept wildcard characters? false



-EventLogName <String>

Specifies the name of the event log.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-EventLogSource <String>

Specifies the name of the event log source.



Required? true

Position? named

Default value

Accept pipeline input? false

Accept wildcard characters? false



-DefaultEventID <String>

Define the default Event ID to use when writing to the Windows Event Log.

This Event ID will be used when writing to the Windows log, but can be overrided by the Write-Log function.



Required? false

Position? named

Default value 0

Accept pipeline input? false

Accept wildcard characters? false



-UseGlobalVariable [<SwitchParameter>]

When UseGlobalVariable is True, the log object is saved in the global PSLOG variable,

otherwise it's returned to the pipeline. Default value is True.



Required? false

Position? named

Default value True

Accept pipeline input? false

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



OUTPUTS



NOTES





Author: ????????yvind Kallstad

Date: 21.11.2014

Version: 1.0



-------------------------- EXAMPLE 1 --------------------------



PS C:\\>New-Log '.\\myScript.log'



Create a new log file called 'myScript.log' in the current folder, and save the log object in $global:PSLOG









-------------------------- EXAMPLE 2 --------------------------



PS C:\\>New-Log '.\\myScript.log' -Header 'MyHeader - MyScript' -Append -Format 'CMTrace'



Create a new log file called 'myScript.log' if it doesn't exist already, and add a custom header to it.

The log format used for logging by Write-Log is the CMTrace format.









-------------------------- EXAMPLE 3 --------------------------



PS C:\\>$log1 = New-Log '.\\myScript_log1.log'; $log2 = New-Log '.\\myScript_log2.log'



Create two different logs that can be written to depending on your own internal script logic. Remember to

pass the correct log object to Write-Log!









-------------------------- EXAMPLE 4 --------------------------



PS C:\\>New-Log -EventLogName 'PowerShell Scripts' -EventLogSource 'MyScript'



Create a new log called 'PowerShell Scripts' with a source of 'MyScript', for logging to the Windows Event Log.











RELATED LINKS