-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSend-DBAEmail.ps1
398 lines (345 loc) · 12.2 KB
/
Send-DBAEmail.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
function Send-DBAEmail {
[CmdletBinding()]
param (
[Parameter(Position=0,HelpMessage="Enter the name of a SQL server")]
[ValidateNotNullorEmpty()]
[Alias("ComputerName")]
[string]$SqlServer=$env:ComputerName,
[string]$Database='dbareports',
[string]$Path='~\Documents'
)
begin {
Import-Module -Name EnhancedHTML2
}
process {
$style = @"
<style>
body {
color:#333333;
font-family:Calibri,Tahoma;
font-size: 10pt;
}
h1 {
text-align:center;
}
h2 {
border-top:1px solid #666666;
}
th {
background-color: #4CAF50;
color: white;
}
.odd { background-color:#ffffff; }
.even { background-color:#dddddd; }
.paginate_enabled_next, .paginate_enabled_previous {
cursor:pointer;
border:1px solid #222222;
background-color:#dddddd;
padding:2px;
margin:4px;
border-radius:2px;
}
.paginate_disabled_previous, .paginate_disabled_next {
color:#666666;
cursor:pointer;
background-color:#dddddd;
padding:2px;
margin:4px;
border-radius:2px;
}
.dataTables_info { margin-bottom:4px; }
.sectionheader { cursor:pointer; }
.sectionheader:hover { color:red; }
.grid { width:100% }
.danger { background-color: red }
.warn { background-color: yellow }
.red {
color:red;
font-weight:bold;
}
</style>
"@
$everything_ok = $true
#Test connectivity to instance. Will stop script if this fails
try {
Write-Verbose -Message "Testing connectivity to server $SQLServer and database $Database"
Invoke-Sqlcmd -ServerInstance $SqlServer -Database $Database -Query 'SELECT @@version' -ErrorAction Stop | Out-Null
}
catch {
$everything_ok = $false
Write-Error -Message "Failed connecting to instance $SqlServer and database $Database"
}
if($everything_ok){
$filepath = Join-Path -Path $Path -ChildPath "DatabaseReport.html"
Write-Verbose -Message "Report will be saved on location $filepath"
$params = @{
'As' = 'Table';
'PreContent' = '<h2>♦ SQL Agent Summary</h2>';
'EvenRowCssClass' = 'even';
'OddRowCssClass' = 'odd';
'MakeHiddenSection'= $false;
'TableCssClass' = 'grid';
'Properties' = 'Environment',
'ServerName',
'NumberOfJobs',
'SuccessfullJobs',
'FailedJobs',
'DisabledJobs',
'UnknownJobs'
}
$html_SAJS = Get-SQLAgentJobSummary -SqlServer $SqlServer -Database $Database | ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table';
'PreContent' = '<h2>♦ Databases not backed up in the last 24 Hours</h2>';
'EvenRowCssClass' = 'even';
'OddRowCssClass' = 'odd';
'MakeHiddenSection'= $false;
'TableCssClass' = 'grid';
'Properties' = 'ServerName',
'Name',
'DaysSinceBackup',
'LastBackupDate',
'LastDifferentialBackupDate',
'LastLogBackupDate'
}
$html_SDMB = Get-SQLDatabaseMissingBackup -SqlServer $SqlServer -Database $Database | ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table';
'PreContent' = '<h2>♦ Low Disk Space</h2>';
'EvenRowCssClass' = 'even';
'OddRowCssClass' = 'odd';
'MakeHiddenSection'= $false;
'TableCssClass' = 'grid';
'Properties' = 'ServerName',
'DiskName',
'Label',
'Capacity',
'FreeSpace',
@{n='Percentage';e={$_.Percentage}; css={if ($_.Percentage -lt 11) {'danger'} else {'warn'}}}
}
$html_SLDS = Get-SQLLowDiskSpace -SqlServer $SqlServer -Database $Database | ConvertTo-EnhancedHTMLFragment @params
$params = @{
'As' = 'Table';
'PreContent' = '<h2>♦ Top 5 Fastest Growing Disks in Last 24 Hours</h2>';
'EvenRowCssClass' = 'even';
'OddRowCssClass' = 'odd';
'MakeHiddenSection'= $false;
'TableCssClass' = 'grid';
'Properties' = 'ServerName',
'DiskName',
'Label',
'Capacity',
'Growth',
'FreeSpace',
'Percentage'
}
$html_SFGD = Get-SQLFastestGrowingDisks -SqlServer $SqlServer -Database $Database | ConvertTo-EnhancedHTMLFragment @params
$params = @{
'CssStyleSheet' = $style;
'Title' = "Database Report for BCC domain";
'PreContent' = "<h1>Database Report for BCC domain</h1>";
'HTMLFragments' = @($html_SAJS,$html_SDMB,$html_SLDS,$html_SFGD);
}
ConvertTo-EnhancedHTML @params | Out-File -FilePath $filepath
}
}
end {
}
}
function Get-SQLAgentJobSummary {
[CmdletBinding()]
param (
[string]$SqlServer=$env:ComputerName,
[string]$Database='dbareports'
)
begin {
$Query = @"
SELECT AJS.DATE
,IL.ServerName
,IL.Environment
,NumberOfJobs
,SuccessfulJobs
,FailedJobs
,DisabledJobs
,UnknownJobs
FROM Info.AgentJobServer AJS
INNER JOIN InstanceList IL ON AJS.InstanceID = IL.InstanceID
WHERE AJS.DATE > DATEADD(Day, - 1, GetDate())
"@
}
process {
$results = Invoke-Sqlcmd -ServerInstance $SqlServer -Database $Database -Query $Query
foreach($result in $results){
$props = @{'Date' = $result.DATE;
'ServerName' = $result.ServerName;
'Environment' = $result.Environment;
'NumberOfJobs' = $result.NumberOfJobs;
'SuccessfulJobs' = $result.SuccessfulJobs;
'FailedJobs' = $result.FailedJobs;
'DisabledJobs' = $result.DisabledJobs;
'UnknownJobs' = $result.UnknownJobs
}
New-Object -TypeName PSObject -Property $props
}
}
end {
}
}
function Get-SQLLowDiskSpace {
[CmdletBinding()]
param (
[string]$SqlServer=$env:ComputerName,
[string]$Database='dbareports'
)
begin {
$Query = @"
SELECT [Date]
,(
SELECT ServerName
FROM dbo.Instancelist
WHERE InstanceID = [DiskSpace].ServerID
) AS ServerName
,[DiskName]
,[Label]
,[Capacity]
,[FreeSpace]
,[Percentage]
FROM [Info].[DiskSpace]
WHERE DATE > DATEADD(Day, - 1, GETDATE())
AND Percentage <= 15
"@
}
process {
$results = Invoke-Sqlcmd -ServerInstance $SqlServer -Database $Database -Query $Query
foreach($result in $results){
$props = @{'Date' = $result.DATE;
'ServerName' = $result.ServerName;
'DiskName' = $result.DiskName;
'Label' = $result.Label;
'Capacity' = $result.Capacity;
'FreeSpace' = $result.FreeSpace;
'Percentage' = $result.Percentage
}
New-Object -TypeName PSObject -Property $props
}
}
end {
}
}
function Get-SQLDatabaseMissingBackup {
[CmdletBinding()]
param (
[string]$SqlServer=$env:ComputerName,
[string]$Database='dbareports'
)
begin {
$Query = @"
DECLARE @CurrDate DATETIME
SET @CurrDate = GETDATE()
SELECT il.[ServerName]
,D.NAME
,CASE
WHEN D.LastBackupDate = '0001-01-01 00:00:00.0000000'
THEN NULL
ELSE D.LastBackupDate
END AS LastBackupDate
,CASE
WHEN D.LastDifferentialBackupDate = '0001-01-01 00:00:00.0000000'
THEN NULL
ELSE D.LastDifferentialBackupDate
END AS LastDifferentialBackupDate
,CASE
WHEN D.LastLogBackupDate = '0001-01-01 00:00:00.0000000'
THEN NULL
ELSE D.LastLogBackupDate
END AS LastLogBackupDate
,(
SELECT MAX(BackupDate)
FROM (
VALUES (D.LastBackupDate)
,(D.LastDifferentialBackupDate)
) AS VALUE(BackupDate)
) AS MaxDate
,DATEDIFF(DAY, (
SELECT MAX(BackupDate)
FROM (
VALUES (D.LastBackupDate)
,(D.LastDifferentialBackupDate)
) AS VALUE(BackupDate)
), @CurrDate) AS DaysSinceBackup
,DATEDIFF(HOUR, (
SELECT MAX(BackupDate)
FROM (
VALUES (D.LastBackupDate)
,(D.LastDifferentialBackupDate)
) AS VALUE(BackupDate)
), @CurrDate) AS HoursinceBackup
,CASE
WHEN (DATEDIFF(HOUR, D.LastBackupDate, @CurrDate) > 24)
AND (DATEDIFF(HOUR, D.LastDifferentialBackupDate, @CurrDate) > 24)
THEN 1
ELSE 0
END AS Olderthan24
FROM [dbo].[InstanceList] il
JOIN [Info].[Databases] D ON IL.InstanceID = D.InstanceID
JOIN [Info].[SQLInfo] SQL ON IL.InstanceID = SQL.InstanceID
WHERE il.Inactive = 0
AND il.NotContactable = 0
AND D.Inactive <> 1
AND D.STATUS = 'Normal'
AND SQL.SQLVersion <> 'SQL 2000'
AND D.IsUpdateable = 1
AND D.NAME != 'tempdb'
AND (
(DATEDIFF(HOUR, D.LastBackupDate, @CurrDate) > 24)
AND (DATEDIFF(HOUR, D.LastDifferentialBackupDate, @CurrDate) > 24)
)
ORDER BY DaysSinceBackup DESC
,HoursinceBackup DESC
"@
}
process {
$results = Invoke-Sqlcmd -ServerInstance $SqlServer -Database $Database -Query $Query
foreach($result in $results){
$props = @{'ServerName' = $result.ServerName;
'Name' = $result.Name;
'LastBackupDate' = $result.LastBackupDate;
'LastDifferentialBackupDate' = $result.LastDifferentialBackupDate;
'LastLogBackupDate' = $result.LastLogBackupDate;
'DaysSinceBackup' = $result.DaysSinceBackup
}
New-Object -TypeName PSObject -Property $props
}
}
end {
}
}
function Get-SQLFastestGrowingDisks {
[CmdletBinding()]
param (
[string]$SqlServer=$env:ComputerName,
[string]$Database='dbareports'
)
begin {
$Query = @"
EXECUTE [dbo].[Get_FastestGrowingDisks]
"@
}
process {
$results = Invoke-Sqlcmd -ServerInstance $SqlServer -Database $Database -Query $Query
foreach($result in $results){
$props = @{'ServerName' = $result.Server;
'DiskName' = $result.DiskName;
'Date' = $result.Date;
'Label' = $result.Label;
'Capacity' = $result.Capacity;
'FreeSpace' = $result.FreeSpace;
'Growth' = $result.Growth;
'Percentage' = $result.Percentage
}
New-Object -TypeName PSObject -Property $props
}
}
end {
}
}