emailfunctions.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
Function Enable-EmailReminder {
    #this function requires the PSScheduledJob module
    [cmdletbinding(SupportsShouldProcess)]
    [OutputType("None")]

    Param(
        [Parameter(Position = 0, HelpMessage = "What time do you want to send your daily email reminder?")]
        [ValidateNotNullOrEmpty()]
        [datetime]$Time = "8:00AM",
        [Parameter(HelpMessage = "What is your email server name or address?")]
        [ValidateNotNullOrEmpty()]
        [string]$SMTPServer = $PSEmailServer,
        [Parameter(Mandatory, HelpMessage = "Enter your email address")]
        [ValidateNotNullOrEmpty()]
        [ValidatePattern("\S+@*.\.\w{2,4}")]
        [string]$To,
        [Parameter(HelpMessage = "Enter the FROM email address. If you don't specify one, the TO address will be used.")]
        [ValidatePattern("\S+@*.\.\w{2,4}")]
        [string]$From,
        [Parameter(HelpMessage = "Include if you need to use SSL?")]
        [switch]$UseSSL,
        [Parameter(HelpMessage = "Specify the port to use for your email server")]
        [ValidateNotNullOrEmpty()]
        [int32]$Port = 25,
        [Parameter(HelpMessage = "Specify any credential you need to authenticate to your mail server.")]
        [PSCredential]$MailCredential,
        [Parameter(HelpMessage = "Send an HTML body email")]
        [switch]$AsHtml,
        [ValidateNotNullOrEmpty()]
        [ValidateScript( {$_ -gt 0})]
        [int]$Days = 3,
        [Parameter(Mandatory, HelpMessage = "Re-enter your local user credentials for the scheduled job task")]
        [ValidateNotNullOrEmpty()]
        [PSCredential]$TaskCredential,
        [ValidateNotNullOrEmpty()]
        [string]$TaskPath = $mytaskHome
    )
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Using these mail parameters:"

        if (-Not $From) {
            $From = $To
        }
        $hash = @{
            To         = $To
            From       = $From
            SMTPServer = $SMTPServer
            Port       = $Port
        }
        if ($MailCredential) {
            $hash.Add("Credential", $MailCredential)
        }
        If ($UseSSL) {
            $hash.add("UseSSL", $True)
        }

        $hash | Out-String | Write-Verbose
        #define the job scriptblock
        $sb = {
            Param([hashtable]$Hash, [int]$Days, [string]$myPath)
            #uncomment Write-Host lines for troubleshooting
            #$PSBoundParameters | out-string | write-host -ForegroundColor cyan
            #get tasks for the next 3 days as the body

            Set-MyTaskPath -Path $myPath
            #get-variable mytask* | out-string | Write-Host
            write-host "[$((Get-Date).ToString())] Getting tasks for the next $days days."
            $data = Get-MyTask -Days $Days
            if ($data) {
                if ($hash.BodyAsHTML) {
                    Write-Host "[$((Get-Date).ToString())] Sending as HTML" -ForegroundColor green
                    #css to be embedded in the html document
                    $head = @"
<Title>Upcoming Tasks</Title>
<style>
body { background-color:rgb(199, 194, 194);
    font-family:Tahoma;
    font-size:12pt; }
td, th { border:1px solid black;
            border-collapse:collapse; }
th { color:white;
        background-color:black; }
table, tr, td, th { padding: 2px; margin: 0px }
tr:nth-child(odd) {background-color: lightgray}
table { width:95%;margin-left:5px; margin-bottom:20px;}
.alert {color: red ;}
.warn {color:#ff8c00;}
</style>
<br>
<H1>My Tasks</H1>
"@

                    [xml]$html = $data | ConvertTo-HTML -Fragment

                    #parse html to add color attributes
                    for ($i = 1; $i -le $html.table.tr.count - 1; $i++) {
                        $class = $html.CreateAttribute("class")
                        #check the value of the percent free memory column and assign a class to the row
                        if ($html.table.tr[$i].td[4] -eq 'True') {
                            $class.value = "alert"
                            $html.table.tr[$i].Attributes.Append($class) | Out-Null
                        }
                        elseif ((($html.table.tr[$i].td[3] -as [DateTime]) - (Get-Date)).totalHours -le 24 ) {
                            $class.value = "warn"
                            $html.table.tr[$i].Attributes.Append($class) | Out-Null
                        }
                    }

                    $Body = ConvertTo-HTML -body $html.InnerXml -Head $head | Out-String

                }
                else {
                    Write-Host "[$((Get-Date).ToString())] Sending as TEXT" -ForegroundColor Green
                    $body = $data | Out-string
                }
            }
            else {
                Write-Warning "No tasks found due in the next $days days."
                #bail out
                return
            }
            $hash.Add("Body", $body)
            $hash.Add("Subject", "Tasks Due in the Next $days Days")
            $hash.Add("ErrorAction", "Stop")
            Try {
                Send-MailMessage @hash
                #if you receive the job I wanted to display some sort of result
                Write-Output "[$((Get-Date).ToString())] Message ($($hash.subject)) sent to $($hash.to) from $($hash.from)"
            }
            Catch {
                throw $_
            }
        } #define scriptblock
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Testing for an existing job"
        Try {
            $job = Get-ScheduledJob -Name myTasksEmail -ErrorAction stop
            if ($job) {
                Write-Warning "An existing mail job was found. Please remove it first with Disable-EmailReminder and try again."
                #bail out
                return
            }
        }
        Catch {
            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] No existing job found"
        }
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Validating Requirements"
        if ((Get-Module PSScheduledJob) -And (($PSVersionTable.Platform -eq 'Win32NT') -OR ($PSVersionTable.PSEdition -eq 'Desktop'))) {

            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Creating a Daily job trigger for $($Time.TimeofDay)"
            $trigger = New-JobTrigger -Daily -At $Time

            $opt = New-ScheduledJobOption -RunElevated -RequireNetwork
            Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Registering the scheduled job"
            if ($AsHtml) {
                $hash.Add("BodyAsHTML", $True)
            }
            #hash table of parameters to splat Register-ScheduledJob
            $regParams = @{
                ScriptBlock        = $sb
                Name               = "myTasksEmail"
                Trigger            = $Trigger
                ArgumentList       = $hash, $Days, $TaskPath
                MaxResultCount     = 5
                ScheduledJobOption = $opt
                Credential         = $TaskCredential
            }
            $regParams | Out-String | Write-Verbose
            Register-ScheduledJob  @regParams
        }
        else {
            Write-Warning "This command requires the PSScheduledJob module on a Windows platform."
        }
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
    } #end

} #close Enable-MailReminder

Function Disable-EmailReminder {
    [cmdletbinding(SupportsShouldProcess)]
    [OutputType("None")]

    Param()
    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"
    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Processing"
        if ((Get-Module PSScheduledJob) -And (($PSVersionTable.Platform -eq 'Win32NT') -OR ($PSVersionTable.PSEdition -eq 'Desktop'))) {
            Try {
                if (Get-ScheduledJob -Name myTasksEmail -ErrorAction stop) {
                    #The cmdlet appears to ignore -WhatIf so I'll handle it myself
                    if ($PSCmdlet.ShouldProcess("myTasksEmail")) {
                        Unregister-ScheduledJob -Name myTasksEmail -ErrorAction stop
                    } #should process
                } #if task found
            }
            Catch {
                Write-Warning "Can't find any matching scheduled jobs with the name 'myTasksEmail'."
            }
        }
        else {
            Write-Warning "This command requires the PSScheduledJob module on a Windows platform."
        }
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"
    } #end

} #close Disable-EmailReminder

Function Get-EmailReminder {
    [cmdletbinding()]
    Param ()

    Begin {
        Write-Verbose "[$((Get-Date).TimeofDay) BEGIN ] Starting $($myinvocation.mycommand)"

    } #begin

    Process {
        Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting scheduled job myTasksEmail"
        if ((Get-Module PSScheduledJob -ListAvailable) -And (($PSVersionTable.Platform -eq 'Win32NT') -OR ($PSVersionTable.PSEdition -eq 'Desktop'))) {
            $t = Get-ScheduledJob myTasksEmail

            $hash = $t.InvocationInfo.Parameters[0].where( {$_.name -eq "argumentlist"}).value

            Try {
                #get the last run
                Write-Verbose "[$((Get-Date).TimeofDay) PROCESS] Getting last job run"
                $last = Get-Job -name $t.name -Newest 1 -ErrorAction stop
            }
            Catch {
                $last = [PSCustomObject]@{
                    PSEndTime = "11/30/1999" -as [datetime]
                    State     = "The task has not yet run"
                }
            }
            [pscustomobject]@{
                Task       = $t.Name
                Frequency  = $t.JobTriggers.Frequency
                At         = $t.JobTriggers.at.TimeOfDay
                To         = $hash.To
                From       = $hash.From
                MailServer = $hash.SMTPServer
                Port       = $hash.Port
                UseSSL     = $hash.UseSSL
                AsHTML     = $hash.BodyAsHTML
                LastRun    = $last.PSEndTime
                LastState  = $last.State
                Started    = $last.psBeginTime
                Ended      = $last.psEndTime
                Result     = $last.output
                Enabled    = $t.Enabled
                Errors     = $last.Errors
                Warnings   = $last.warnings
            }
        }
        else {
            Write-Warning "This command requires the PSScheduledJob module on a Windows platform."
        }
    } #process

    End {
        Write-Verbose "[$((Get-Date).TimeofDay) END ] Ending $($myinvocation.mycommand)"

    } #end

} #close Get-EmailReminder