-
Notifications
You must be signed in to change notification settings - Fork 1
/
SecureAuditor-SendMail.ps1
67 lines (63 loc) · 2.45 KB
/
SecureAuditor-SendMail.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
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$From,
[Parameter(Mandatory)]
[string[]]$To,
[Parameter(Mandatory)]
[string]$SmtpServer,
[string]$Port = 25,
[pscredential]$Credential = $null,
[switch]$UseSSL
)
if ($PSVersionTable.PSVersion.Major -lt 6) {
# Enable tls1.2 from default (Ssl3, Tls)
# https://stackoverflow.com/questions/41618766/powershell-invoke-webrequest-fails-with-ssl-tls-secure-channel
[Net.ServicePointManager]::SecurityProtocol = 'tls12, tls11, tls'
# Progress bar can significantly impact cmdlet performance
# https://github.com/PowerShell/PowerShell/issues/2138
$ProgressPreference = 'SilentlyContinue'
}
$subject = ("Secure Audit Report for {0}" -f [environment]::MachineName)
$auditorPath = [IO.Path]::Combine($PSScriptRoot, '../SecureAuditor.ps1')
$body = & $auditorPath | Out-String
$isHtml = $false
if (Get-Command 'ConvertFrom-Markdown' -ErrorAction SilentlyContinue) {
# https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/convertfrom-markdown
$body = ($body | ConvertFrom-Markdown).Html
$isHtml = $true
}
# https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/send-mailmessage
$parameters = @{
From = $From
To = $To
Subject = $Subject
Body = $Body
Encoding = 'UTF8'
SmtpServer = $SmtpServer
Port = $Port
}
if ($useSsl -eq $true) {
$parameters.Add('UseSSL', $true)
}
if ($credential -ne $null) {
$parameters.Add('Credential', $Credential)
}
if ($isHtml) {
$parameters.Add("BodyAsHtml", $true)
}
$config = Get-IniContent -file ([IO.Path]::Combine($PSScriptRoot, '../SecureAuditor.ini'))
$config = Get-IniContent -file ([IO.Path]::Combine($PSScriptRoot, '../SecureAuditor.local.ini')) -ini $config
if ([bool]$config.FileIntegrityMonitoring.Enabled) {
$attachmentPath = $config.FileIntegrityMonitoring.BaselinePath
if (-not [IO.Path]::IsPathRooted($attachmentPath)) {
$attachmentPath = [IO.Path]::Combine($PSScriptRoot, '..', $attachmentPath)
}
}
if ($null -ne $attachmentPath -and (Test-Path -Path $attachmentPath -ErrorAction SilentlyContinue)) {
$filename = [IO.Path]::GetFileName($attachmentPath)
$zipPath = [IO.Path]::Combine([System.IO.Path]::GetTempPath(), ("{0}.zip" -f $filename))
Compress-Archive -LiteralPath $attachmentPath -DestinationPath $zipPath -Force
$parameters.Add('Attachments', $zipPath)
}
Send-MailMessage @parameters