-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSendMyQueries.ps1
More file actions
239 lines (210 loc) · 8 KB
/
SendMyQueries.ps1
File metadata and controls
239 lines (210 loc) · 8 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
230
231
232
233
234
235
236
237
238
239
#requires -Version 2
<#
.SYNOPSIS
Execute few queries from MSSQL DB and send the results as a pretty HTML
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.String]
$AddressList
)
function Get-QueryResultAsHTMLTable
{
<#
.SYNOPSIS
Execute the query and parse the result as XML
.DESCRIPTION
Execute an MSSQL query and reteave the data, parsing the data into an html table with the headers provided
.EXAMPLE
Get-QueryResultAsHTMLTable
-Data 'Column 1</td><td>Column 2</td><td>Column 3</td></tr>',
-SQLQuery $('SELECT * FROM Table')
-ConnectionString 'Server=Server;Database=DB;User Id=user;Password=pass'
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.String]
$Data,
[Parameter(Mandatory = $true, Position = 1)]
[Object]
$SQLQuery,
[Parameter(Mandatory = $true, Position = 2)]
[System.String]
$ConnectionString
)
$Connection = New-Object -TypeName System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = $ConnectionString
$Connection.Open()
$Command = New-Object -TypeName System.Data.SQLClient.SQLCommand
$Command.Connection = $Connection
$Command.CommandText = $SQLQuery
$Reader = $Command.ExecuteReader()
while ($Reader.Read())
{
$Data = $Data + '<tr>'
for ($i = 0; $i -lt $Reader.FieldCount; $i++)
{
if ($Reader.GetValue($i) -match 'fail')
{
$Data = $Data + '<td bgcolor="#ff4d4d">' +$Reader.GetValue($i) +'</td>'
}
elseif ($Reader.GetValue($i) -match 'SUCCESS' -Or $Reader.GetValue($i) -match 'pass')
{
$Data = $Data + '<td bgcolor="#00cc00">' +$Reader.GetValue($i) +'</td>'
}
else
{
$Data = $Data + '<td>' +$Reader.GetValue($i) +'</td>'
}
}
$Data = $Data + '</tr>'
}
$Connection.Close()
return $Data
}
function Send-HTMLmail
{
<#
.SYNOPSIS
Send an email with HTML body
.DESCRIPTION
use this command to send email with html body to several users
.EXAMPLE
Send-HTMLmail
-SendTo specify array of contacts
-emailSmtpServer choose the smtp server
-emailSmtpServerPort smtp server port
-emailSmtpUser username for smtp server
-emailSmtpPass password for smtp server
-emailFrom user name to send from
-Subject the subject of the email (auto add date to the title)
- the html body
#>
[CmdletBinding()]
param
(
[Parameter(Mandatory = $true, Position = 0)]
[System.Object]
$SendTo,
[Parameter(Mandatory = $false, Position = 1)]
[System.String]
$emailSmtpServer = 'smtp.gmail.com',
[Parameter(Mandatory = $false, Position = 2)]
[System.String]
$emailSmtpServerPort = '587',
[Parameter(Mandatory = $false, Position = 3)]
[System.String]
$emailSmtpUser = 'user@gmail.com',
[Parameter(Mandatory = $false, Position = 4)]
[System.String]
$emailSmtpPass = 'Password',
[Parameter(Mandatory = $false, Position = 5)]
[System.String]
$emailFrom = 'user@gmail.com',
[Parameter(Mandatory = $false, Position = 6)]
[System.String]
$Subject = 'Email title',
[Parameter(Mandatory = $true, Position = 7)]
[System.String]
$html
)
$emailMessage = New-Object -TypeName System.Net.Mail.MailMessage
$emailMessage.From = $emailFrom
foreach ($adress in $SendTo)
{
$emailMessage.To.Add( $adress )
}
$date = Get-Date
$emailMessage.Subject = "$Subject - $date"
$emailMessage.IsBodyHtml = $true
$emailMessage.Body = $html ##Get-Content $HtmlFilePath
$SMTPClient = New-Object -TypeName System.Net.Mail.SmtpClient -ArgumentList ( $emailSmtpServer , $emailSmtpServerPort )
$SMTPClient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList ( $emailSmtpUser , $emailSmtpPass )
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object -TypeName System.Net.NetworkCredential -ArgumentList ( $emailSmtpUser , $emailSmtpPass )
$SMTPClient.Send( $emailMessage )
}
$header = '<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<style>
.rotate90
{
transform:rotate(180deg);
-ms-transform:rotate(180deg); /* IE 9 */
-webkit-transform:rotate(180deg); /* Opera, Chrome, and Safari */
}
</style>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>This is the email title</title>
</head>
<body>
<table width="583" border="0" cellspacing="0" cellpadding="0">
<tr>
</tr>
<tr bgcolor="#3d90bd">
<td align="left" valign="top" bgcolor="#3d90bd"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="35" align="left" valign="top"> </td>
<td align="left" valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center" valign="middle" bgcolor="#3d90bd">
<div style="color:#FFFFFF; font-family:Roboto-Regular, Times, serif; font-size:34px;">Multi Platform Results from Last Night - 6.2.3</div>
<div style="font-family: Verdana, Geneva, sans-serif; color:#898989; font-size:12px;"></div></td>
</tr>
<tr>
<td align="left" valign="top" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#525252;">
<table BORDERCOLOR=Black border="1" cellspacing="3" cellpadding="5" width="500" align="center" style="background-color:#FFFFFF;text-align: center;margin-bottom: 5px">
<tr><td>'
$footer = '</table>
</td>
</tr>
</table></td>
<td width="35" align="left" valign="top"> </td>
</tr>
</table><p></p></td>
</tr>
<tr>
<td align="left" valign="top" bgcolor="#3d90bd" style="background-color:#3d90bd;"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="35"> </td>
<td height="50" align="center" valign="middle" style="color:#FFFFFF; font-size:11px; font-family:Arial, Helvetica, sans-serif;"><b>Company NAME</b><br>For more info contact us:</br><a href="mailto:main@email.com?Subject=Errors%20In%20Test" target="_top">
PV Infra team</a>
<td width="35"> </td>
</tr>
</table></td>
</tr>
</table>
</body>
</html>
'
##First Query
$Data = 'Operation System</td><td>Count</td><td>Results</td></tr>'
$SQLQuery = $('select [Os],count([TestName]),[Result]
FROM [TeamCity].[dbo].[MultiPlatformCommands]
where CONVERT(DATE, [ResultTime]) = CONVERT(DATE, CURRENT_TIMESTAMP)
group by [Result], [OS]
order by [Result]
')
$Data = Get-QueryResultAsHTMLTable -Data $Data -SQLQuery $SQLQuery -ConnectionString 'Server=Server;Database=DB;User Id=user;Password=pass'
$Data = $Data + '</table>
</td>
</tr>
<tr>
<td align="center" valign="top" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#525252;">
<table BORDERCOLOR=Black border="1" cellspacing="3" cellpadding="5" width="500" align="center" style="background-color:#FFFFFF;text-align: center">
<tr><td>'
##Secound Query
$Data = $Data + 'Test Name</td><td>Operation System</td><td>Results</td></tr>'
$SQLQuery = $("select [TestName] , [Os],[Result]
FROM [TeamCity].[dbo].[MultiPlatformCommands]
where CONVERT(DATE, [ResultTime]) = CONVERT(DATE, CURRENT_TIMESTAMP)
and Result = 'Fail'
")
$Data = Get-QueryResultAsHTMLTable -Data $Data -SQLQuery $SQLQuery -ConnectionString 'Server=Server;Database=DB;User Id=user;Password=pass'
## build the complete HTML body
$html = $header + $Data + $footer
Send-HTMLmail -SendTo $AddressList -html $html