My Solution to T-SQL Tuesday #31 – Logging


TT150x150

Here is my solution to Logging. Sir Aaron Nelson is a PowerSheller as me, he is accepting Posh Solutions as well. Sorriso

   1: #######################

   2: <#

   3: .SYNOPSIS

   4: Log Messages into a Log File.

   5: .DESCRIPTION

   6: The Write-Log function was writed to help looging messages.

   7: .INPUTS

   8: None

   9:     You cannot pipe objects to Write-Log 

  10: .OUTPUTS

  11: None

  12: .PARAMETERS

  13: $MSG = Message to Log . Mandatory Parameter

  14: $Date = Current Date. Used to log with the Message. If it not informed, the current date is used in format "yyyy/MM/dd hh:mm:ss

  15: $LogAt = Path to write the log file. if it not informed $pshome is used

  16: $FileName = The Name of the File. If it not informed, "LogFile" is used

  17: $NewFIle = Switch parameter. If informed, a new file is created. If the name of the file is the same , will be overwritten.

  18: .EXAMPLE

  19: Write-Log -FileName LogCommand -LogAt c:\Temp1 -Msg "The command is invalid" -Newfile

  20: This command create a log file called LogCommand at c:\temp1 path with the message "The command is invalid". The newfile switch is used so if there is some file with the same name will be overwritten .

  21: .EXAMPLE

  22: Write-Log -FileName LogCommand -LogAt c:\Temp1 -Msg "The command is invalid"

  23: This command appends to a log file called LogCommand at c:\temp1 path with the message "The command is invalid". The newfile switch not used so if there is some file with the same name will be created otherwise will be appended.

  24: .EXAMPLE

  25: Write-Log -FileName LogCommand  -Msg "The command is invalid"

  26: This command appends to a log file called LogCommand at $pshome pathn (the logAT was not informed) with the message "The command is invalid". The newfile switch not used so if there is some file with the same name will be created otherwise will be appended.

  27:  

  28: #>

  29:  

  30: function Write-Log

  31: {

  32:     param    (    

  33:                 [parameter(Position=1,Mandatory=$true)]

  34:                 [ValidateNotNullOrEmpty()]

  35:                 [string] $Msg,

  36:                 [parameter(Position=2)]

  37:                 $date = (get-date -format "yyyy/MM/dd hh:mm:ss"),

  38:                 [parameter(Position=3)]

  39:                 [string] $LogAt = $pshome,

  40:                 [parameter(Position=4)]

  41:                 [string] $FileName = "LogFile",

  42:                 [parameter(Position=5)]

  43:                 [switch] $Newfile

  44:             )

  45:     

  46:     "$($date) - $($Msg)" | Out-File -FilePath (Join-Path $LogAt "$($Filename).log") -Append:(!$newfile) -Force

  47: }

  48:  

Usage :

Parameters :

$MSG = Message to Log . Mandatory Parameter
$Date = Current Date. Used to log with the Message. If it not informed, the current date is used in format “yyyy/MM/dd hh:mm:ss
$LogAt = Path to write the log file. if it not informed $pshome is used
$FileName = The Name of the File. If it not informed, “LogFile” is used
$NewFIle = Switch parameter. If informed, a new file is created. If the name of the file is the same , will be overwritten.

Examples :

   1: Write-Log -FileName LogCommand -LogAt c:\Temp1 -Msg "The command is invalid" -Newfile

This command create a log file called LogCommand at c:\temp1 path with the message “The command is invalid”. The newfile switch is used so if there is some file with the same name will be overwritten .

   1: Write-Log -FileName LogCommand -LogAt c:\Temp1 -Msg "The command is invalid"

This command appends to a log file called LogCommand at c:\temp1 path with the message “The command is invalid”. The newfile switch not used so if there is some file with the same name will be created otherwise will be appended.

   1: Write-Log -FileName LogCommand  -Msg "The command is invalid"

This command appends to a log file called LogCommand at $pshome pathn (the logAT was not informed) with the message “The command is invalid”. The newfile switch not used so if there is some file with the same name will be created otherwise will be appended.

The log file is  :

image

For more information type get-help write-log –full

 

#PowerShellLifeStyle

About Laerte Junior

Laerte Junior Laerte Junior is a SQL Server specialist and an active member of WW SQL Server and the Windows PowerShell community. He also is a huge Star Wars fan (yes, he has the Darth Vader´s Helmet with the voice changer). He has a passion for DC comics and living the simple life. "May The Force be with all of us"
This entry was posted in Powershell, SQL SERVER EM GERAL. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s