Public/Write-Wardlog.ps1
function Write-WardLog { [CmdletBinding()] Param ( # The place you would like to output your log. [Parameter(Mandatory = $true)] [ValidatePattern( '\.txt$' )] [System.IO.FileInfo] $LogPath, # The message you would like to leave in your log. [Parameter(Mandatory = $true)] $LogMessage ) Begin { if ($LogPath -and $LogPath.Count -eq 1) { # Checking to see if log file exists, attempts to create it if not. if (!(Test-Path -Path $LogPath -PathType Leaf)) { Write-Verbose "Log file doesn't exist at - $LogPath" try { Write-Verbose "Attempting to create log file at - $LogPath" New-Item -Path $LogPath -ErrorAction Stop Write-Verbose "Successfully created log file at - $LogPath" } catch { Write-Error "Unable to create log file at - $LogPath" Break } } else { Write-Verbose "Log file already exists at - $LogPath" Write-Verbose "Whoopdefuckin' doooo" } } else { Write-Error "There is not a single log path, IDIOT." Break } } Process { if ($LogMessage) { Write-Verbose "We have log message." foreach ($Record in $LogMessage) { try { Write-Verbose "Log message - $Record" Write-Verbose "Attempting to write log." Out-File -FilePath $LogPath -InputObject ("$(Get-Date -Format "MM/dd/yyyy [HH:mm:ss]") - " + "$Record") -Append -ErrorAction Stop Write-Verbose "Successfully wrote log." } catch { Write-Error "Unable to write message to log file." Break } } } else { Write-Warning "Log message is null." Write-Warning "Maybe you should not run this module if you aren't even gonna show up with a message, sheesh." } } End { Write-Verbose "End processing for Write-StreamLog." } } |