|
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 | |
|
103 | 102 | $SenderCredential, |
104 | 103 |
|
105 | 104 | [string] |
106 | | - $RecipientDefault |
| 105 | + $RecipientDefault, |
| 106 | + |
| 107 | + [switch] |
| 108 | + $UseSSL |
107 | 109 | ) |
108 | 110 |
|
109 | | - begin |
110 | | - { |
| 111 | + begin { |
111 | 112 | #region Repetitions (ugly) |
112 | 113 | # Specifying repetitions directly in the commandline is ugly. |
113 | 114 | # It ignores explicit settings and requires copying the repetition object from another task. |
|
155 | 156 | #endregion Repetitions (ugly) |
156 | 157 |
|
157 | 158 | #region Setup Task Configuration |
158 | | - if (-not $NoTask) |
159 | | - { |
| 159 | + if (-not $NoTask) { |
160 | 160 | $action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-NoProfile -Command Invoke-MDDaemon" |
161 | 161 | $triggers = @() |
162 | 162 | $triggers += New-ScheduledTaskTrigger -AtStartup -RandomDelay "00:15:00" |
|
173 | 173 | TaskName = 'MailDaemon' |
174 | 174 | InputObject = $taskItem |
175 | 175 | } |
176 | | - if ($TaskUser) |
177 | | - { |
| 176 | + if ($TaskUser) { |
178 | 177 | $parametersRegister["User"] = $TaskUser.UserName |
179 | 178 | $parametersRegister["Password"] = $TaskUser.GetNetworkCredential().Password |
180 | 179 | } |
181 | 180 | } |
182 | 181 | #endregion Setup Task Configuration |
183 | 182 |
|
184 | 183 | #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 | | - } |
| 184 | + $parameters = $PSBoundParameters | ConvertTo-PSFHashtable -Include 'PickupPath', 'SentPath', 'MailSentRetention', 'SmtpServer', 'SenderDefault', 'RecipientDefault', 'UseSSL' |
191 | 185 |
|
192 | 186 | $paramMainInstallCall = @{ |
193 | 187 | ArgumentList = $parameters |
|
217 | 211 | #endregion The Main Setup Scriptblock |
218 | 212 | } |
219 | 213 |
|
220 | | - process |
221 | | - { |
| 214 | + process { |
222 | 215 | #region Ensure Modules are installed |
223 | 216 | $testResults = Test-Module -ComputerName $ComputerName -Credential $Credential -Module @{ |
224 | 217 | MailDaemon = $script:ModuleVersion |
|
227 | 220 |
|
228 | 221 | $failedTests = $testResults | Where-Object Success -EQ $false |
229 | 222 |
|
230 | | - if ($failedTests) |
231 | | - { |
| 223 | + if ($failedTests) { |
232 | 224 | $grouped = $failedTests | Group-Object Name |
233 | | - foreach ($groupSet in $grouped) |
234 | | - { |
| 225 | + foreach ($groupSet in $grouped) { |
235 | 226 | Copy-Module -ModuleName (Get-Module $groupSet.Name).ModuleBase -ToComputer $groupSet.Group.ComputerName |
236 | 227 | } |
237 | 228 | } |
|
242 | 233 | Invoke-PSFCommand @paramMainInstallCall |
243 | 234 |
|
244 | 235 | #region Securely store credentials |
245 | | - if ($PSBoundParameters.ContainsKey('SenderCredential')) |
246 | | - { |
| 236 | + if ($PSBoundParameters.ContainsKey('SenderCredential')) { |
247 | 237 | $parametersSave = @{ |
248 | | - ComputerName = $ComputerName |
249 | | - Credential = $SenderCredential |
250 | | - Path = 'C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml' |
| 238 | + ComputerName = $ComputerName |
| 239 | + TargetCredential = $SenderCredential |
| 240 | + Path = 'C:\ProgramData\PowerShell\MailDaemon\senderCredentials.clixml' |
251 | 241 | } |
| 242 | + if ($Credential) { $parametersSave['Credential'] = $Credential } |
252 | 243 | if ($TaskUser) { $parametersSave['AccessAccount'] = $TaskUser } |
253 | 244 | Save-MDCredential @parametersSave |
254 | 245 |
|
|
260 | 251 | #endregion Securely store credentials |
261 | 252 |
|
262 | 253 | #region Setup Task |
263 | | - if (-not $NoTask) |
264 | | - { |
265 | | - foreach ($computerObject in $ComputerName) |
266 | | - { |
| 254 | + if (-not $NoTask) { |
| 255 | + foreach ($computerObject in $ComputerName) { |
267 | 256 | if ($ComputerName.Type -like 'CimSession') { $parametersRegister["CimSession"] = $computerObject.InputObject } |
268 | 257 | elseif (-not $ComputerName.IsLocalhost) { $parametersRegister["CimSession"] = $ComputerName } |
269 | 258 |
|
|
0 commit comments