Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.78 KB | None | 0 0
  1. <#
  2. .SYNOPSIS
  3. Emails a machine status report
  4. .DESCRIPTION
  5. Generates an HTML email with machine status (online/offline) and uptime
  6. .PARAMETER ComputerName
  7. The name of the Computer(s) you want to run the command against.
  8. .EXAMPLE
  9. Get-ServerDown -ComputerName Computername1
  10. .EXAMPLE
  11. Get-ServerDown -ComputerName Computername1,Computername2
  12. #>
  13.  
  14. param
  15. (
  16. [array]$ComputerName = "zepper,sql10,ad10",
  17. [switch]$OfflineOnly = $false
  18. )
  19.  
  20. $username = 'dataitpowershell'
  21. $password = convertto-securestring -String "Datait2016!" -AsPlainText -Force
  22.  
  23. function Get-ComputerUptime {
  24. param ($ComputerName)
  25.  
  26. $pc = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $ComputerName
  27. $booted = $pc.ConvertToDateTime($pc.LastBootUpTime)
  28. $uptime = (Get-Date) - $booted
  29.  
  30. Return [String]::Format('{0:00} Days, {1:00} Hours, {2:00} Minutes, {3:00} Seconds', $uptime.Days, $uptime.Hours, $uptime.Minutes, $uptime.Seconds)
  31. }
  32.  
  33. function Send-GmailMessage {
  34. param (
  35. [Parameter(Mandatory = $true)]
  36. [String]$Recipient,
  37. [String]$From = "dataitpowershell@gmail.com",
  38. [String]$Subject,
  39. [String]$Body
  40. )
  41.  
  42. #splatting parameters
  43. $param = @{
  44. SmtpServer = 'smtp.gmail.com'
  45. Port = 587
  46. UseSsl = $true
  47. Credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $password
  48. From = $From
  49. To = $Recipient
  50. Subject = $Subject
  51. Body = $Body
  52. }
  53.  
  54. Send-MailMessage @param
  55. }
  56.  
  57. # loop through all computers in parameter
  58. $results = ForEach ($computer in $ComputerName)
  59. {
  60. [PSCustomObject]@{
  61. Computer = $computer
  62. Online = Test-Connection $computer -Quiet -Count 1
  63. Uptime = Get-ComputerUpTime $computer
  64. }
  65. Write-host $ComputerName
  66. }
  67.  
  68. $header = @"
  69. <style type='text/css'>
  70. table {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  71. th {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
  72. td {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  73. </style>
  74. "@
  75.  
  76. # generate html
  77. if ($OfflineOnly)
  78. {
  79. $HTML = $results | Where-Object Online -eq $false | ConvertTo-Html -Head $header -PreContent "<h3>Server Status Report</h3>" -PostContent "<p><br/><h5>Generated: $(Get-Date)</</p>" | Out-String
  80. }
  81. else
  82. {
  83. $HTML = $results | ConvertTo-Html -Head $header -PreContent "<h3>Server Status Report</h3>" -PostContent "<p><br/><h6>Generated: $(Get-Date)</p>" | Out-String
  84. }
  85.  
  86.  
  87. # email html report
  88. if ($results.count -gt 0)
  89. {
  90. Send-GmailMessage -Recipient mrbilde@gmail.com `
  91. -Subject "Server Status Report" `
  92. -Body $HTML
  93. }
  94.  
  95. Get-ComputerUptime
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement