]> git.proxmox.com Git - mirror_smartmontools-debian.git/blame - os_win32/smartd_mailer.ps1
import smartmontools 7.0
[mirror_smartmontools-debian.git] / os_win32 / smartd_mailer.ps1
CommitLineData
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#
ff28b140 8# SPDX-License-Identifier: GPL-2.0-or-later
f9e10201 9#
ff28b140 10# $Id: smartd_mailer.ps1 4760 2018-08-19 18:45:53Z chrfranke $
f9e10201
JD
11#
12
13$ErrorActionPreference = "Stop"
14
15# Parse command line and check environment
16$dryrun = $false
17if (($args.Count -eq 1) -and ($args[0] -eq "--dryrun")) {
18 $dryrun = $true
19}
20
21$toCsv = $env:SMARTD_ADDRCSV
22$subject = $env:SMARTD_SUBJECT
23$file = $env:SMARTD_FULLMSGFILE
24
25if (!((($args.Count -eq 0) -or $dryrun) -and $toCsv -and $subject -and $file)) {
26 echo `
27"smartd mailer script
28
29Usage:
30set SMARTD_ADDRCSV='Comma separated mail addresses'
31set SMARTD_SUBJECT='Mail Subject'
32set SMARTD_FULLMSGFILE='X:\PATH\TO\Message.txt'
33
34.\$($MyInvocation.MyCommand.Name) [--dryrun]
35"
36 exit 1
37}
38
39# Set default sender address
40if ($env:COMPUTERNAME -match '^[-_A-Za-z0-9]+$') {
41 $hostname = $env:COMPUTERNAME.ToLower()
42} else {
43 $hostname = "unknown"
44}
45if ($env:USERDNSDOMAIN -match '^[-._A-Za-z0-9]+$') {
46 $hostname += ".$($env:USERDNSDOMAIN.ToLower())"
47} elseif ( ($env:USERDOMAIN -match '^[-_A-Za-z0-9]+$') `
48 -and ($env:USERDOMAIN -ne $env:COMPUTERNAME) ) {
49 $hostname += ".$($env:USERDOMAIN.ToLower()).local"
50} else {
51 $hostname += ".local"
52}
53
54$from = "smartd daemon <root@$hostname>"
55
56# Read configuration
57. .\smartd_mailer.conf.ps1
58
59# Create parameters
60$to = $toCsv.Split(",")
61$body = Get-Content -Path $file | Out-String
62
63$parm = @{
64 SmtpServer = $smtpServer; From = $from; To = $to
65 Subject = $subject; Body = $body
66}
67if ($port) {
68 $parm += @{ Port = $port }
69}
70if ($useSsl) {
71 $parm += @{ useSsl = $true }
72}
73
74if ($username -and ($password -or $passwordEnc)) {
75 if (!$passwordEnc) {
76 $secureString = ConvertTo-SecureString -String $password -AsPlainText -Force
77 } else {
78 $passwordEnc = $passwordEnc -replace '[\r\n\t ]',''
79 $secureString = ConvertTo-SecureString -String $passwordEnc
80 }
81 $credential = New-Object -Typename System.Management.Automation.PSCredential -Argumentlist $username,$secureString
82 $parm += @{ Credential = $credential }
83}
84
85# Send mail
86if ($dryrun) {
87 echo "Send-MailMessage" @parm
88} else {
89 Send-MailMessage @parm
90}