4 # Home page of code is: http://www.smartmontools.org
6 # Copyright (C) 2016 Christian Franke
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)
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/>.
16 # $Id: smartd_mailer.ps1 4338 2016-09-07 19:31:28Z chrfranke $
19 $ErrorActionPreference = "Stop"
21 # Parse command line and check environment
23 if (($args.Count -eq 1) -and ($args[0] -eq "--dryrun")) {
27 $toCsv = $env:SMARTD_ADDRCSV
28 $subject = $env:SMARTD_SUBJECT
29 $file = $env:SMARTD_FULLMSGFILE
31 if (!((($args.Count -eq 0) -or $dryrun) -and $toCsv -and $subject -and $file)) {
36 set SMARTD_ADDRCSV='Comma separated mail addresses'
37 set SMARTD_SUBJECT='Mail Subject'
38 set SMARTD_FULLMSGFILE='X:\PATH\TO\Message.txt'
40 .\$($MyInvocation.MyCommand.Name) [--dryrun]
45 # Set default sender address
46 if ($env:COMPUTERNAME -match '^[-_A-Za-z0-9]+$') {
47 $hostname = $env:COMPUTERNAME.ToLower()
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"
60 $from = "smartd daemon <root@$hostname>"
63 . .\smartd_mailer.conf.ps1
66 $to = $toCsv.Split(",")
67 $body = Get-Content -Path $file | Out-String
70 SmtpServer = $smtpServer; From = $from; To = $to
71 Subject = $subject; Body = $body
74 $parm += @{ Port = $port }
77 $parm += @{ useSsl = $true }
80 if ($username -and ($password -or $passwordEnc)) {
82 $secureString = ConvertTo-SecureString -String $password -AsPlainText -Force
84 $passwordEnc = $passwordEnc -replace '[\r\n\t ]',''
85 $secureString = ConvertTo-SecureString -String $passwordEnc
87 $credential = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $username,$secureString
88 $parm += @{ Credential = $credential }
93 echo "Send-MailMessage" @parm
95 Send-MailMessage @parm