Created
May 9, 2013 13:28
-
-
Save BenNeise/5547433 to your computer and use it in GitHub Desktop.
Writes passed string parameters to a logfile, and to screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Write-Log { | |
<# | |
.Synopsis | |
Writes timestamped output to screen and a logfile. | |
.Description | |
Writes timestamped output to screen and a logfile. | |
.Parameter Message | |
Message to be displayed | |
.Parameter LogFile | |
Path to logfile | |
.Example | |
Writes a simple message to screen and to the logfile | |
Write-Log "Script complete!" C:\Logfile.txt | |
.Example | |
As the message needs to be a single string, it mut be concatenated in parenthesis before being output. | |
Write-Log ("Script " + $strScriptname + " complete") $strLogFile | |
.Notes | |
Ben Neise 05/03/2012 | |
#> | |
Param ( | |
[Parameter(Mandatory=$true, Position=0)] | |
[String]$Message, | |
[Parameter(Mandatory=$true, Position=1)] | |
[string]$LogFile | |
) | |
Write-Host (Get-Date -format HH:mm:ss) "- " -NoNewLine -ForegroundColor DarkGray; Write-Host $Message | |
(Get-Date -format HH:mm:ss) + " - " + $Message | Out-File $LogFile -Append -Encoding ASCII | |
If ($error){ | |
ForEach ($objError in $Error){ | |
"Error!" | Out-File $LogFile -Append -Encoding ASCII | |
"Reason: " + $objError.CategoryInfo.Reason | Out-File $LogFile -Append -Encoding ASCII | |
"Command: " + $objError.InvocationInfo.MyCommand | Out-File $LogFile -Append -Encoding ASCII | |
"Line-number: " + $objError.InvocationInfo.ScriptLineNumber | Out-File $LogFile -Append -Encoding ASCII | |
"Character: " + $objError.InvocationInfo.OffsetInLine | Out-File $LogFile -Append -Encoding ASCII | |
"Exception: " + $objError.Exception | Out-File $LogFile -Append -Encoding ASCII | |
} | |
$error.Clear() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment