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