Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. #simple CSS Style
  2. $style = @"
  3. <style type='text/css'>
  4. td {border:1px solid gray;}
  5. .offline{background-color: #E01B1B;}
  6. </style>
  7. "@
  8.  
  9. #Import SQL Module
  10. Import-Module SQLPS -DisableNameChecking
  11.  
  12. function Get-DBStatus {
  13. <# [CmdletBinding()]
  14. param (
  15. [Parameter(Mandatory=$True)][string]$Servername
  16. )#>
  17.  
  18. $ServerList = Get-Content C:\PowerShell\PD.txt
  19.  
  20. Foreach ($serverName in $ServerList){
  21.  
  22. #Create SMO Object
  23. $SQLServer = New-Object ('Microsoft.SqlServer.Management.Smo.Server') $ServerName
  24.  
  25.  
  26.  
  27. foreach ($db in $SQLServer.Databases){
  28.  
  29. #Change text to read Online/Offline as opposed to "True" or "False"
  30. Switch ($db.IsAccessible) {
  31. "True" {$dbstatus = "Online"}
  32. "False" {$dbstatus = "Offline"}
  33. }
  34. #Properties of function
  35. $props = @{ 'Server' = $ServerName
  36. 'DbName' = $db.Name
  37. 'Status' = $dbstatus}
  38. New-Object -TypeName PSObject -Property $props
  39. }
  40.  
  41.  
  42. }
  43.  
  44. }
  45.  
  46. #let's get content from Get-Service and output this to styled HTML
  47. Get-DBStatus | ConvertTo-Html -Property Server, DBName, Status -Title "Database Status" -Head $style |
  48.  
  49. Foreach {
  50. #if service is running, use green background
  51. if ($_ -like "*<td>Offline</td>*")
  52. {
  53. $_ -replace "<tr>", "<tr class='offline'>"
  54. }
  55. else
  56. {
  57. #display normally
  58. $_
  59. }
  60. } |
  61. Out-File "C:\PowerShell\db_status.html" -force
  62. #Set-Alias ie "$env:programfiles\Internet Explorer\iexplore.exe"
  63. #ie "C:\PowerShell\db_status.html"
  64. 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