├── Example └── log.log ├── LICENSE ├── Public └── Write-LogEntry.ps1 ├── README.md ├── WriteLogEntry.psd1 ├── WriteLogEntry.psm1 └── en-US └── about_WriteLogEntry.help.txt /Example/log.log: -------------------------------------------------------------------------------- 1 | 20170413T033149 [INFO]: This is an informational log event 2 | 20170413T033149 [DEBUG]: This is an debugging log event 3 | 20170413T033149 [ERROR]: This is an error log event 4 | 20170413T033149 [ERROR]: Error calling Do-SomethingElse 5 | 20170413T033149 [ERROR]: The term 'Do-SomethingElse' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. (CommandNotFoundException: :6 char:5) 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Josh Rickard 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Public/Write-LogEntry.ps1: -------------------------------------------------------------------------------- 1 | <# 2 | .Synopsis 3 | Used to create and output information from functions 4 | .DESCRIPTION 5 | This function will write to a log file. You can specify if the log type is: 6 | Informational (Info) 7 | Debugging (Debugging) 8 | Error (Error) 9 | This function will by default create a log file in the parent folder of the calling scope, but 10 | you can specify a seperate log location if you choose. 11 | $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(‘.\’)\log.log 12 | 13 | The default parameter set for this function is the Info logging, but there are 2 other sets 14 | Debug 15 | Error 16 | 17 | Example output that this function will place into a log file is 18 | Function call 19 | Write-LogEntry -Info 'This is an informational log event' 20 | Output 21 | 20170401T055438 [INFO]: This is an informational log event 22 | 23 | Function call 24 | Write-LogEntry -Debugging 'This is an debugging log event' 25 | Output 26 | 20170401T055440 [DEBUG]: This is an debugging log event 27 | 28 | Function call 29 | Write-LogEntry -Error 'This is an error log event' 30 | Output 31 | 20170401T055442 [ERROR]: This is an error log event 32 | 33 | Function call 34 | try { 35 | do-something 36 | } catch { 37 | Write-LogEntry -Error 'This is an error log event' -ErrorRecord $Error[0] 38 | } 39 | Output 40 | 20170401T055444 [ERROR]: This is an error log event 41 | 20170401T055444 [ERROR]: The term 'do-something' is not recognized as the name of a cmdlet, ` 42 | function, script file, or operable program. Check the spelling of the name, ` 43 | or if a path was included, verify that the path is correct and try again. ` 44 | (CommandNotFoundException: :1 char:7) 45 | .EXAMPLE 46 | Write-LogEntry -Info 'This is an informational log event' 47 | 48 | Output 49 | 20170401T055438 [INFO]: This is an informational log event 50 | .EXAMPLE 51 | Write-LogEntry -Debugging 'This is an debugging log event' 52 | 53 | Output 54 | 20170401T055440 [DEBUG]: This is an debugging log event 55 | .EXAMPLE 56 | Write-LogEntry -Error 'This is an error log event' 57 | 58 | Output 59 | 20170401T055442 [ERROR]: This is an error log event 60 | .EXAMPLE 61 | try { 62 | do-something 63 | } catch { 64 | Write-LogEntry -Error 'This is an error log event' -ErrorRecord $Error[0] 65 | } 66 | 67 | Output 68 | 20170401T055444 [ERROR]: This is an error log event 69 | 20170401T055444 [ERROR]: The term 'do-something' is not recognized as the name of a cmdlet, ` 70 | function, script file, or operable program. Check the spelling of the name, ` 71 | or if a path was included, verify that the path is correct and try again. ` 72 | (CommandNotFoundException: :1 char:7) 73 | 74 | .INPUTS 75 | System.String 76 | System.Management.Automation.ErrorRecord 77 | .NOTES 78 | Name: Write-LogEntry 79 | Created by: Josh Rickard 80 | Created Date: 11/24/2016 81 | .FUNCTIONALITY 82 | Write-LogEntry is a PowerShell helper function that will accept or create a log file and 83 | add strings based on severity, as well as parse $error[0] records for easy interpretation 84 | and readability. 85 | #> 86 | function Write-LogEntry 87 | { 88 | [CmdletBinding(DefaultParameterSetName = 'Info', 89 | SupportsShouldProcess=$true, 90 | PositionalBinding=$false, 91 | HelpUri = 'https://github.com/MSAdministrator/WriteLogEntry', 92 | ConfirmImpact='Medium')] 93 | [OutputType()] 94 | Param 95 | ( 96 | # Information type of log entry 97 | [Parameter(Mandatory=$true, 98 | ValueFromPipelineByPropertyName=$true, 99 | Position=0, 100 | ParameterSetName = 'Info')] 101 | [ValidateNotNull()] 102 | [ValidateNotNullOrEmpty()] 103 | [Alias("information")] 104 | [System.String]$Info, 105 | 106 | # Debug type of log entry 107 | [Parameter(Mandatory=$true, 108 | ValueFromPipelineByPropertyName=$true, 109 | Position=0, 110 | ParameterSetName = 'Debug')] 111 | [ValidateNotNull()] 112 | [ValidateNotNullOrEmpty()] 113 | [System.String]$Debugging, 114 | 115 | # Error type of log entry 116 | [Parameter(Mandatory=$true, 117 | ValueFromPipeline=$true, 118 | Position=0, 119 | ParameterSetName = 'Error')] 120 | [ValidateNotNull()] 121 | [ValidateNotNullOrEmpty()] 122 | [System.String]$Error, 123 | 124 | # The error record containing an exception to log 125 | [Parameter(Mandatory=$false, 126 | ValueFromPipeline=$true, 127 | ValueFromPipelineByPropertyName=$true, 128 | Position=1, 129 | ParameterSetName = 'Error')] 130 | [ValidateNotNull()] 131 | [ValidateNotNullOrEmpty()] 132 | [Alias("record")] 133 | [System.Management.Automation.ErrorRecord]$ErrorRecord, 134 | 135 | # Logfile location 136 | [Parameter(Mandatory=$false, 137 | ValueFromPipelineByPropertyName=$true, 138 | Position=2)] 139 | [Alias("file", "location")] 140 | [System.String]$LogFile = "$($ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath('.\'))" + "\log.log" 141 | ) 142 | 143 | if (!(Test-Path -Path $LogFile)) 144 | { 145 | try 146 | { 147 | New-Item -Path $LogFile -ItemType File -Force | Out-Null 148 | } 149 | catch 150 | { 151 | Write-Error -Message 'Error creating log file' 152 | break 153 | } 154 | } 155 | 156 | # Creating a new mutex object 157 | $mutex = New-Object -TypeName 'Threading.Mutex' -ArgumentList $false, 'MyInterprocMutex' 158 | 159 | switch ($PSBoundParameters.Keys) 160 | { 161 | 'Error' 162 | { 163 | $mutex.waitone() | Out-Null 164 | 165 | Add-Content -Path $LogFile -Value "$((Get-Date).ToString('yyyyMMddThhmmss')) [ERROR]: $Error" 166 | 167 | if ($PSBoundParameters.ContainsKey('ErrorRecord')) 168 | { 169 | $Message = '{0} ({1}: {2}:{3} char:{4})' -f $ErrorRecord.Exception.Message, 170 | $ErrorRecord.FullyQualifiedErrorId, 171 | $ErrorRecord.InvocationInfo.ScriptName, 172 | $ErrorRecord.InvocationInfo.ScriptLineNumber, 173 | $ErrorRecord.InvocationInfo.OffsetInLine 174 | 175 | Add-Content -Path $LogFile -Value "$((Get-Date).ToString('yyyyMMddThhmmss')) [ERROR]: $Message" 176 | } 177 | 178 | $mutex.ReleaseMutex() | Out-Null 179 | } 180 | 'Info' 181 | { 182 | $mutex.waitone() | Out-Null 183 | 184 | Add-Content -Path $LogFile -Value "$((Get-Date).ToString('yyyyMMddThhmmss')) [INFO]: $Info" 185 | 186 | $mutex.ReleaseMutex() | Out-Null 187 | } 188 | 'Debugging' 189 | { 190 | Write-Debug -Message "$Debugging" 191 | 192 | $mutex.waitone() | Out-Null 193 | 194 | Add-Content -Path $LogFile -Value "$((Get-Date).ToString('yyyyMMddThhmmss')) [DEBUG]: $Debugging" 195 | 196 | $mutex.ReleaseMutex() | Out-Null 197 | } 198 | }# End of switch statement 199 | } # end of Write-LogEntry function 200 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MSAdministrator/WriteLogEntry/108971f4e320fab2e85518170b3a22a4fe6ae4a9/README.md -------------------------------------------------------------------------------- /WriteLogEntry.psd1: -------------------------------------------------------------------------------- 1 | # 2 | # Module manifest for module 'WriteLogEntry' 3 | # 4 | # Generated by: Josh Rickard 5 | # 6 | # Generated on: 11/24/2016 7 | # 8 | 9 | @{ 10 | 11 | # Script module or binary module file associated with this manifest. 12 | RootModule = 'WriteLogEntry.psm1' 13 | 14 | # Version number of this module. 15 | ModuleVersion = '1.0' 16 | 17 | # Supported PSEditions 18 | # CompatiblePSEditions = @() 19 | 20 | # ID used to uniquely identify this module 21 | GUID = 'f61a9862-760d-40ad-b349-7259b612217f' 22 | 23 | # Author of this module 24 | Author = 'Josh Rickard' 25 | 26 | # Company or vendor of this module 27 | CompanyName = '' 28 | 29 | # Copyright statement for this module 30 | Copyright = '(c) 2016 MSAdministrator. All rights reserved.' 31 | 32 | # Description of the functionality provided by this module 33 | Description = 'A PowerShell helper function to create and write information to a log file' 34 | 35 | # Minimum version of the Windows PowerShell engine required by this module 36 | # PowerShellVersion = '' 37 | 38 | # Name of the Windows PowerShell host required by this module 39 | # PowerShellHostName = '' 40 | 41 | # Minimum version of the Windows PowerShell host required by this module 42 | # PowerShellHostVersion = '' 43 | 44 | # Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 45 | # DotNetFrameworkVersion = '' 46 | 47 | # Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only. 48 | # CLRVersion = '' 49 | 50 | # Processor architecture (None, X86, Amd64) required by this module 51 | # ProcessorArchitecture = '' 52 | 53 | # Modules that must be imported into the global environment prior to importing this module 54 | # RequiredModules = @() 55 | 56 | # Assemblies that must be loaded prior to importing this module 57 | # RequiredAssemblies = @() 58 | 59 | # Script files (.ps1) that are run in the caller's environment prior to importing this module. 60 | # ScriptsToProcess = @() 61 | 62 | # Type files (.ps1xml) to be loaded when importing this module 63 | # TypesToProcess = @() 64 | 65 | # Format files (.ps1xml) to be loaded when importing this module 66 | # FormatsToProcess = @() 67 | 68 | # Modules to import as nested modules of the module specified in RootModule/ModuleToProcess 69 | # NestedModules = @() 70 | 71 | # Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export. 72 | FunctionsToExport = 'Write-LogEntry' 73 | 74 | # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. 75 | CmdletsToExport = @() 76 | 77 | # Variables to export from this module 78 | VariablesToExport = '*' 79 | 80 | # Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export. 81 | AliasesToExport = @() 82 | 83 | # DSC resources to export from this module 84 | # DscResourcesToExport = @() 85 | 86 | # List of all modules packaged with this module 87 | # ModuleList = @() 88 | 89 | # List of all files packaged with this module 90 | # FileList = @() 91 | 92 | # Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell. 93 | PrivateData = @{ 94 | 95 | PSData = @{ 96 | 97 | # Tags applied to this module. These help with module discovery in online galleries. 98 | # Tags = @() 99 | 100 | # A URL to the license for this module. 101 | # LicenseUri = '' 102 | 103 | # A URL to the main website for this project. 104 | ProjectUri = 'https://github.com/MSAdministrator/WriteLogEntry' 105 | 106 | # A URL to an icon representing this module. 107 | # IconUri = '' 108 | 109 | # ReleaseNotes of this module 110 | # ReleaseNotes = '' 111 | 112 | } # End of PSData hashtable 113 | 114 | } # End of PrivateData hashtable 115 | 116 | # HelpInfo URI of this module 117 | # HelpInfoURI = '' 118 | 119 | # Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix. 120 | # DefaultCommandPrefix = '' 121 | 122 | } 123 | 124 | -------------------------------------------------------------------------------- /WriteLogEntry.psm1: -------------------------------------------------------------------------------- 1 | #requires -Version 2 2 | #Get public and private function definition files. 3 | $Public = @( Get-ChildItem -Path $PSScriptRoot\Public\*.ps1 -Recurse -ErrorAction SilentlyContinue ) 4 | $Private = @( Get-ChildItem -Path $PSScriptRoot\Private\*.ps1 -Recurse -ErrorAction SilentlyContinue ) 5 | 6 | #Dot source the files 7 | Foreach($import in @($Public + $Private)) 8 | { 9 | Try 10 | { 11 | . $import.fullname 12 | } 13 | Catch 14 | { 15 | Write-Error -Message "Failed to import function $($import.fullname): $_" 16 | } 17 | } 18 | 19 | Export-ModuleMember -Function $Public.Basename -------------------------------------------------------------------------------- /en-US/about_WriteLogEntry.help.txt: -------------------------------------------------------------------------------- 1 | PSTOPIC 2 | about_WriteLogEntry 3 | 4 | SHORT DESCRIPTION 5 | A PowerShell helper function to create and write information to a log file 6 | 7 | LONG DESCRIPTION 8 | This function will write to a log file. You can specify if the log type is: Informational (Info), Debugging (Debugging), and Error (Error). This function will by default create a log file in the parent folder of the calling scope, but you can specify a seperate log location if you choose. 9 | 10 | DETAILED DESCRIPTION 11 | This function will write to a log file. You can specify if the log type is: Informational (Info), Debugging (Debugging), and Error (Error). This function will by default create a log file in the parent folder of the calling scope, but you can specify a seperate log location if you choose. --------------------------------------------------------------------------------