| 1 | #
|
|---|
| 2 | # smartd mailer script
|
|---|
| 3 | #
|
|---|
| 4 | # Copyright (C) 2016 Christian Franke
|
|---|
| 5 | #
|
|---|
| 6 | # This program is free software; you can redistribute it and/or modify
|
|---|
| 7 | # it under the terms of the GNU General Public License as published by
|
|---|
| 8 | # the Free Software Foundation; either version 2, or (at your option)
|
|---|
| 9 | # any later version.
|
|---|
| 10 | #
|
|---|
| 11 | # You should have received a copy of the GNU General Public License
|
|---|
| 12 | # (for example COPYING); If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 13 | #
|
|---|
| 14 | # $Id$
|
|---|
| 15 | #
|
|---|
| 16 |
|
|---|
| 17 | # Parse command line and check environment
|
|---|
| 18 | $dryrun = $false
|
|---|
| 19 | if (($args.Count -eq 1) -and ($args[0] -eq "--dryrun")) {
|
|---|
| 20 | $dryrun = $true
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | $toCsv = $env:SMARTD_ADDRCSV
|
|---|
| 24 | $subject = $env:SMARTD_SUBJECT
|
|---|
| 25 | $file = $env:SMARTD_FULLMSGFILE
|
|---|
| 26 |
|
|---|
| 27 | if (!((($args.Count -eq 0) -or $dryrun) -and $toCsv -and $subject -and $file)) {
|
|---|
| 28 | echo `
|
|---|
| 29 | "smartd mailer script
|
|---|
| 30 |
|
|---|
| 31 | Usage:
|
|---|
| 32 | set SMARTD_ADDRCSV='Comma separated mail addresses'
|
|---|
| 33 | set SMARTD_SUBJECT='Mail Subject'
|
|---|
| 34 | set SMARTD_FULLMSGFILE='X:\PATH\TO\Message.txt'
|
|---|
| 35 |
|
|---|
| 36 | .\$($MyInvocation.MyCommand.Name) [--dryrun]
|
|---|
| 37 | "
|
|---|
| 38 | exit 1
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | # Read configuration
|
|---|
| 42 | $from = "smartd daemon <root@$($env:COMPUTERNAME.ToLower()).local>"
|
|---|
| 43 | . .\smartd_mailer.conf.ps1
|
|---|
| 44 | if (!$?) { exit 1 }
|
|---|
| 45 |
|
|---|
| 46 | # Create parameters
|
|---|
| 47 | $to = $toCsv.Split(",")
|
|---|
| 48 | $body = Get-Content -Path $file | Out-String
|
|---|
| 49 |
|
|---|
| 50 | $parm = @{
|
|---|
| 51 | SmtpServer = $smtpServer; From = $from; To = $to
|
|---|
| 52 | Subject = $subject; Body = $body
|
|---|
| 53 | }
|
|---|
| 54 | if ($port) {
|
|---|
| 55 | $parm += @{ Port = $port }
|
|---|
| 56 | }
|
|---|
| 57 | if ($useSsl) {
|
|---|
| 58 | $parm += @{ useSsl = $true }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | if ($username -and ($password -or $passwordSecure)) {
|
|---|
| 62 | if (!$passwordSecure) {
|
|---|
| 63 | $secureString = ConvertTo-SecureString -String $password -AsPlainText -Force
|
|---|
| 64 | } else {
|
|---|
| 65 | $secureString = ConvertTo-SecureString -String $passwordSecure
|
|---|
| 66 | }
|
|---|
| 67 | $credential = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $username,$secureString
|
|---|
| 68 | $parm += @{ Credential = $credential }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | # Send mail
|
|---|
| 72 | if ($dryrun) {
|
|---|
| 73 | echo "Send-MailMessage" @parm
|
|---|
| 74 | } else {
|
|---|
| 75 | Send-MailMessage @parm
|
|---|
| 76 | if (!$?) { exit 1 }
|
|---|
| 77 | }
|
|---|