-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSend-EmailReport.ps1
More file actions
229 lines (216 loc) · 6.69 KB
/
Send-EmailReport.ps1
File metadata and controls
229 lines (216 loc) · 6.69 KB
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
function Send-EmailReport{
<#
.Synopsis
Sends formatted email of object output
.DESCRIPTION
This script takes an input object or array of objects, organizes them into an HTML table, and sends a formatted HTML email to the specified recipients.
Input object can be passed through the pipeline or specified by parameter.
HTML table format includes a column for each property in the object. Any formatting of properties or input data must be done before calling this function.
.PARAMETER To
Specify the email address(es) to send the report to.
.PARAMETER Subject
Specify the email subject. If the header parameter is not specified the subject field is also used in the header of the email.
.PARAMETER Body
Specify the object or array of objects to be included in the body of the email. This object or objects are expanded into a table format and wrapped in HTML.
If a property of an object is itself an object with properties or an array of objects, the script includes them in the standard powershell notation. The script will not attempt to expand any of these properties
.PARAMETER SMTPServer
The SMTP server to use for sending the email. The default value should be sufficient for most cases.
.PARAMETER From
The email address to use in the from field of the email. The default value of PSReporting@domain.com should be sufficient for most cases, but this can be changed as needed. There is no validation by the SMTP server, this doesn't need to be a valid email address
.PARAMETER Header
Any text to be included at the top of the email, before the table of data is presented. The default value of this is the email subject and current date/time. This is presented in a larger font as an HTML header field.
.PARAMETER H2
Any text to be included at the top of the email, before the table of data is presented but below the Header. This is presented in a slightly smaller font than the Header field.
.PARAMETER H3
Any text to be included at the top of the email, before the table of data but below the Header or H2 fields. This is presented in the same font as the table data.
.PARAMETER AsAttachment
This switch parameter causes the body data to be included as a csv attachment, rather than an HTML table in the email.
.EXAMPLE
Get-ADUser -filter * | Send-EmailReport -To myboss@domain.com -subject "User List"
This example gets all users in AD and sends the list in an email
#>
[CmdletBinding(SupportsShouldProcess=$True,ConfirmImpact="Low")]
Param
(
[Parameter(Mandatory=$true)]
[String[]]$To,
[String[]]$CC,
[Parameter(Mandatory=$True)]
[String]$Subject,
[Parameter(Mandatory=$True,
ValueFromPipeline=$True)]
[Object[]]$Body,
[Parameter(Mandatory=$True)]
[String]$SMTPServer,
[Parameter(Mandatory=$True)]
[String]$From,
[String]$Header,
[String]$H2,
[String]$H3,
[Switch]$AsAttachment
)
Begin{
$head = @'
<style>
html, body {
height: 100%;
}
body, p, h1, h2, h3, li, td {
font-family: 'Segoe UI', Verdana, Arial;
}
body {
color: #000000;
margin:0; padding:0;
font-size : .8em;
width:100%;
}
img {
-ms-interpolation-mode:bicubic;
}
h1 {
font-weight : bold;
font-size : 1.3em;
margin-top:.7em;
margin-bottom: 1em;
border-bottom: 1px silver solid;
padding-bottom: 8px;
padding-top:0px;
color: #1A72B9;
}
h2 {
font-weight : bold;
font-size : 1.05em;
margin-bottom: 0.25em;
color: #1A72B9;
}
h3 {
font-weight : bold;
font-size : 1em;
margin-bottom: 0.25em;
color: #125095;
}
p {
margin:0px 0em 1em 0em;
line-height:1.5em;
}
li {
margin-bottom: 4pt;
}
a:active, a:link, a:visited {
text-decoration:underline;
}
a:hover {
text-decoration:none;
}
table {
font-size: 1em;
margin-top: 1em 0 1em 0;
border-collapse:collapse;
width:90%;
}
tr {
vertical-align: top;
}
th {
background-color: #EDF6FF;
vertical-align: bottom;
color:#215A8F;
padding:5px;
border-bottom:1px solid #90ADBC;
}
td {
vertical-align: top;
margin-top: .25em;
padding:10px;
border-bottom:1px solid #90ADBC;
}
td, th {
text-align: left;
padding-left:1em;
border-right:2px solid white;
}
small {
font-size: .85em;
}
tt {
font-family: monospace;
font-size:1.1em;
}
pre {
font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace;
font-size: 1.02em;
background-color:#F7F7F7;
padding:1px 5px;
/* Wrap content in pre tags if too long */
overflow-x: auto; /* Use horizontal scroller if needed; for Firefox 2, not needed in Firefox 3 */
white-space: pre-wrap; /* css-3 */
white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
.syntax {
margin-top: 0em;
margin-left: 1em;
margin-right: 1em;
background-color: whitesmoke;
}
hr {
color: silver;
background-color: silver;
height: 1px;
border-bottom:1px solid #90ADBC;
}
</style>
'@
If ($Header){
$ReportHTML = "<h1>$Header</h1>"
}else {
$ReportHTML = "<h1>$Subject - $(Get-Date)</h1>"
}
If ($H2){
$ReportHTML += "<h2>$H2</h2>"
}
If ($H3){
$ReportHTML += "<h3>$H3</h3><br>"
}
Write-Verbose "Verifying connection to SMTP Server"
Try{
$socket = new-object Net.Sockets.TcpClient
$socket.Connect($SMTPServer,25)
if (! $socket.Connected){Throw}
} Catch {
Write-Error "Unable to connect to SMTP server $smtpserver. Check your network connection" -ErrorAction Stop
} Finally {
$Socket.Close()
}
}
Process{
$output += $body
}
End{
If ($AsAttachment){
$attachment = "$($env:temp)\$Subject-$(get-date -UFormat "%m.%d.%y").csv"
$output | Export-Csv -NoTypeInformation -Path $attachment -Force
}else {
$ReportHTML += $output | convertto-html -Head $head
$ReportHTML += "<h2>Number of objects found: $(($output | measure-object).count)</h2>"
}
if ($pscmdlet.ShouldProcess($To)){
If ($attachment){
If ($CC){
Send-MailMessage -SmtpServer $SMTPServer -To $To -Cc $CC -Subject $Subject -Body "$ReportHTML" -BodyAsHtml -From $From -Attachments $attachment
} else {
Send-MailMessage -SmtpServer $SMTPServer -To $To -Subject $Subject -Body "$ReportHTML" -BodyAsHtml -From $From -Attachments $attachment
}
remove-item $attachment
} else {
If ($CC){
Send-MailMessage -SmtpServer $SMTPServer -To $To -Cc $CC -Subject $Subject -Body "$ReportHTML" -BodyAsHtml -From $From
} else {
Send-MailMessage -SmtpServer $SMTPServer -To $To -Subject $Subject -Body "$ReportHTML" -BodyAsHtml -From $From
}
}
}
}
}