Logging function for Powershell v2. Needed to address current Powershell logging limitations. See a discussion about said limitations here: http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/#comment-2899 .

Logs to file and prints messages to the console and optionally logs to the system event logs. Note – admin rights are required when specifying the EventLogName parameter because the SourceExists method requires admin rights because it checks rights on the Security log. This limitation seems silly to me and if you think so as well get Microsoft to fix it by voting here: https://connect.microsoft.com/VisualStudio/feedback/details/293617/eventlog-sourceexists-always-fails-for-non-administrators#tabs .

Feel free to add features as necessary.

Will Steele: I suggest adding the Encoding switch to the Out-File calls (lines 58 and 60) to ensure the file didn’t clog up the output file. I also added a parameter, $LogEncoding, (lines 37-38) to allow for Encoding selection. The default is ASCII.

It might be worth adding a few lines that allow for folder creation using the input file parameter. For instance, if the folder path referenced in the -Path parameter is invalid it doesn’t handle this. Example:

- Changed formatting of $msg so that only $message is indented instead of entire line (looks cleaner)

- suppressed output when creating path/file

#>

}

Submit a correction or amendment below (click here to make a fresh posting)After submitting an amendment, you'll be able to view the differences between the old and new posts easily.
Syntax highlighting:

Your Name
Remember me

Post Title (optional)

How long should your post be retained?a daya monthforever

Description (No HTML please, light textile accepted for markup)

To highlight particular lines, prefix each line with @@

PoshCode is a repository of PowerShell scripts
that are free for public use. As you find things useful, we only ask that you
give back to our community.