forked from mmessano/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathVerify-SQLBackups.ps1
134 lines (116 loc) · 5 KB
/
Verify-SQLBackups.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
### Code that can be used to Monitor all you Sql Instances Backups from One Location
#Create a new Excel object using COM
$ErrorActionPreference = "silentlycontinue"
$Excel = New-Object -ComObject Excel.Application
$Excel.visible = $False
$Excel.DisplayAlerts = $false
$ExcelWorkbooks = $Excel.Workbooks.Add()
$Sheet = $ExcelWorkbooks.Worksheets.Item(1)
#$MonitorBody = "D:\PowerShell\PScripts\Mail.htm"
#$date = get-date -uformat "%Y%m%d"
$date = ( get-date ).ToString('yyyy/MM/dd')
$save = "E:\Dexma\Logs\DatabaseBackup_Report.xls"
#Counter variable for rows
$intRow = 1
#Read the contents of the Servers.txt file
#foreach ($instance in get-content "serverlist.txt")
##################Loop in all your sqlserver instances#########################
foreach ($instance in get-content "\\xmonitor11\Dexma\Data\ServerLists\SMC_IMP.txt")
{
#Create column headers
$Sheet.Cells.Item($intRow,1) = "INSTANCE NAME:"
$Sheet.Cells.Item($intRow,2) = $instance
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,2).Font.Bold = $True
$intRow++
$Sheet.Cells.Item($intRow,1) = "DATABASE NAME"
$Sheet.Cells.Item($intRow,2) = "LAST FULL BACKUP"
$Sheet.Cells.Item($intRow,3) = "LAST LOG BACKUP"
$Sheet.Cells.Item($intRow,4) = "FULL BACKUP AGE(DAYS)"
$Sheet.Cells.Item($intRow,5) = "LOG BACKUP AGE(HOURS)"
#Format the column headers
for ($col = 1; $col -le 5; $col++)
{
$Sheet.Cells.Item($intRow,$col).Font.Bold = $True
$Sheet.Cells.Item($intRow,$col).Interior.ColorIndex = 50
$Sheet.Cells.Item($intRow,$col).Font.ColorIndex = 36
}
$intRow++
#######################################################
#This script gets SQL Server database information using PowerShell
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
# Create an SMO connection to the instance
$s = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $instance
$dbs = $s.Databases
#Formatting using Excel
ForEach ($db in $dbs)
{
if ($db.Name -ne "tempdb") #We do not need the backup information for the tempdb database
{
#We use Date Math to extract the number of days since the last full backup
$NumDaysSinceLastFullBackup = ((Get-Date) - $db.LastBackupDate).Days
#Here we use TotalHours to extract the total number of hours
$NumDaysSinceLastLogBackup = ((Get-Date) - $db.LastLogBackupDate).TotalHours
if($db.LastBackupDate -eq "1/1/2005 12:00 AM")
#This date is a start of Sqlserver infra.
#This is the default dateTime value for databases that have not had any backups
{
$fullBackupDate="Never been backed up"
$fgColor3="red"
}
else
{
$fullBackupDate="{0:g}" -f $db.LastBackupDate
}
$Sheet.Cells.Item($intRow, 1) = $db.Name
$Sheet.Cells.Item($intRow, 2) = $fullBackupDate
$fgColor3="green"
#Use the .ToString() Method to convert the value of the Recovery model to string and ignore Log #backups for databases with Simple recovery model
if ($db.RecoveryModel.Tostring() -eq "SIMPLE")
{
$logBackupDate="N/A"
$NumDaysSinceLastLogBackup="N/A"
}
else
{
if($db.LastLogBackupDate -eq "1/1/2011 12:00 AM")
{
$logBackupDate="Never been backed up"
}
else
{
$logBackupDate= "{0:g2}" -f $db.LastLogBackupDate
}
}
$Sheet.Cells.Item($intRow, 3) = $logBackupDate
#Define your service-level agreement in terms of days here.
if ($NumDaysSinceLastFullBackup -gt 0)
{
$fgColor = 3
}
else
{
$fgColor = 50
}
$Sheet.Cells.Item($intRow, 4) = $NumDaysSinceLastFullBackup
$Sheet.Cells.item($intRow, 4).Interior.ColorIndex = $fgColor
$Sheet.Cells.Item($intRow, 5) = $NumDaysSinceLastLogBackup
$intRow ++
}
}
$intRow ++
}
$Sheet.UsedRange.EntireColumn.AutoFit()
$ExcelWorkbooks.SaveAs($save)
$Excel.quit()
CLS
######Send Email with excel sheet as a attachment#######
$mail = New-Object System.Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($save)
$mail.From = "[email protected]"
$mail.To.Add("[email protected]")
$mail.Subject = "Database Backup Report for all SQL servers on $date "
$mail.Body = "This mail gives us detailed information for all the database backups which are scheduled to run every day. Please review the attached Excel report every day and fix the failed backups which are marked in Red and make sure the Full Backup Age(DAYS) is Zero."
$mail.Attachments.Add($att)
$smtp = New-Object System.Net.Mail.SmtpClient("outbound.smtp.dexma.com")
$smtp.Send($mail)