-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReport-PrintServerIssues.ps1
More file actions
619 lines (434 loc) · 22.3 KB
/
Report-PrintServerIssues.ps1
File metadata and controls
619 lines (434 loc) · 22.3 KB
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -NonInteractive -File D:\Scripts\Report-PrintServerIssues\Report-PrintServerIssues.ps1
[CmdletBinding(DefaultParameterSetName="Schedule")]
Param (
[Parameter(Mandatory=$False, ParameterSetName='Schedule')]
[int]$SendEmailDay = 1,
[Parameter(Mandatory=$False, ParameterSetName='EmailForce')]
[switch]$ForceEmail
)
Function Append-HtmlSection {
Param (
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
$Table,
[Parameter(Mandatory=$True)]
[ValidateNotNullOrEmpty()]
[string]$Heading,
[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string]$Reason,
[Parameter(Mandatory=$False)]
[ValidateNotNullOrEmpty()]
[string]$Action
)
$HtmlArgs = @{}
$HtmlArgs['PreContent'] = "<h2>$Heading</h2>$(If ($Reason) {"<p>$Reason."})<p>$($Table.Count) issue(s) found:<p>"
If ($Action) {
$HtmlArgs['PostContent'] = "<p><i>Corrective action: $($Action).</i>"
}
($Table | ConvertTo-Html -Fragment @HtmlArgs) -replace '###', '<br>' | Out-File $HtmlReportPathLocal -Encoding default -Append
}
# --------------------------------------------------
# Define variables
# --------------------------------------------------
$EmailSubject = "Weekly report on print server issues"
$EmailSender = "PrintServerIssues@domain.tld"
$EmailServer = "smtp.domain.tld"
$ScriptBasePath = Get-Item $PSCommandPath
$ReportBasePath = "D:\Reports\PrintServerIssues"
If (-Not(Test-Path $ScriptBasePath -ErrorAction SilentlyContinue)) {
Throw "Base path ""$ScriptBasePath"" inaccesible"
}
$PrintServersTxt = "$ScriptBasePath\PrintServers.txt"
$EmailRecipientsTxt = "$ScriptBasePath\EmailRecipients.txt"
$CommonDriversTxt = "$ScriptBasePath\CommonDrivers.txt"
$IgnoreDriversTxt = "$ScriptBasePath\IgnoreDrivers.txt"
$IgnorePrintersTxt = "$ScriptBasePath\IgnorePrinters.txt"
$UniDriversCsv = "$ScriptBasePath\UniDrivers.csv"
$HtmlReportPathLocal = "$ReportBasePath\PrintServerIssues.html"
$HtmlReportPathRemote = $HtmlReportPathLocal.Replace((Get-Item $HtmlReportPathLocal).PSDrive.Root,"\\$($env:COMPUTERNAME.ToLower())\")
$RegKey = 'HKCU:\SOFTWARE\DFDS\Report-PrintServerIssues' # Do not run PS with -NoProfile
# --------------------------------------------------
# Load server and email recipient lists
# --------------------------------------------------
$ComputerName = Get-Content $PrintServersTxt
$EmailRecipient = Get-Content $EmailRecipientsTxt
# --------------------------------------------------
# Load lists of common drivers and stuff to ignore
# --------------------------------------------------
$CommonDrivers = Get-Content $CommonDriversTxt
$IgnorePrinters = Get-Content $IgnorePrintersTxt
$IgnoreDrivers = Get-Content $IgnoreDriversTxt
$ProcessorArchitectures = @('Windows NT x86', 'Windows x64')
# --------------------------------------------------
# Load uni driver mapping
# --------------------------------------------------
$UniDrivers = @{}
Import-Csv $UniDriversCsv -Delimiter ';' | ForEach {
$UniDrivers[$_.Brand] = $_.DriverName
}
# --------------------------------------------------
# Test if each server is reachable
# --------------------------------------------------
# Define ping status messages
$PingStatusTable = @{}
$PingStatusTable['0'] = 'Success'
$PingStatusTable['11001'] = 'Buffer Too Small'
$PingStatusTable['11002'] = 'Destination Net Unreachable'
$PingStatusTable['11003'] = 'Destination Host Unreachable'
$PingStatusTable['11004'] = 'Destination Protocol Unreachable'
$PingStatusTable['11005'] = 'Destination Port Unreachable'
$PingStatusTable['11006'] = 'No Resources'
$PingStatusTable['11007'] = 'Bad Option'
$PingStatusTable['11008'] = 'Hardware Error'
$PingStatusTable['11009'] = 'Packet Too Big'
$PingStatusTable['11010'] = 'Request Timed Out'
$PingStatusTable['11011'] = 'Bad Request'
$PingStatusTable['11012'] = 'Bad Route'
$PingStatusTable['11013'] = 'TimeToLive Expired Transit'
$PingStatusTable['11014'] = 'TimeToLive Expired Reassembly'
$PingStatusTable['11015'] = 'Parameter Problem'
$PingStatusTable['11016'] = 'Source Quench'
$PingStatusTable['11017'] = 'Option Too Big'
$PingStatusTable['11018'] = 'Bad Destination'
$PingStatusTable['11032'] = 'Negotiating IPSEC'
$PingStatusTable['11050'] = 'General Failure'
# Test connection
$ComputerError = @()
ForEach ($Computer in ($ComputerName | Sort-Object)) {
# Attempt to lookup computername in DNS, add to $ComputerError array with error message if fails
Try {$IPAddress = @(Resolve-DnsName -Name $Computer -ErrorAction Stop)[0] | Select -Expand IPAddress; $Resolvable = $true}
Catch {
$ErrorMessage = "$($_.InvocationInfo.MyCommand): $($_.Exception.Message)"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
Continue
}
# Attempt to ping computername, add to $ComputerError array with error message if fails
$PingStatus = (Get-WmiObject -Class Win32_PingStatus -Filter "Address='$IPAddress' AND Timeout=1000").StatusCode
If ($PingStatus -ne 0) {
$ErrorMessage = "Win32_PingStatus: '$($PingStatusTable["$PingStatus"])' ($PingStatus)"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
Continue
}
# Check for remote admin rights
If (-Not(Test-Path "\\dkcph-infpprt1\admin$" -ErrorAction SilentlyContinue)) {
$ErrorMessage = "No admin rights on computer"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
Continue
}
}
# --------------------------------------------------
# Get printers
# --------------------------------------------------
$ComputerNoError = $ComputerName | Where-Object {$ComputerError.ComputerName -notcontains $_} | Sort-Object
Write-Verbose "Getting printer information from $($ComputerNoError.Count) computer(s)" -Verbose
$Printers = ForEach ($Computer in $ComputerNoError) {
Try {Get-Printer -ComputerName $Computer -ErrorAction Stop | Where-Object {$IgnorePrinters -notcontains $_.Name} | Sort-Object Name}
Catch {
$ErrorMessage = "$($_.InvocationInfo.MyCommand): $($_.Exception.Message)"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
}
}
# --------------------------------------------------
# Get drivers
# --------------------------------------------------
$ComputerNoError = $ComputerName | Where-Object {$ComputerError.ComputerName -notcontains $_} | Sort-Object
Write-Verbose "Getting driver information from $($ComputerNoError.Count) computer(s)" -Verbose
$Drivers = ForEach ($Computer in $ComputerNoError) {
Try {Get-PrinterDriver -ComputerName $Computer | Where-Object {$IgnoreDrivers -notcontains $_.Name} | Sort-Object ComputerName, Name, PrinterEnvironment}
Catch {
$ErrorMessage = "$($_.InvocationInfo.MyCommand): $($_.Exception.Message)"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
}
}
# --------------------------------------------------
# Get ports
# --------------------------------------------------
$Ports = @()
$ComputerNoError = $ComputerName | Where-Object {$ComputerError.ComputerName -notcontains $_} | Sort-Object
Write-Verbose "Getting port information from $($ComputerNoError.Count) computer(s)" -Verbose
$Ports = ForEach ($Computer in $ComputerNoError) {
Try {Get-PrinterPort -ComputerName $Computer | Where-Object {$_.Description -ne 'Local Port'} | Sort-Object Name}
Catch {
$ErrorMessage = "$($_.InvocationInfo.MyCommand): $($_.Exception.Message)"
$ComputerError += [pscustomobject]@{
ComputerName = $Computer
Error = $ErrorMessage
}
Write-Warning "$Computer - $ErrorMessage"
}
}
# --------------------------------------------------
# Initiate report file
# --------------------------------------------------
$HtmlPre = @"
<title>Print server issues</title>
<meta http-equiv='refresh' content='900'>
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: #1B5786; text-align: left; color: white; font-family: Verdana; font-size: 0.875em}
TD {border-width: 1px; padding: 5px; border-style: solid; border-color: black; background-color: #E3EAF0; font-family: Verdana; font-size: 0.875em}
Body {font-family: Verdana; font-size: 0.875em}
H1 {font-family: Verdana}
</style>
<body>
<h1>Print server issues</h1>
<h2>Summary</h2>
<p>Report generated on <b>$((Get-Date -Format s).Replace('T',' ').Substring(0,16))</b>.
<br>Always up-to-date report: <a href="file://$HtmlReportPathRemote">$HtmlReportPathRemote</a>.
<p>Queried <b>$($ComputerName.Count)</b> print servers: $(($ComputerName | Sort-Object) -join ', ')
<p>Found <b>$($Printers.Count)</b> printers
<br>Found <b>$($Drivers.Count)</b> drivers
<br>Found <b>$($Ports.Count)</b> ports
<p>Ignored <b>$($IgnorePrinters.Count)</b> printer(s): $(($IgnorePrinters | Sort-Object) -join ', ')
<br>Ignored <b>$($IgnoreDrivers.Count)</b> driver(s): $(($IgnoreDrivers | Sort-Object) -join ', ')
</ul>
"@
$HtmlPre | Out-File $HtmlReportPathLocal -Encoding default
# --------------------------------------------------
# Computers with issues
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Computers with issues"
If ($ComputerError) {Append-HtmlSection -Heading $Heading -Table ($ComputerError | Sort-Object ComputerName)}
# --------------------------------------------------
# Computers with common drivers missing
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Computers with common drivers missing"
$Reason = "Common drivers should be installed on all print servers, to ensure new printers can easily be setup"
$Action = "Run script to install common drivers: \\dkcph-infpswl1\DML\Hardware\Print\Install-Common-Drivers.cmd"
Write-Verbose $Heading -Verbose
$Table = @(ForEach ($Computer in $ComputerNoError) {
ForEach ($Driver in $CommonDrivers) {
$DriverInstalled = [bool]($Drivers | Where-Object {$_.ComputerName -eq $Computer -and $_.Name -eq $Driver})
If (-Not($DriverInstalled)) {
[pscustomobject]@{
ComputerName = $Computer
DriverName = $Driver
}
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Drivers not in use (except default ones)
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Drivers not in use"
$Reason = "Drivers not in use muddies the waters, increasing the risk of choosing a wrong driver for printers"
$Action = "Remove unnecessary drivers"
Write-Verbose $Heading -Verbose
$Table = @(ForEach ($Computer in ($Drivers | Where-Object {$CommonDrivers -notcontains $_.Name} | Select -Unique -ExpandProperty ComputerName)) {
$ComputerDriverNames = $Drivers | Where-Object {$_.ComputerName -eq $Computer -and $CommonDrivers -notcontains $_.Name} | Select-Object -Unique -ExpandProperty Name
ForEach ($Driver in $ComputerDriverNames) {
$DriverInUse = [bool]($Printers | Where-Object {$_.ComputerName -eq $Computer -and $_.DriverName -eq $Driver})
If (-Not($DriverInUse)) {
[pscustomobject]@{
ComputerName = $Computer
DriverName = $Driver
}
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Drivers not installed for all processor architectures
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Drivers not installed for all processor architectures"
$Reason = "Drivers need to be installed for all supported architectures, to ensure that both 32-bit and 64-bit clients can print"
$Action = "Install drivers for the missing processor architectures"
Write-Verbose $Heading -Verbose
$DriversMissingArchitecture = @()
$Table = @(ForEach ($Computer in ($Drivers | Select -Unique -ExpandProperty ComputerName)) {
$Drivers | Where-Object {$_.ComputerName -eq $Computer} | Group-Object Name | ForEach {
$DriverName = $_.Name
Compare-Object -ReferenceObject $ProcessorArchitectures -DifferenceObject $_.Group.PrinterEnvironment -PassThru | ForEach {
$DriversMissingArchitecture += [pscustomobject]@{
ComputerName = $Computer
DriverName = $DriverName
MissingArchitecture = $_
}
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Comments not populated
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Printers without comments"
$Reason = "The comment field must contain the make and model of the printer. This is also required for other print server health checks"
$Action = "Add/correct printer model in comment, or delete and re-create printer using Excel sheet"
Write-Verbose $Heading -Verbose
$Table = @($Printers | Where-Object {$_.Comment.Length -eq 0} | Select ComputerName, Name, DriverName)
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Printers not using proper drivers
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Printers using wrong drivers"
$Reason = "Wrong printer drivers can lead to printing issues"
$Action = "Add/correct printer model in comment"
Write-Verbose $Heading -Verbose
$Table = @(ForEach ($Printer in ($Printers | Where-Object {$_.Comment.Length -gt 0})) {
$Brand = $Printer.Comment.Split(' ')[0]
$UniDriver = $UniDrivers[$Brand]
Switch ([bool]$UniDriver) {
$true {
$ExpectedDriverPrefix = $UniDriver
}
Default {
$ExpectedDriverPrefix = $Brand
}
}
If ($Printer.DriverName -notlike "$($ExpectedDriverPrefix)*") {
[pscustomobject]@{
ComputerName = $Printer.ComputerName
PrinterName = $Printer.Name
PrinterComment = $Printer.Comment
DriverName = $Printer.DriverName
ExpectedDriverPrefix = $ExpectedDriverPrefix
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Ports not using proper names
# (indicating manual creation)
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Printer ports not using proper names"
$Reason = "Printer ports must be created using the Excel sheet to ensure consistency. The printer port name must correspond with the name of the printer"
$Action = "Re-create ports using Excel sheet, attach printers to correct ports"
Write-Verbose $Heading -Verbose
$Table = @($Ports | Where-Object {$_.Name -notlike '*-*'} | Select ComputerName, @{N='PortName';E={$_.Name}})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Port/print mismatch
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Printer/port name mismatch"
$Reason = "Printer ports must be created using the Excel sheet to ensure consistency. The printer port name must correspond with the name of the printer"
$Action = "Re-create ports using Excel sheet, attach printers to correct ports"
Write-Verbose $Heading -Verbose
$NonFollowMePrinters = $Printers | Where-Object {$_.Name -notlike '*Follow Me'}
$Table = @(ForEach ($Printer in $NonFollowMePrinters) {
# Ignoring spaces and hyphens, does the printer name properly correlate with the port name?
If (-Not("$($Printer.Name -replace "[ -&]", '')" -like "$($Printer.PortName -replace "[ -]", '')*")) {
[pscustomobject]@{
ComputerName = $Printer.ComputerName
PrinterName = $Printer.Name
PortName = $Printer.PortName
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Ports not in use
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Ports not in use"
$Reason = "Ports not in use are probably left-overs after making changes. Remove them to avoid confusion"
$Action = "Remove ports not in use"
Write-Verbose $Heading -Verbose
$Table = @(ForEach ($Computer in ($Ports | Select -Unique -ExpandProperty ComputerName)) {
$ComputerPortNames = $Ports | Where-Object {$_.ComputerName -eq $Computer} | Select-Object -Unique -ExpandProperty Name
ForEach ($Port in $ComputerPortNames) {
$PortInUse = [bool]($Printers | Where-Object {$_.ComputerName -eq $Computer -and $_.PortName -eq $Port})
If (-Not($PortInUse)) {
[pscustomobject]@{
ComputerName = $Computer
PortName = $Port
}
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Ports with more than one printer?
# Shortest name, match like* against others?
# --------------------------------------------------
Remove-Variable Heading, Reason, Action, Table -ErrorAction SilentlyContinue
$Heading = "Ports with more than one printer attached"
$Reason = "In some cases this could be due to legacy multi-port print servers being used. In most cases this is just left-overs after testing, or a simple mistake"
$Action = "Ensure printer-to-port mappings are correct"
Write-Verbose $Heading -Verbose
$Table = @(ForEach ($Computer in ($Ports | Select -Unique -ExpandProperty ComputerName)) {
$ComputerPortNames = $Ports | Where-Object {$_.ComputerName -eq $Computer} | Select-Object -Unique -ExpandProperty Name
ForEach ($Port in $ComputerPortNames) {
$PortPrinters = @($Printers | Where-Object {$_.ComputerName -eq $Computer -and $_.PortName -eq $Port} | Select-Object -ExpandProperty Name)
$ShortestName = $PortPrinters | Sort-Object Length | Select-Object -First 1
If ($PortPrinters | Where-Object {$_ -notlike "$ShortestName*"}) {
[pscustomobject]@{
ComputerName = $Computer
PortName = $Port
PrinterNames = $PortPrinters -join "###"
}
}
}
})
If ($Table) {Append-HtmlSection -Heading $Heading -Table $Table -Reason $Reason -Action $Action}
# --------------------------------------------------
# Wrap up HTML report
# --------------------------------------------------
$HtmlPost = @"
</body>
</html>
"@
$HtmlPost | Out-File $HtmlReportPathLocal -Encoding default -Append
Write-Verbose "Report generated" -Verbose
# --------------------------------------------------
# Send e-mail?
# --------------------------------------------------
# Debug
<#
Remove-ItemProperty $RegKey -Name 'EmailLastSent'
(Get-ItemProperty $RegKey -Name 'EmailLastSent').EmailLastSent
#>
# Any email recipients defined?
If ($EmailRecipient) {
# Get time email was last sent, default to "very long ago"
Try {$EmailLastSent = Get-Date (Get-ItemProperty $RegKey -Name 'EmailLastSent' -ErrorAction SilentlyContinue).EmailLastSent}
Catch {$EmailLastSent = Get-Date 0}
# Calculate days since email was last sent
$Now = Get-Date
$DayOfWeek = $Now.DayOfWeek.value__
$HoursSinceLastEmail = New-TimeSpan -Start $EmailLastSent -End $Now | Select-Object -Expand TotalHours
# If day of week matches $SendEmailDay and it's been at least 24 hours since last e-mail (to avoid spamming, if report is generated more frequent than daily), start send e-mail routine
[bool]$SendEmail = ($ForceEmail -or ($DayOfWeek -eq $SendEmailDay -and $HoursSinceLastEmail -ge 24)) -and $EmailRecipient
If ($SendEmail) {
# Send report
$EmailBody = Get-Content $HtmlReportPathLocal -Raw
Send-MailMessage -Body $EmailBody -BodyAsHtml -Subject $EmailSubject -From $EmailSender -To $EmailRecipient -SmtpServer $EmailServer
# Record time email was sent to registry
If (-Not(Test-Path $RegKey)) {
New-Item -Path $RegKey -Force | Out-Null
}
Set-ItemProperty $RegKey -Name 'EmailLastSent' -Value (Get-Date -Format 's') -Force
Write-Verbose "Emailed report to $(($EmailRecipient | Sort-Object) -join ', ')" -Verbose
} Else {
Write-Verbose "Not emailing report. It is not $([DayOfWeek]$SendEmailDay) or report has already been emailed in the last 24 hours, and -ForceEmail was not specified." -Verbose
}
}