PowerShell Script: Database Health Check Email

PowerShell Script: Database Health Check Email

# Mail Server information
$SMTPServer = "smtp.gmail.com"
$SMTPPort = "587"
$UserName = "ramesh30.m@gmail.com"
$Password = "Gmail application security password here"

# Adding email addresses
$to = "ramesh30.m@gmail.com"
$from = "ramesh30.m@gmail.com"
$cc = "rameshmamillapalli@outlook.com"

$subject = "Database Report | $(Get-Date)"

# HTML styling for report
$head = @" 
<style>
    h1 { background-color: aqua; display: block; width: fit-content; margin-left:auto; margin-right:auto; font-size: 15px; }
    h2 { background-color: aqua; display: block; width: fit-content; margin-left:auto; margin-right:auto; padding:5px; }
    table { border-width:1px; border-style:double; border-color:beige; border-collapse:collapse; border:1px solid black; padding:5px 10px; margin-left:auto; margin-right:auto; }
    th { background-color: grey; font-style: normal; padding:5px 10px; border:1px solid black; }
    tr { background-color: chocolate; border:1px solid black; padding:5px; }
    td { border:1px solid black; padding:5px 10px; }
    tr:nth-child(even) { background-color: darkgray; }
    tr:nth-child(odd) { background-color: aqua; }
</style>
"@

$report = "DatabaseHealthCheckReport.html"
$filepath = "D:\MAPS_DEV\$report"

# Delete existing report if it exists
if(Test-Path $filepath) {
    Remove-Item $filepath -Force
}

$instances = @("MAPS\PROD", "MAPS\DEV")
$instances | ForEach-Object {
    $indInstance = $_
    $body = "

Database Health Check Report of $($indInstance) dated on $(Get-Date)

"
Get-DbaDatabase -SqlInstance $indInstance | ConvertTo-Html -Property SQLInstance, Name, Status, RecoveryModel, Encrypted -Body $body -Head $head | Out-File -FilePath $filepath -Append } # Sending Email cls $message = New-Object System.Net.Mail.MailMessage $message.Subject = $subject $message.To.Add($to) $message.CC.Add($cc) $message.Body = Get-Content -Path $filepath $message.IsBodyHtml = $true $message.From = $from # SMTP client $smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort) $SMTP.EnableSsl = $true $smtp.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password) $smtp.Send($mess

Comments

Popular posts from this blog

Always On FailOver Events

System.Data.SqlClient.SqlException (0x80131904): Execution Timeout Expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

The transaction log for database is full due to 'OLDEST_PAGE'