Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified Module/PSLogging/PSLogging.psd1
Binary file not shown.
625 changes: 17 additions & 608 deletions Module/PSLogging/PSLogging.psm1

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions Module/PSLogging/private/Get-LogDate.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function Get-LogDate {
<#
.SYNOPSIS
Creates a Date and Timestamp to be used in log file creation
.DESCRIPTION
Creates a nicely formatted timestamp for use in log creation
.PARAMETER Format
Displays the date and time in the Microsoft .NET Framework format indicated by the format specifier.
Enter a format specifier. For a list of available format specifiers, see DateTimeFormatInfo Class http://msdn.microsoft.com/library/system.globalization.datetimeformatinfo.aspx (http://msdn.microsoft.com/library/system.globalization.datetimeformatinfo.aspx) in MSDN.
.EXAMPLE
Get-LogDate
.EXAMPLE
Get-LogDate -Format 'yyyy-MM-dd HH:mm:ss'
.EXAMPLE
Get-LogDate
.NOTES
Default formatting if no formatting provided: 'yyyy-MM-dd HH:mm:ss'
#>
Param(
[Parameter(Position=0)]
[String]$Format = 'MM-dd-yyyy HH:mm:ss.ffffff'
)

Get-Date -Format ("{0}" -f $Format)
}
39 changes: 39 additions & 0 deletions Module/PSLogging/public/Export-Log.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function Export-Log
{
param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$LogPath,
[Parameter(Mandatory = $true, Position = 1)]
[string]$Outfile,
[Parameter(Mandatory = $true, Position = 2)]
[ValidateSet("Csv", "Json", "Xml")]
[string]$Format
)

$Data = Import-Csv -Path $LogPath -Delimiter ";" -Header TimeStamp, MessageType, Message #needs foutafhandeling

if ($Data)
{
if ($Format -eq "Csv")
{
$Data | Export-Csv $Outfile -UseCulture -NoTypeInformation
}

if ($Format -eq "Json")
{
$Data | ConvertTo-Json | Out-File $Outfile
}

if ($Format -eq "Xml")
{
$Data | Export-Clixml $Outfile
}
}
else
{
Write-Error "Cannot export, no data or not a correct log file"
}
}



90 changes: 90 additions & 0 deletions Module/PSLogging/public/Send-Log.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
Function Send-Log
{
<#
.SYNOPSIS
Emails completed log file to list of recipients

.DESCRIPTION
Emails the contents of the specified log file to a list of recipients

.PARAMETER SMTPServer
Mandatory. FQDN of the SMTP server used to send the email. Example: smtp.google.com

.PARAMETER LogPath
Mandatory. Full path of the log file you want to email. Example: C:\Windows\Temp\Test_Script.log

.PARAMETER EmailFrom
Mandatory. The email addresses of who you want to send the email from. Example: "admin@9to5IT.com"

.PARAMETER EmailTo
Mandatory. The email addresses of where to send the email to. Seperate multiple emails by ",". Example: "admin@9to5IT.com, test@test.com"

.PARAMETER EmailSubject
Mandatory. The subject of the email you want to send. Example: "Cool Script - [" + (Get-Date).ToShortDateString() + "]"

.INPUTS
Parameters above

.OUTPUTS
Email sent to the list of addresses specified

.NOTES
Version: 1.0
Author: Luca Sturlese
Creation Date: 05.10.12
Purpose/Change: Initial function development.

Version: 1.1
Author: Luca Sturlese
Creation Date: 02/09/15
Purpose/Change: Changed function name to use approved PowerShell Verbs. Improved help documentation.

Version: 1.2
Author: Luca Sturlese
Creation Date: 02/09/15
Purpose/Change: Added SMTPServer parameter to pass SMTP server as oppposed to having to set it in the function manually.

.LINK
http://9to5IT.com/powershell-logging-v2-easily-create-log-files

.EXAMPLE
Send-Log -SMTPServer "smtp.google.com" -LogPath "C:\Windows\Temp\Test_Script.log" -EmailFrom "admin@9to5IT.com" -EmailTo "admin@9to5IT.com, test@test.com" -EmailSubject "Cool Script"

Sends an email with the contents of the log file as the body of the email. Sends the email from admin@9to5IT.com and sends
the email to admin@9to5IT.com and test@test.com email addresses. The email has the subject of Cool Script. The email is
sent using the smtp.google.com SMTP server.
#>

[CmdletBinding()]

Param (
[Parameter(Mandatory = $true, Position = 0)]
[string]$SMTPServer,
[Parameter(Mandatory = $true, Position = 1)]
[string]$LogPath,
[Parameter(Mandatory = $true, Position = 2)]
[string]$EmailFrom,
[Parameter(Mandatory = $true, Position = 3)]
[string]$EmailTo,
[Parameter(Mandatory = $true, Position = 4)]
[string]$EmailSubject
)

Process
{
Try
{
$sBody = ( Get-Content $LogPath | Out-String )

#Create SMTP object and send email
$oSmtp = New-Object Net.Mail.SmtpClient( $SMTPServer )
$oSmtp.Send( $EmailFrom, $EmailTo, $EmailSubject, $sBody )
Exit 0
}

Catch
{
Exit 1
}
}
}
159 changes: 159 additions & 0 deletions Module/PSLogging/public/Start-Log.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
Function Start-Log
{
<#
.SYNOPSIS
Creates a new log file

.DESCRIPTION
Creates a log file with the path and name specified in the parameters. Checks if log file exists, and if it does deletes it and creates a new one.
Once created, writes initial logging data

.PARAMETER LogPath
Mandatory. Path of where log is to be created. Example: C:\Windows\Temp

.PARAMETER LogName
Mandatory. Name of log file to be created. Example: Test_Script.log

.PARAMETER ScriptVersion
Mandatory. Version of the running script which will be written in the log. Example: 1.5

.PARAMETER ToScreen
Optional. When parameter specified will display the content to screen as well as write to log file. This provides an additional
another option to write content to screen as opposed to using debug mode.

.INPUTS
Parameters above

.OUTPUTS
Log file created

.NOTES

.LINK
http://9to5IT.com/powershell-logging-v2-easily-create-log-files

.EXAMPLE
Start-Log -LogPath "C:\Windows\Temp" -LogName "Test_Script.log" -ScriptVersion "1.5"

Creates a new log file with the file path of C:\Windows\Temp\Test_Script.log. Initialises the log file with
the date and time the log was created (or the calling script started executing) and the calling script's version.
#>

[CmdletBinding()]

Param (
[Parameter(Mandatory = $true, Position = 0)]
[ValidateSet("log", "csv", "json", "xml", "cmtrace")]
[string]$LogFormat,
[Parameter(Mandatory = $true, Position = 1)]
[string]$LogPath,
[Parameter(Mandatory = $true, Position = 2)]
[string]$LogName,
[Parameter(Mandatory = $true, Position = 3)]
[string]$ScriptVersion,
[Parameter(Mandatory = $false, Position = 4)]
[switch]$ToScreen,
[Parameter(Mandatory = $false)]
[switch]$ToEventLog,
[Parameter(Mandatory = $false)]
[switch]$Banner
)

Process
{
$sFullPath = Join-Path -Path $LogPath -ChildPath $LogName

$Msg = [PSCustomObject]@{
TimeStamp = Get-LogDate
MessageType = "INFO"
Message = "Started processing at [$(Get-LogDate)]."
}

if($LogFormat -eq "log")
{
#Check if file exists and delete if it does
If ( (Test-Path -Path $sFullPath) )
{
Remove-Item -Path $sFullPath -Force
}

#Create file and start logging
New-Item -Path $sFullPath -ItemType File
}

#Write Content to Log
If ( $Banner -eq $True )
{
if ($LogFormat -eq "log")
{
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;***************************************************************************************************"
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;Started processing at [$(Get-LogDate)]."
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;***************************************************************************************************"
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;"
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;Running script version [$ScriptVersion]."
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;"
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;***************************************************************************************************"
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;"
}
}
else
{
if ($LogFormat -eq "log")
{
Add-Content -Path $sFullPath -Value "$(Get-LogDate);INFO;Started processing at [$(Get-LogDate)]."
}

if ($LogFormat -eq "xml")
{
$Msg | Export-Clixml $sFullPath
}

if ($LogFormat -eq "json")
{
$Msg | ConvertTo-Json | Out-File $sFullPath
}

if ($LogFormat -eq "csv")
{
$Msg | Export-Csv $sFullPath -UseCulture -NoTypeInformation
}

if ($LogFormat -eq "cmtrace")
{
$toLog = "{0} `$$<{1}><{2}><thread={3}>" -f ($Msg.MessageType + ":" + $Msg.Message), $LogName, $Msg.TimeStamp, $pid
$toLog | Out-File -Append -Encoding UTF8 -FilePath $sFullPath
}
}

#Write Content to the EventViewer
If ( $ToEventLog -eq $True )
{
$LogBaseName = Get-Item $sFullPath | Select-Object -ExpandProperty BaseName
New-EventLog -LogName "Application" -Source $LogBaseName # needs fix if already exists
Write-EventLog -LogName "Application" -Source $LogBaseName -EventID 3001 -EntryType Information -Message "$($LogBaseName) $($ScriptVersion) started" -Category 1
}

#Write to screen for debug mode
Write-Debug "***************************************************************************************************"
Write-Debug "Started processing at [$(Get-LogDate)]."
Write-Debug "***************************************************************************************************"
Write-Debug ""
Write-Debug "Running script version [$ScriptVersion]."
Write-Debug ""
Write-Debug "***************************************************************************************************"
Write-Debug ""

#Write to screen for ToScreen mode
If ( $ToScreen -eq $True )
{
Write-Output "***************************************************************************************************"
Write-Output "Started processing at [$(Get-LogDate)]."
Write-Output "***************************************************************************************************"
Write-Output ""
Write-Output "Running script version [$ScriptVersion]."
Write-Output ""
Write-Output "***************************************************************************************************"
Write-Output ""
}
}
}
Loading