A N

October 5, 2015

Powershell ‘Write-Log’ function

Filed under: Tips — donrsh @ 11:41 am

Powershell function to write log.

<#	
	.NOTES
	===========================================================================
	 Created on:   	2015-10-05 
	 Created by:   	Arash Nabi
	 Email: 	arash@nabi.nu 
	===========================================================================
	.DESCRIPTION
		Write-Log function creates a log file in the $PSScrtipRoot. It's usefull
                to call it in your script and output some message or verbose output. 
	==========================================================================
	.EX
		Write-Log -Message "Email report send"
#> 
function Write-Log
{
	[CmdletBinding()]
	[OutputType([int])]
	Param
	(
		[Parameter(Mandatory = $true,
				   ValueFromPipelineByPropertyName = $False,
				   HelpMessage = "Please enter Message for the log",
				   Position = 0,
				   ValueFromPipeline = $True)]
		[ValidateNotNullorEmpty()]
		$Message
		
		
	)
	try
	{
		$LogFolder = "$PSScriptRoot\log"
		$d = get-date
		$M = $d.Month.ToString()
		$Y = $d.Year.ToString()
		$LogFile = "$LogFolder\$("Script" + "-" + $Y + "-" + $M + ".log")"
		if (!(Test-Path -Path $LogFile))
		{
			$Dir = New-Item -Path $LogFolder -ItemType Directory -Force
			$Log = New-Item -Path "$Dir\$("Script" + "-" + $Y + "-" + $M + ".log")" -ItemTyp File -Force
			
		}
		$DateTime = (get-date -Format "yyyy-MM-dd HH:mm:ss")
		if ($PSBoundParameters['Message'])
		{
			"[$DateTime] " + $Message | out-file $LogFile -ErrorAction SilentlyContinue -append
		}
		
	}
	catch [System.Net.WebException], [System.Exception]
	{
		$errorMessage = $_.Exception.Message
		Write-Warning -Message $errorMessage
	}
	finally
	{
	}
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: