Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #simple CSS Style
- $style = @"
- <style type='text/css'>
- td {border:1px solid gray;}
- .offline{background-color: #E01B1B;}
- </style>
- "@
- #Import SQL Module
- Import-Module SQLPS -DisableNameChecking
- function Get-DBStatus {
- <# [CmdletBinding()]
- param (
- [Parameter(Mandatory=$True)][string]$Servername
- )#>
- $ServerList = Get-Content C:\PowerShell\PD.txt
- Foreach ($serverName in $ServerList){
- #Create SMO Object
- $SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
- foreach ($db in $SQLServer.Databases){
- #Change text to read Online/Offline as opposed to "True" or "False"
- Switch ($db.IsAccessible) {
- "True" {$dbstatus = "Online"}
- "False" {$dbstatus = "Offline"}
- }
- #Properties of function
- $props = @{ 'Server' = $ServerName
- 'DbName' = $db.Name
- 'Status' = $dbstatus}
- New-Object -TypeName PSObject -Property $props
- }
- }
- }
- #let's get content from Get-Service and output this to styled HTML
- Get-DBStatus | ConvertTo-Html -Property Server, DBName, Status -Title "Database Status" -Head $style |
- Foreach {
- #if service is running, use green background
- if ($_ -like "*<td>Offline</td>*")
- {
- $_ -replace "<tr>", "<tr class='offline'>"
- }
- else
- {
- #display normally
- $_
- }
- } |
- Out-File "C:\PowerShell\db_status.html" -force
- #Set-Alias ie "$env:programfiles\Internet Explorer\iexplore.exe"
- #ie "C:\PowerShell\db_status.html"
- Send-MailMessage -to "Email1 <email1@email.com>", "email2 <email2@email.com>" -from "Admin <admin@sql.com>" -Subject "DB Status" -SmtpServer smtp.relay.com -Attachments "C:\powershell\db_status.html" -BodyAsHtml (Get-Content C:\PowerShell\db_status.html | Out-String)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement