Functions/Msg.ps1

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
function Msg {
    <#
        .Synopsis
           Main output function.
        .DESCRIPTION
           Writes messages to file log and/or eventvwr std-out/std-err OR host.
        .PARAMETER Message
           String to show and/or log to file or eventlog.
        .PARAMETER Type
           Message type, primarilly used for eventlog writing.
        .PARAMETER useEventLog
           Write message to windows EventLog (NOTE:This needs to be done as Administrator for first run)
        .PARAMETER EventLogName
            The name of eventlog to write to
        .PARAMETER EventId
            Event ID number for EventLog
        .PARAMETER useFileLog
            Write message to Log file (if omitted the message will be sent to std-out)
        .PARAMETER logFile
            Name of file to write message to.
        .PARAMETER logFilePath
            Target folder for log file. If omitted the script path is used.
     
        .EXAMPLE
           Msg "The secret is OUT"
     
           Writes "The secret is OUT" to std-out as "INFORMATION" message
        .EXAMPLE
           Msg "This was NO good" "ERROR"
     
           Writes the message to std-error
        .EXAMPLE
           Msg "This was NO good" "ERROR" -useEventLog
     
           Writes message to console (Write-Host) and to the windows Eventlog.
        .EXAMPLE
           Msg "Write this to file" -useFileLog
     
           Writes message to console (Write-Host) and to the standard log file name.
        #>

    [CmdletBinding()]
    Param(
        [parameter(Position = 0, mandatory = $true, ValueFromPipeline=$true)]
        $Message,
        [parameter(Position = 1, mandatory = $false)]
        [string]$Type = "INFORMATION",        
        [switch]$useEventLog,
        [string]$EventLogName = "Application",
        [int]$EventId = 4695,     
        [switch]$useFileLog,
        [string]$logFile,
        [string]$logFilePath    
    )
        
    $scriptName = Split-Path -Leaf $MyInvocation.PSCommandPath  
    $logstring = (Get-Date).ToString() + ";" + $Message
        
    if ($useEventLog.IsPresent) {
        Write-Verbose "Msg:Use eventlog"
        #We will get an error if not running as administrator
        try {
            if (![system.diagnostics.eventlog]::SourceExists($scriptName)) {
                [system.diagnostics.EventLog]::CreateEventSource($scriptName, $EventLogName)
            }
            Write-EventLog -LogName $EventLogName -Source $scriptName -EntryType $Type -Message $Message -EventId $EventId -Category 0
        }
        catch {
            Write-Error "ERROR;Run as ADMINISTRATOR;$($PSItem.Exception.Message)"
        }        
    }
    
    if ($useFileLog.IsPresent) {
        Write-Verbose "Msg:Use logfile"
                
        if ([string]::IsNullOrEmpty($logFilePath)) {
            $logFilePath = $MyInvocation.PSCommandPath | Split-Path -Parent
        }
                            
        if ([string]::IsNullOrEmpty($logFile)) {
            $logfile = $logFilePath + "\" + ($MyInvocation.PSCommandPath | Split-Path -Leaf) + "." + (Get-Date -Format 'yyyy-MM') + ".log"
        }
        elseif (!$logFile.Contains("\")) {
            $logfile = $logFilePath + "\" + $logfile
        }                        
    
        #Write to log file
        $stream = [System.IO.File]::AppendText($logFile)
        $stream.WriteLine($logstring)
        $stream.close()
    
    }
    
    if ($useFileLog.IsPresent -or $useEventLog.IsPresent) {
        #Write to console
        Write-Host "$Type;$logstring"
    }
    else {

        Write-Verbose "Msg:Use std-Out/std-Err"
        if ($Type -match "Err") {
            Write-Error "$Type;$logstring"
        }
        else {
            Write-Output "$Type;$logstring"
        }
    }        
}