MessageHandling.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
Function Write-Message{
    <#
    .SYNOPSIS
    Handle message events in a script
    .DESCRIPTION
    Write log files, event logs, host messages and perform a specfic task based on the type of message
    .PARAMETER Message
    The message that is to be written
    .PARAMETER MessageType
    The type of message, Valid types Error Warning Information and SuccessAudit
    .PARAMETER WriteLog
    Path to log file, if not specified no messages will be written to a log
    Writes the log in a csv format
    .PARAMETER WriteEvent
    Writes the message to the application event log
    .PARAMETER Source
    Specifies where the event was created from, if not specified will default to MosaicMK Message Handling
    .PARAMETER WriteToHost
    Writes the message to the PowerShell host
    .PARAMETER CloseOnError
    When an error message type is written the script will exit
    .PARAMETER CloseOnSuccess
    When a SuccessAudit message type is written the script will exit
    .PARAMETER EventID
    The event number that will be written to the log file, Event log and used as an exit code when
    exit CloseOnError or CloseOnSuccess is used.
 
    if not specified the script will default the Event ID to the following:
    Error = 1
    Warning = 2
    Information = 100
    SuccessAudit = 0
    .EXAMPLE
    Write-Message -Message "Starting Proccess" -MessageType Information -WriteLog C:\Logs\proccess.log -WriteEvent -WriteToHost
    Writes the message 'Starting Proccess' to the proccess.log file, The Event log and to the PowerShell host window as a Information message type
    .EXAMPLE
    Write-Message -Message "Starting Proccess" -MessageType Warning -WriteToHost
    Writes the message 'Starting Proccess' to the PowerShell host window as a warning message type
    .EXAMPLE
    Write-Message -Message "Could not stop proccess" -MessageType Error -WriteEvent -CloseOnError -EventID 500
    Writes the message to the event log then closes exits the script with event id 500
    .NOTES
    A function for each message type is present so addition tasks can be competed when a message of a specific type is written.
 
    Copyright (C) MosaicMK Software LLC - All Rights Reserved
    Unauthorized copying of this application via any medium is strictly prohibited Proprietary and confidential
    Written by MosaicMK Software LLC (contact@mosaicmk.com)
 
    By using this software you agree to the following:
    Agreement Permission is hereby granted, free of charge, to any person or organization obtaining a copy of this software and associated documentation files (the "Software"),
    to deal in the Software and the rights to use and distribute the software so long a no licensing and or documentation files are remove, revised or modified
    the Software is furnished to do so, subject to the following conditions:
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
 
    Contact: Contact@MosaicMK.com
    Version 1.1.0
    .LINK
    https://www.mosaicmk.com
    #>


    PARAM(
        [Parameter(Mandatory=$true)]
        [String]$Message,
        [Parameter(Mandatory=$true)]
        [ValidateSet('Error','Warning','Information','SuccessAudit')]
        [string]$MessageType,
        [string]$WriteLog,
        [switch]$WriteEvent,
        [string]$Source,
        [switch]$WriteToHost,
        [switch]$CloseOnError,
        [switch]$CloseOnSuccess,
        [int]$EventID
    )
    $Time = Get-Date
    #If no Event ID was set, sets the Event ID based on the message type
    IF (!($EventID)){
        switch ($MessageType) {
            Error {$EventID = 1}
            Warning {$EventID = 2}
            Information {$EventID = 100}
            SuccessAudit {$EventID = 0}
        }
    }
    #if no source was specified uses "MosaicMK Message Handling" as the source
    IF (!($Source)){$Source = "MosaicMK Message Handling"}
    #Function to run then an Error Message Type is written
    function Invoke-error {
        Write-Error "$Message" -ErrorAction SilentlyContinue
        IF ($WriteToHost){$Host.UI.WriteErrorLine("$Message")}
        IF ($CloseOnError){Exit $EventID}
    }
    #Function to run then a Warning Message Type is written
    function Invoke-Warning {
        IF ($WriteToHost){$Host.UI.WriteWarningLine("$Message")}
    }
    #Function to run then an Information Message Type is written
    function Invoke-Information {
        IF ($WriteToHost){Write-Host "$Message"}
    }
    #Function to run then a SuccessAudit Message Type is written
    Function Invoke-SuccessAudit {
        IF ($WriteToHost){Write-Host "$Message"}
        IF ($CloseOnSuccess){Exit $EventID}
    }
    #if a log file is specified writes the message to the log file
    IF ($WriteLog){
        IF (!(Test-Path $WriteLog)){New-Item $WriteLog -ItemType File -Force | out-null;Add-Content -Value "Time, Message Type, Event ID, Message" -Path $WriteLog}
        Add-Content -Value "$Time, $MessageType, $EventID, $Message" -Path $WriteLog
    }
    #if the writeEvent switch was used, write the message to the application event log
    IF ($WriteEvent){
        New-EventLog -LogName Application -Source $Source -ErrorAction SilentlyContinue
        Write-EventLog -LogName Application -EventId $EventID -Source $Source -EntryType $MessageType -Message $Message -InformationAction SilentlyContinue
    }
    #foreach message type run the corresponding function
    switch ($MessageType) {
        Error {Invoke-error}
        Warning {Invoke-Warning}
        Information {Invoke-Information}
        SuccessAudit {Invoke-SuccessAudit}
    }
}