+# This function is originally by wasserja at https://gallery.technet.microsoft.com/scriptcenter/Write-Log-PowerShell-999c32d0\r
+<# \r
+.Synopsis \r
+ Write-Log writes a message to a specified log file with the current time stamp. \r
+.DESCRIPTION \r
+ The Write-Log function is designed to add logging capability to other scripts. \r
+ In addition to writing output and/or verbose you can write to a log file for \r
+ later debugging. \r
+.NOTES \r
+ Created by: Jason Wasser @wasserja \r
+ Modified: 11/24/2015 09:30:19 AM \r
+ \r
+ Changelog: \r
+ * Code simplification and clarification - thanks to @juneb_get_help \r
+ * Added documentation. \r
+ * Renamed LogPath parameter to Path to keep it standard - thanks to @JeffHicks \r
+ * Revised the Force switch to work as it should - thanks to @JeffHicks \r
+ \r
+ To Do: \r
+ * Add error handling if trying to create a log file in a inaccessible location. \r
+ * Add ability to write $Message to $Verbose or $Error pipelines to eliminate \r
+ duplicates. \r
+.PARAMETER Message \r
+ Message is the content that you wish to add to the log file. \r
+.PARAMETER Path \r
+ The path to the log file to which you would like to write. By default the function will \r
+ create the path and file if it does not exist. \r
+.PARAMETER Level \r
+ Specify the criticality of the log information being written to the log (i.e. Error, Warning, Informational) \r
+.PARAMETER NoClobber \r
+ Use NoClobber if you do not wish to overwrite an existing file. \r
+.EXAMPLE \r
+ Write-Log -Message 'Log message' \r
+ Writes the message to c:\Logs\PowerShellLog.log. \r
+.EXAMPLE \r
+ Write-Log -Message 'Restarting Server.' -Path c:\Logs\Scriptoutput.log \r
+ Writes the content to the specified log file and creates the path and file specified. \r
+.EXAMPLE \r
+ Write-Log -Message 'Folder does not exist.' -Path c:\Logs\Script.log -Level Error \r
+ Writes the message to the specified log file as an error message, and writes the message to the error pipeline. \r
+.LINK \r
+ https://gallery.technet.microsoft.com/scriptcenter/Write-Log-PowerShell-999c32d0 \r
+#>\r
+function Write-Log \r
+{ \r
+ [CmdletBinding()] \r
+ Param \r
+ ( \r
+ [Parameter(Mandatory=$true, \r
+ ValueFromPipelineByPropertyName=$true)] \r
+ [ValidateNotNullOrEmpty()] \r
+ [Alias("LogContent")] \r
+ [string]$Message, \r
+ \r
+ [Parameter(Mandatory=$false)] \r
+ [Alias('LogPath')] \r
+ [string]$Path='C:\Logs\PowerShellLog.log', \r
+ \r
+ [Parameter(Mandatory=$false)] \r
+ [ValidateSet("Error","Warn","Info")] \r
+ [string]$Level="Info", \r
+ \r
+ [Parameter(Mandatory=$false)] \r
+ [switch]$NoClobber \r
+ ) \r
+ \r
+ Begin \r
+ { \r
+ # Set VerbosePreference to Continue so that verbose messages are displayed. \r
+ $VerbosePreference = 'Continue' \r
+ } \r
+ Process \r
+ { \r
+ \r
+ # If the file already exists and NoClobber was specified, do not write to the log. \r
+ if ((Test-Path $Path) -AND $NoClobber) { \r
+ Write-Error "Log file $Path already exists, and you specified NoClobber. Either delete the file or specify a different name." \r
+ Return \r
+ } \r
+ \r
+ # If attempting to write to a log file in a folder/path that doesn't exist create the file including the path. \r
+ elseif (!(Test-Path $Path)) { \r
+ Write-Verbose "Creating $Path." \r
+ $NewLogFile = New-Item $Path -Force -ItemType File \r
+ } \r
+ \r
+ else { \r
+ # Nothing to see here yet. \r
+ } \r
+ \r
+ # Format Date for our Log File \r
+ $FormattedDate = Get-Date -Format "yyyy-MM-dd HH:mm:ss" \r
+ \r
+ # Write message to error, warning, or verbose pipeline and specify $LevelText \r
+ switch ($Level) { \r
+ 'Error' { \r
+ Write-Error $Message \r
+ $LevelText = 'ERROR:' \r
+ } \r
+ 'Warn' { \r
+ Write-Warning $Message \r
+ $LevelText = 'WARNING:' \r
+ } \r
+ 'Info' { \r
+ Write-Verbose $Message \r
+ $LevelText = 'INFO:' \r
+ } \r
+ } \r
+ \r
+ # Write log entry to $Path \r
+ "$FormattedDate $LevelText $Message" | Out-File -FilePath $Path -Append \r
+ } \r
+ End \r
+ { \r
+ } \r
+}\r