EZLog.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
#Requires -Version 5.0
$ErrorActionPreference = 'Stop'
Set-PSDebug -Strict

Add-Type -TypeDefinition @"
    public enum MsgCategory
    {
       INF = 0,
       WAR = 1,
       ERR = 2
    }

    public enum Interval
    {
       Daily = 0,
       Monthly = 2,
       Yearly = 3
    }
"@


Function Write-EZLog
{
<#
.SYNOPSIS
   Utility cmdlet to write logs to disk in an easy and pragmatic way.

.DESCRIPTION
   This cmdlet allows to write timestamped and nice formatted logs with a header and footer.
   It also allows to specify if the log entry being written is an info, a warning or an error.
   
   The header contains the following information :
       - full script path of the caller,
       - account under the script was run,
       - computer name of the machine whose executed the script,
       - and more...
   The footer contains the elapsed time from the beginning of the log session.

.PARAMETER Header
    Mandatory switch to start a log session.

.PARAMETER Category
    Category can be one of the following value : INF, WAR, ERR

.PARAMETER Message
    Specify the content of the data to log.

.PARAMETER Footer
    Mandatory switch to end a log session. If you omit to close your log session, you won't know how much time
    your script was running.

.EXAMPLE
   First thing to do is write a header and define a log file where the data will be written.

   Write-EZLog -Header -LogFile C:\logs\mylogfile.log
   
   Next, anywhere in your script when you need to write a log, do one of the folowing command:

   Write-EZLog -Category INF -Message 'This is an info to be written in the log file'
   Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file'
   Write-EZLog -Category ERR -Message 'This is an error to be written in the log file'

   Finaly, to close your logfile you need to write a footer, just do that:

   Write-EZLog -Footer

.EXAMPLE
   If you want to see the logs in the PowerShell console whereas they are still written to disk,
   you can specify the -ToScreen switch.
   Info entries will be written in cyan color, Yellow for warnings, and Red for the errors.

   Write-EZLog -Category WAR -Message 'This is a warning to be written in the log file' -ToScreen

.NOTES
   AUTHOR: Arnaud PETITJEAN - arnaud@powershell-scripting.com
   LASTEDIT: 2018/03/30

#>

    [cmdletBinding(DefaultParameterSetName="set2", SupportsShouldProcess=$False)]
    PARAM (
        [parameter(Mandatory=$true, ParameterSetName="set2", ValueFromPipeline=$false, position=0)]
        [MsgCategory]$Category,
       
        [parameter(Mandatory=$true, ParameterSetName="set2", ValueFromPipeline=$false, position=1)]
        [Alias("Msg")]
        [String]$Message,
       
        [parameter(Mandatory=$true, ParameterSetName="set1", ValueFromPipeline=$false)]
        [Switch]$Header,
       
        [parameter(Mandatory=$true, ParameterSetName="set3", ValueFromPipeline=$false)]
        [Switch]$Footer,

        [parameter(Mandatory=$true, ValueFromPipeline=$false)]
        [String]$LogFile,

        [parameter(Mandatory=$false, ValueFromPipeline=$false)]
        [Char]$Delimiter = $( if ((Get-Culture).TextInfo.ListSeparator -eq ' ')  {','} else {(Get-Culture).TextInfo.ListSeparator}), 

        [parameter(Mandatory=$false, ValueFromPipeline=$false)]
        [Switch]$ToScreen=$false
    )
   
    $Color = 'Cyan'

    $currentScriptName = $myinvocation.ScriptName
    $StartDate_str     = Get-Date -UFormat "%Y-%m-%d %H:%M:%S"

    if ($isLinux -or $isMacOS) {
        $currentUser     = $ENV:USER
        $currentComputer = uname -n
        $OSName          = uname -s
        $OSArchi         = uname -m
        $StrTerminator   = "`r"        
    }
    else {
        $currentUser     = $ENV:USERDOMAIN + '\' + $ENV:USERNAME
        $currentComputer = $ENV:COMPUTERNAME  
        $WmiInfos        = Get-CimInstance win32_operatingsystem
        $OSName          = $WmiInfos.caption
        $OSArchi         = $WmiInfos.OSArchitecture
        $StrTerminator     = "`r`n"       
    }
   
    Switch ($PsCmdlet.ParameterSetName)
    {
       "set1" # Header
       {
          $Message =  "+----------------------------------------------------------------------------------------+{0}"
          $Message += "Script fullname : $currentScriptName{0}"
          $Message += "When generated : $StartDate_str{0}"
          $Message += "Current user : $currentUser{0}"
          $Message += "Current computer : $currentComputer{0}"
          $Message += "Operating System : $OSName{0}"
          $Message += "OS Architecture : $OSArchi{0}"
          $Message += "+----------------------------------------------------------------------------------------+{0}"
          $Message += "{0}"

          $Message = $Message -f $StrTerminator
          # Log file creation
          [VOID] (New-Item -ItemType File -Path $LogFile -Force)
          Add-Content -Path $LogFile -Value $Message -NoNewline
          break
       }

       "set2" # Body
       {
           $date = Get-Date -UFormat "%Y-%m-%d %H:%M:%S"
           switch ($Category)
           {
               INF  { $Message = ("$date{0} INF{0} $Message{1}" -f $Delimiter, $StrTerminator); $Color = 'Cyan'   ; break }
               WAR  { $Message = ("$date{0} WAR{0} $Message{1}" -f $Delimiter, $StrTerminator); $Color = 'Yellow' ; break }
               ERR  { $Message = ("$date{0} ERR{0} $Message{1}" -f $Delimiter, $StrTerminator); $Color = 'Red'    ; break }
           }
            
           Add-Content -Path $LogFile -Value $Message -NoNewLine
           break
       }
                  
       "set3" # Footer
       {
          # Extracting start date from the file header
          [VOID]( (Get-Content $LogFile -TotalCount 3)[-1] -match '^When generated\s*: (?<date>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})$' )
          if ($Matches.date -eq $null)
          {
             throw "Cannot get the start date from the header. Please check if the file header is correctly formatted."
          }
          $StartDate   = [DateTime]$Matches.date
          $EndDate     = Get-Date
          $EndDate_str = Get-Date $EndDate -UFormat "%Y-%m-%d %H:%M:%S"

          $duration_TotalSeconds = [int](New-TimeSpan -Start $StartDate -End $EndDate | Select-Object -ExpandProperty TotalSeconds)
          $duration_TotalMinutes = (New-TimeSpan -Start $StartDate -End $EndDate | Select-Object -ExpandProperty TotalMinutes)
          $duration_TotalMinutes = [MATH]::Round($duration_TotalMinutes, 2)

          $Message = "{0}"
          $Message += "+----------------------------------------------------------------------------------------+{0}"
          $Message += "End time : $EndDate_str{0}"
          $Message += "Total duration (seconds) : $duration_TotalSeconds{0}"
          $Message += "Total duration (minutes) : $duration_TotalMinutes{0}"
          $Message += "+----------------------------------------------------------------------------------------+{0}"
          
          $Message = $Message -f $StrTerminator

          # Append the footer to the log file
          Add-Content -Path $LogFile -Value $Message -NoNewLine
          break
       }
   } # End switch

   if ($ToScreen)
   {
       Write-Host $Message -ForegroundColor $Color
   }
}

Function Get-Log
{
    Param ($file)
    
    $content = Get-Content -Path $file
    $index = 0
    $sepPos = @()
    foreach ($line in $content)
    {
        if ($line -like '+-*-+')
        {
            $sepPos += $index
        }
        $index++
    }
    
    $result = (Get-Content $file)[(($SepPos[1])+1)..(($SepPos[2])-1)]
    $result -ne '' # Blank lines exclusion
}


Function ConvertFrom-EZlog
{
<#
.SYNOPSIS
   Cmdlet that convert an EZLog file to a set of objects.

.DESCRIPTION
   This cmdlet allows from a log file generated by EZLog to get an object.
   The goal is to be able to easily filter on events with a single Where-Object and
   get to the info stored in the header and in the footer.

   Furthermore it eases the ability to transform the data to JSON if piped to ConvertTo-JSON.

.PARAMETER FilePath
    Specify the log file's path.

.EXAMPLE
   ConvertFrom-EZlog -FilePath C:\temp\mylog.log

   Returns an object from the log file.

.EXAMPLE
   ConvertFrom-EZlog -FilePath C:\temp\mylog.log | ConvertTo-JSON

   Get a JSON format from a logfile.

.NOTES
   AUTHOR: Arnaud PETITJEAN - arnaud@powershell-scripting.com
   LASTEDIT: 2016/11/15
#>


    Param ( 
       [parameter(Mandatory=$true, ValueFromPipeline=$true, position=0)]
       [Alias("Path")]
       [string]$FilePath

     # [parameter(Mandatory=$false, ValueFromPipeline=$false)]
     # [switch]$ToJson
    )

    begin
    {
        
    }

    process
    {
        $result = @{
            Header = @{  ScriptFullname   = ''
                         WhenGenerated    = ''
                         CurrentUser      = ''
                         CurrentComputer  = ''
                         OperatingSystem  = ''
                         OSArchitecture   = ''
                    }
            Events = @()
            Footer = @{  EndTime          = ''
                         Duration         = ''
            }
        }

        $header = Get-Content -Path $FilePath | Select-Object -Skip 1  -First 6
        
        $result.Header.ScriptFullname  = $( $null = $header[0] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?' 
                                            if ( $matches ) 
                                            {
                                               $matches.Value   
                                            }
                                          )
        $result.Header.WhenGenerated   = $( $null = $header[1] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ( $matches ) 
                                            {
                                               [DateTime]($matches.Value)
                                            }
                                          )
        
        $result.Header.CurrentUser     = $( $null = $header[2] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ( $matches ) 
                                            {
                                               $matches.Value
                                            }
                                          )

        $result.Header.CurrentComputer  = $( $null = $header[3] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ( $matches ) 
                                            {
                                               $matches.Value
                                            }
                                          )
                                                  
        $result.Header.OperatingSystem = $( $null = $header[4] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ($matches) 
                                            {
                                               $matches.Value
                                            }
                                          )
        $result.Header.OSArchitecture  = $( $null = $header[5] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ($matches) 
                                            {
                                               $matches.Value
                                            }
                                          )
    
        $footer = Get-Content -Path $FilePath | Select-Object -Skip 1  -Last 3
        $result.Footer.EndTime         = $( $null = $footer[0] -match '(?<Name>.+?)(:\s{1})(?<Value>.+)?'
                                            if ($matches) 
                                            {
                                               [DateTime]($matches.Value)
                                            }
                                          )

        $result.Footer.Duration        = New-TimeSpan -Start $result.Header.WhenGenerated -End $result.Footer.EndTime



        $LogMessages = Get-Log -file $FilePath  
        $separator   = $LogMessages[0][19]
        foreach ($log in $LogMessages)
        {
            $res = $log -split $separator
            $result.Events += [PSCustomObject]@{
                                    Date     = $res[0] -as [DateTime]
                                    Category = $res[1].Trim()
                                    Message  = $res[2].Trim()
                                }
        }
    }

    end
    {
        $result
    }
}

Function Invoke-EZLogRotation
{
<#
.SYNOPSIS
   Clean up old log files in order to keep only the most recent ones.

.DESCRIPTION
   Ancient logs files are either deleted or archived into a Zip file.

   Options are available to specify how many newest files to keep.
   It's also possible to determine a time interval for the logs to keep (daily, monthly, yearly)

.PARAMETER Path
    Directory containing the logs to rotate.

.PARAMETER Filter
    Pattern to identify the logs to rotate. Ex : *.log, *ezlog*.txt, etc.

.PARAMETER Newest
    Specify how many files to keep in the directory. The files are sorted by the LastWriteTime attribute
    and only the newest files determined by this parameter are kept. Other files are deleted or archived.

.PARAMETER Interval
    Specify the periodicy of the rotation.
    Possible values are : Daily, Monthly, Yearly

    Daily : Keep only the logs of the day
    Monthly : Keep only the logs of the month
    Yearly : Keep only the logs of the year

.PARAMETER ArchiveTo
    Indicate if the old logs need to be archived in a Zip before before deletion.

    If not specified, old logs are deleted.

    If specified, you must indicate a file path containing the .zip extension.
    Eg. : C:\logs\archive.zip

    If the archive file already exists, logs will be append to it. If you want to overwrite it you can use
    the -OverwriteArchive switch.

.PARAMETER OverwriteArchive
    Works in association with -ArchiveTo.
    If specified, it overwrites an archive if it already exists.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Newest 15

    Only keeps the latest 15 newest *.log files in the C:\LogFiles directory. Older files will be deleted.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Newest 15 -ArchiveTo C:\LogFiles\archive.zip

    Only keeps the latest 15 newest *.log files in the C:\LogFiles directory. Before deletion, older files
    will be archived into the C:\LogFiles\archive.zip.
    If the archive.zip file already exists, logs will be appended to it.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Newest 15 -ArchiveTo C:\LogFiles\archive.zip -OverwriteArchive

    Only keeps the latest 15 newest *.log files in the C:\LogFiles directory. Before deletion, older files
    will be archived into the C:\LogFiles\archive.zip.
    If the archive.zip file already exists it will be overwritten.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Interval Daily

    Only keeps the *.log files of the day in the C:\LogFiles directory. Older files will be deleted.
    
.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Interval Monthly

    Only keeps the *.log files of the month in the C:\LogFiles directory. Older files will be deleted.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Interval Monthly -ArchiveTo C:\LogFiles\archive.zip -Overwrite

    Only keeps the *.log files of the month in the C:\LogFiles directory. Older files will be archived monthly.

.EXAMPLE
    Invoke-EZLogRotation -Path C:\LogFiles\*.log -Interval Yearly

    Only keeps the *.log files of the current year in the C:\LogFiles directory matching the pattern *.log. Older files will be deleted.

.NOTES
   AUTHOR: Arnaud PETITJEAN
   LASTEDIT: 2018/04/03

#>

    [cmdletBinding(DefaultParameterSetName="", SupportsShouldProcess=$False)]
    PARAM (
        [parameter(Mandatory=$true, position=0)]
        [String]$Path,
       
        [parameter(Mandatory=$true, position=1)]
        [String]$Filter,

        [parameter(Mandatory=$true, ParameterSetName="set1", ValueFromPipeline=$false)]
        [Int]$Newest,

        [parameter(Mandatory=$true, ParameterSetName="set2", ValueFromPipeline=$false, position=1)]
        [Interval]$Interval,
       
        [parameter(Mandatory=$false)]
        [ValidateScript({$_ -like '*.zip'})] 
        [String]$ArchiveTo,
        
        [parameter(Mandatory=$false)]
        [Switch]$OverwriteArchive
    )

    Switch ($PSCmdlet.ParameterSetName) {

        'Set1' {
            $filesToRemove = Get-ChildItem -Path $Path -Filter $Filter | Sort-Object -Property LastWriteTime -Descending | 
                    Select-Object -Skip $Newest
        }
        
        'Set2' {
            Switch ($Interval) {
                'Daily' { $filesToKeep =  Get-ChildItem -Path $Path -Filter $Filter | 
                                             Where-Object { $_.LastWriteTime.Year  -eq (Get-Date).Year -and 
                                                            $_.LastWriteTime.Month -eq (Get-Date).Month -and
                                                            $_.LastWriteTime.Day   -eq (Get-Date).Day }
                          $filesToRemove = Get-ChildItem "$Path\$Filter" -Exclude $filesToKeep
                          Break
                        }
            <# 'Weekly' { $filesToKeep = Get-ChildItem -Path $Path -Filter $Filter |
                                             Where-Object { $_.LastWriteTime -ge (Get-FirstDayOfWeekDate) -and
                                                            $_.LastWriteTime -le (Get-Date) }
                          $filesToRemove = Get-ChildItem "$Path\$Filter" -Exclude $filesToKeep
                          Break
                        }
            #>

                'Monthly' { $filesToKeep =  Get-ChildItem -Path $Path -Filter $Filter | 
                                             Where-Object { $_.LastWriteTime -ge (Get-FirstDayOfMonthDate) -and
                                                            $_.LastWriteTime -le (Get-Date) }
                          $filesToRemove = Get-ChildItem "$Path\$Filter" -Exclude $filesToKeep
                          Break
                        }
                'Yearly' { $filesToKeep =  Get-ChildItem -Path $Path -Filter $Filter | 
                                             Where-Object { $_.LastWriteTime.Year -eq (Get-Date).Year }
                          $filesToRemove = Get-ChildItem "$Path\$Filter" -Exclude $filesToKeep
                          Break
                        }                        
            }
        }
    }

    if ($ArchiveTo -and $OverwriteArchive) {
        $filesToRemove | Compress-Archive -DestinationPath $ArchiveTo -CompressionLevel Optimal -Force
    }
    elseif ($ArchiveTo) {
        $filesToRemove | Compress-Archive -DestinationPath $ArchiveTo -CompressionLevel Optimal -Update
    }
    
    $filesToRemove | Remove-Item

}

Function Get-FirstDayOfWeek 
{

    (Get-Culture).DateTimeFormat.FirstDayOfWeek

}

Function Get-FirstDayOfWeekDate
{
    Param ( [Datetime]$date = (Get-Date) )

    if ( ( (Get-FirstDayOfWeek) -eq [DayOfWeek]::Monday) -and ($date.DayOfWeek -eq [DayOfWeek]::Sunday) ) {
        $nbJoursARetirer = 6 
    } 
    elseif ( ( (Get-FirstDayOfWeek) -eq [DayOfWeek]::Sunday) -and ($date.DayOfWeek -eq [DayOfWeek]::Sunday) ) {
        $nbJoursARetirer = 0 
    }
    else {
        $nbJoursARetirer = [int]$date.DayOfWeek - [int](Get-FirstDayOfWeek)
    }

    $date = $date.AddDays(-$nbJoursARetirer)
    New-Object -TypeName datetime $date.Year, $date.Month, $date.Day, 0, 0, 0, 0
}

Function Get-FirstDayOfMonthDate
{
    Param ( [Datetime]$date = (Get-Date) )

    New-Object -TypeName datetime $date.Year, $date.Month, 1, 0, 0, 0, 0
}