|
1 | | -function Install-MDDaemon |
2 | | -{ |
3 | | -<# |
| 1 | +function Install-MDDaemon { |
| 2 | + <# |
4 | 3 | .SYNOPSIS |
5 | 4 | Configures a computer for using the Mail Daemon |
6 | 5 | |
|
31 | 30 | |
32 | 31 | .PARAMETER SentPath |
33 | 32 | The folder in which emails that were successfully sent are stored for a specified time before being deleted. |
| 33 | +
|
| 34 | + .PARAMETER FailedPath |
| 35 | + The path where mails that could repeatedly not be sent are moved to. |
34 | 36 | |
35 | 37 | .PARAMETER DaemonUser |
36 | 38 | The user to grant permissions needed to function as the Daemon account. |
|
42 | 44 | |
43 | 45 | .PARAMETER MailSentRetention |
44 | 46 | The time to keep successfully sent emails around. |
| 47 | +
|
| 48 | + .PARAMETER MailAbandonThreshold |
| 49 | + How long we attempt to send an email before abandoning it and moving it to -FailedPath. |
| 50 | +
|
| 51 | + .PARAMETER MailFailedRetention |
| 52 | + How long we keep an abandoned email around before removing it entirely. |
45 | 53 | |
46 | 54 | .PARAMETER SmtpServer |
47 | 55 | The mailserver to use for sending emails. |
|
57 | 65 | .PARAMETER RecipientDefault |
58 | 66 | Default email address to send the email to, if the individual script queuing the email does not specify one. |
59 | 67 | |
| 68 | + .PARAMETER UseSSL |
| 69 | + Use SSL for sending emails. |
| 70 | +
|
| 71 | + .PARAMETER NoLogging |
| 72 | + Disables logging. |
| 73 | + Unless specified, this setup step will also prepare the windows eventlog by creating a dedicated eventlog for MailDaemon. |
| 74 | +
|
60 | 75 | .EXAMPLE |
61 | 76 | PS C:\> Install-MDDaemon -ComputerName DC1, DC2, DC3 -TaskUser $cred -DaemonUser "DOMAIN\MailDaemon" -SmtpServer 'mail.domain.org' -SenderDefault 'daemon@domain.org' -RecipientDefault 'helpdesk-t2@domain.org' |
62 | 77 | |
|
83 | 98 |
|
84 | 99 | [string] |
85 | 100 | $SentPath, |
| 101 | + |
| 102 | + [string] |
| 103 | + $FailedPath, |
86 | 104 |
|
87 | 105 | [string] |
88 | 106 | $DaemonUser, |
|
92 | 110 |
|
93 | 111 | [Timespan] |
94 | 112 | $MailSentRetention, |
| 113 | + |
| 114 | + [Timespan] |
| 115 | + $MailAbandonThreshold, |
| 116 | + |
| 117 | + [Timespan] |
| 118 | + $MailFailedRetention, |
95 | 119 |
|
96 | 120 | [string] |
97 | 121 | $SmtpServer, |
|
103 | 127 | $SenderCredential, |
104 | 128 |
|
105 | 129 | [string] |
106 | | - $RecipientDefault |
| 130 | + $RecipientDefault, |
| 131 | + |
| 132 | + [switch] |
| 133 | + $UseSSL, |
| 134 | + |
| 135 | + [switch] |
| 136 | + $NoLogging |
107 | 137 | ) |
108 | 138 |
|
109 | | - begin |
110 | | - { |
| 139 | + begin { |
111 | 140 | #region Repetitions (ugly) |
112 | 141 | # Specifying repetitions directly in the commandline is ugly. |
113 | 142 | # It ignores explicit settings and requires copying the repetition object from another task. |
|
155 | 184 | #endregion Repetitions (ugly) |
156 | 185 |
|
157 | 186 | #region Setup Task Configuration |
158 | | - if (-not $NoTask) |
159 | | - { |
| 187 | + if (-not $NoTask) { |
160 | 188 | $action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -Command Invoke-MDDaemon" |
| 189 | + if ($NoLogging) { $action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -Command Invoke-MDDaemon -NoLogging" } |
161 | 190 | $triggers = @() |
162 | 191 | $triggers += New-ScheduledTaskTrigger -AtStartup -RandomDelay "00:15:00" |
163 | 192 | $triggers += New-ScheduledTaskTrigger -At "00:00:00" -Daily |
|
173 | 202 | TaskName = 'MailDaemon' |
174 | 203 | InputObject = $taskItem |
175 | 204 | } |
176 | | - if ($TaskUser) |
177 | | - { |
| 205 | + if ($TaskUser) { |
178 | 206 | $parametersRegister["User"] = $TaskUser.UserName |
179 | 207 | $parametersRegister["Password"] = $TaskUser.GetNetworkCredential().Password |
180 | 208 | } |
181 | 209 | } |
182 | 210 | #endregion Setup Task Configuration |
183 | 211 |
|
184 | 212 | #region Preparing Parameters |
185 | | - $parameters = @{ } |
186 | | - foreach ($key in $PSBoundParameters.Keys) |
187 | | - { |
188 | | - if ($key -notin 'PickupPath', 'SentPath', 'MailSentRetention', 'SmtpServer', 'SenderDefault', 'RecipientDefault') { continue } |
189 | | - $parameters[$key] = $PSBoundParameters[$key] |
190 | | - } |
| 213 | + $parameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include 'PickupPath', 'SentPath', 'FailedPath', 'MailSentRetention', 'MailAbandonThreshold', 'MailFailedRetention', 'SmtpServer', 'SenderDefault', 'RecipientDefault', 'UseSSL' |
191 | 214 |
|
192 | 215 | $paramMainInstallCall = @{ |
193 | 216 | ArgumentList = $parameters |
|
217 | 240 | #endregion The Main Setup Scriptblock |
218 | 241 | } |
219 | 242 |
|
220 | | - process |
221 | | - { |
| 243 | + process { |
222 | 244 | #region Ensure Modules are installed |
223 | 245 | $testResults = Test-Module -ComputerName $ComputerName -Credential $Credential -Module @{ |
224 | 246 | MailDaemon = $script:ModuleVersion |
|
227 | 249 |
|
228 | 250 | $failedTests = $testResults | Where-Object Success -EQ $false |
229 | 251 |
|
230 | | - if ($failedTests) |
231 | | - { |
| 252 | + if ($failedTests) { |
232 | 253 | $grouped = $failedTests | Group-Object Name |
233 | | - foreach ($groupSet in $grouped) |
234 | | - { |
| 254 | + foreach ($groupSet in $grouped) { |
235 | 255 | Copy-Module -ModuleName (Get-Module $groupSet.Name).ModuleBase -ToComputer $groupSet.Group.ComputerName |
236 | 256 | } |
237 | 257 | } |
|
242 | 262 | Invoke-PSFCommand @paramMainInstallCall |
243 | 263 |
|
244 | 264 | #region Securely store credentials |
245 | | - if ($PSBoundParameters.ContainsKey('SenderCredential')) |
246 | | - { |
| 265 | + if ($PSBoundParameters.ContainsKey('SenderCredential')) { |
247 | 266 | $parametersSave = @{ |
248 | | - ComputerName = $ComputerName |
249 | | - Credential = $SenderCredential |
250 | | - Path = 'C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml' |
| 267 | + ComputerName = $ComputerName |
| 268 | + TargetCredential = $SenderCredential |
| 269 | + Path = 'C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml' |
251 | 270 | } |
| 271 | + if ($Credential) { $parametersSave['Credential'] = $Credential } |
252 | 272 | if ($TaskUser) { $parametersSave['AccessAccount'] = $TaskUser } |
253 | 273 | Save-MDCredential @parametersSave |
254 | 274 |
|
255 | | - $parametersInvoke = @{ $parametersInvoke['ComputerName'] = $ComputerName } |
| 275 | + $parametersInvoke = @{ ComputerName = $ComputerName } |
| 276 | + if ($Credential) { $parametersInvoke['Credential'] = $Credential } |
256 | 277 | Invoke-PSFCommand @parametersInvoke -ScriptBlock { |
257 | 278 | Set-MDDaemon -SenderCredentialPath "C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml" |
258 | 279 | } |
259 | 280 | } |
260 | 281 | #endregion Securely store credentials |
| 282 | + |
| 283 | + #region Setup Logging |
| 284 | + if (-not $NoLogging) { |
| 285 | + Invoke-PSFCommand @parametersInvoke -ScriptBlock { |
| 286 | + if ($PSVersionTable.PSVersion.Major -gt 5 -and -not $IsWindows) { return } |
| 287 | + Set-PSFLoggingProvider -Name eventlog -InstanceName MailDaemonInvoke -LogName MailDaemon -Source MailDaemon -Enabled $true -Wait |
| 288 | + Write-PSFMessage -Message "Setting up MailDaemon logging" |
| 289 | + Disable-PSFLoggingProvider -Name eventlog -InstanceName MailDaemonInvoke |
| 290 | + } |
| 291 | + } |
| 292 | + #endregion Setup Logging |
261 | 293 |
|
262 | 294 | #region Setup Task |
263 | | - if (-not $NoTask) |
264 | | - { |
265 | | - foreach ($computerObject in $ComputerName) |
266 | | - { |
267 | | - if ($ComputerName.Type -like 'CimSession') { $parametersRegister["CimSession"] = $computerObject.InputObject } |
268 | | - elseif (-not $ComputerName.IsLocalhost) { $parametersRegister["CimSession"] = $ComputerName } |
269 | | - |
270 | | - $null = Register-ScheduledTask @parametersRegister |
271 | | - } |
| 295 | + if (-not $NoTask) { |
| 296 | + Invoke-PSFCommand @parametersInvoke -ScriptBlock { |
| 297 | + param ($ParametersRegister) |
| 298 | + |
| 299 | + $taskObject = Get-ScheduledTask -TaskName $ParametersRegister.TaskName -ErrorAction Ignore |
| 300 | + if ($taskObject) { $taskObject | Unregister-ScheduledTask } |
| 301 | + |
| 302 | + $null = Register-ScheduledTask @ParametersRegister |
| 303 | + } -ArgumentList $parametersRegister |
272 | 304 | } |
273 | 305 | #endregion Setup Task |
274 | 306 | } |
|
0 commit comments