Advertisement
Guest User

Untitled

a guest
Sep 30th, 2015
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.71 KB | None | 0 0
  1. #########################################################################################################################
  2. #########################################################################################################################
  3. <#
  4. Description:
  5.  
  6. This script will take a list of HTTPS URLs and check it's certificate for an expiration date.
  7. It will send out an HTML table report with highlited "EXPIRED", "ABOUT TO EXPIRE" and "ERROR"
  8. status messages according to a pre-defined threashold.
  9.  
  10. Usage:
  11.  
  12. Edit the mail server credentials and "minimumCertAgeDays" variable further down.
  13. Run the script normally.
  14.  
  15. Troubleshooting:
  16.  
  17. If you get an error message like: 'Cannot convert value “21/08/2015 23:59:59″ to type “System.DateTime”',
  18. remove lines 184-183.
  19.  
  20. Credit:
  21.  
  22. Coloring tabs:
  23. http://community.spiceworks.com/scripts/show/2450-change-cell-color-in-html-table-with-powershell-set-cellcolor
  24.  
  25. Getting SSL cert expiration dates with PS:
  26. https://iamoffthebus.wordpress.com/2014/02/04/powershell-to-get-remote-websites-ssl-certificate-expiration/
  27.  
  28. Gil Falkovitch for combining everything into a report.
  29. #>
  30.  
  31. #########################################################################################################################
  32. #########################################################################################################################
  33.  
  34. Function Set-CellColor
  35. {
  36.  
  37.  
  38. [CmdletBinding()]
  39. Param (
  40. [Parameter(Mandatory,Position=0)]
  41. [string]$Property,
  42. [Parameter(Mandatory,Position=1)]
  43. [string]$Color,
  44. [Parameter(Mandatory,ValueFromPipeline)]
  45. [Object[]]$InputObject,
  46. [Parameter(Mandatory)]
  47. [string]$Filter,
  48. [switch]$Row
  49. )
  50.  
  51. Begin {
  52. Write-Verbose "$(Get-Date): Function Set-CellColor begins"
  53. If ($Filter)
  54. { If ($Filter.ToUpper().IndexOf($Property.ToUpper()) -ge 0)
  55. { $Filter = $Filter.ToUpper().Replace($Property.ToUpper(),"`$Value")
  56. Try {
  57. [scriptblock]$Filter = [scriptblock]::Create($Filter)
  58. }
  59. Catch {
  60. Write-Warning "$(Get-Date): ""$Filter"" caused an error, stopping script!"
  61. Write-Warning $Error[0]
  62. Exit
  63. }
  64. }
  65. Else
  66. { Write-Warning "Could not locate $Property in the Filter, which is required. Filter: $Filter"
  67. Exit
  68. }
  69. }
  70. }
  71.  
  72. Process {
  73. ForEach ($Line in $InputObject)
  74. { If ($Line.IndexOf("<tr><th") -ge 0)
  75. { Write-Verbose "$(Get-Date): Processing headers..."
  76. $Search = $Line | Select-String -Pattern '<th ?[a-z\-:;"=]*>(.*?)<\/th>' -AllMatches
  77. $Index = 0
  78. ForEach ($Match in $Search.Matches)
  79. { If ($Match.Groups[1].Value -eq $Property)
  80. { Break
  81. }
  82. $Index ++
  83. }
  84. If ($Index -eq $Search.Matches.Count)
  85. { Write-Warning "$(Get-Date): Unable to locate property: $Property in table header"
  86. Exit
  87. }
  88. Write-Verbose "$(Get-Date): $Property column found at index: $Index"
  89. }
  90. If ($Line -match "<tr( style=""background-color:.+?"")?><td")
  91. { $Search = $Line | Select-String -Pattern '<td ?[a-z\-:;"=]*>(.*?)<\/td>' -AllMatches
  92. $Value = $Search.Matches[$Index].Groups[1].Value -as [double]
  93. If (-not $Value)
  94. { $Value = $Search.Matches[$Index].Groups[1].Value
  95. }
  96. If (Invoke-Command $Filter)
  97. { If ($Row)
  98. { Write-Verbose "$(Get-Date): Criteria met! Changing row to $Color..."
  99. If ($Line -match "<tr style=""background-color:(.+?)"">")
  100. { $Line = $Line -replace "<tr style=""background-color:$($Matches[1])","<tr style=""background-color:$Color"
  101. }
  102. Else
  103. { $Line = $Line.Replace("<tr>","<tr style=""background-color:$Color"">")
  104. }
  105. }
  106. Else
  107. { Write-Verbose "$(Get-Date): Criteria met! Changing cell to $Color..."
  108. $Line = $Line.Replace($Search.Matches[$Index].Value,"<td style=""background-color:$Color"">$Value</td>")
  109. }
  110. }
  111. }
  112. Write-Output $Line
  113. }
  114. }
  115.  
  116. End {
  117. Write-Verbose "$(Get-Date): Function Set-CellColor completed"
  118. }
  119. }
  120.  
  121. Function sendemail($subject,$body,$recipients,$smtpUsername,$smtpPassword,$smtpServer,$fromAddress,$useCredentials,$useSSL,$port){
  122.  
  123.  
  124. $secpasswd = ConvertTo-SecureString “$smtpPassword” -AsPlainText -Force
  125. $mycreds = New-Object System.Management.Automation.PSCredential (“$smtpUsername”, $secpasswd)
  126.  
  127.  
  128. if ($useCredentials){
  129.  
  130. if ($useSSL){
  131.  
  132. Send-MailMessage -To "$recipients" -SmtpServer "$smtpServer" -Credential $mycreds -UseSsl -subject "$subject" -Port "587" -Body "$body" -From "$fromAddress" -BodyAsHtml
  133. }
  134. else{
  135.  
  136. Send-MailMessage -To "$recipients" -SmtpServer "$smtpServer" -Credential $mycreds -subject "$subject" -Port "587" -Body "$body" -From "$fromAddress" -BodyAsHtml
  137.  
  138. }
  139.  
  140. }
  141. else{
  142.  
  143. Send-MailMessage -To "$recipients" -SmtpServer "$smtpServer" -subject "$subject" -Port "587" -Body "$body" -From "$fromAddress" -BodyAsHtml
  144. }
  145.  
  146.  
  147.  
  148.  
  149. }
  150.  
  151. function checkURL($url){
  152.  
  153. try {
  154.  
  155. $stuff = "" | select URL,ExpirationDate, DaysLeft, StatusCode, Status
  156.  
  157. $stuff.URL = $url
  158.  
  159. $req = [Net.HttpWebRequest]::Create($url)
  160. $req.Timeout = $timeoutMilliseconds
  161. $res = $req.GetResponse()
  162.  
  163. $stuff.statuscode = [int]$res.statuscode
  164.  
  165. $expiration = $req.ServicePoint.Certificate.GetExpirationDateString()
  166. $a = $expiration
  167. #$expiration=[DateTime]$expiration
  168. $expiration
  169. #$a = $a.Replace(' AM','')
  170. #$a = $a.Replace(' PM','')
  171. $d = [datetime]::ParseExact($a, 'M/d/yyyy h:mm:s tt', $null)
  172. $expiration = $d
  173.  
  174. $stuff.ExpirationDate = $expiration
  175.  
  176. [int]$certExpiresIn = ($expiration - $(get-date)).Days
  177.  
  178. $stuff.DaysLeft = $certExpiresIn
  179.  
  180. if ($certExpiresIn -gt $minimumCertAgeDays){
  181.  
  182. Write-Host Cert for site $url expires in $certExpiresIn days [on $expiration] -f Green
  183. $stuff.status = "VALID"
  184. }
  185.  
  186. else
  187. {
  188. if ( $certExpiresIn -le $minimumCertAgeDays -and $certExpiresIn -ge 1 ) {
  189. $stuff.status = "ABOUT TO EXPIRE!"
  190.  
  191. }
  192.  
  193. else{
  194. $stuff.status = "EXPIRED!"
  195.  
  196. }
  197.  
  198. }
  199.  
  200. $global:certs += $stuff
  201.  
  202. rv expiration
  203. rv certExpiresIn
  204. #rv a
  205. #rv d
  206. rv req
  207.  
  208. }
  209.  
  210. catch {
  211.  
  212.  
  213. Write-host $_
  214. if ( ( $_ | Select-String "Not Found") -ne $null ){
  215.  
  216. $certError = $true
  217. $stuff.Status = "ERROR!"
  218. $stuff.statuscode = 404
  219. $global:certs+= $stuff
  220. }
  221.  
  222. if ( ( $_ | Select-String "Forbidden") -ne $null ){
  223.  
  224. $certError = $true
  225. $stuff.Status = "ERROR!"
  226. $stuff.statuscode = 403
  227. $global:certs+= $stuff
  228. }
  229.  
  230. # rv expiration
  231. #rv certExpiresIn
  232. #rv req
  233.  
  234. }
  235.  
  236.  
  237. }
  238.  
  239. ####################################################################################################
  240. ##################################EDIT THESE########################################################
  241.  
  242. $useCredentials = $true
  243. $useSSL = $true
  244. $smtpUsername = “AKIAJFP2TLXVFCGUSTYQ”
  245. $smtpPassword = “AmriFemd2WlilqWI7g2p2bepvq+Z09pp+S3TLlgS6Uom”
  246. $smtpServer = "email-smtp.eu-west-1.amazonaws.com"
  247. $fromAddress = "SSL Report <sslreport@nayax.com>"
  248. $recipients = "admin@nayax.com"
  249. $port = "587"
  250.  
  251. $minimumCertAgeDays = 30
  252. $timeoutMilliseconds = 10000
  253.  
  254. $urls = @("https://jira.nayax.net:8061","https://jira.nayax.net:8081","https://jira.nayax.net:8091","https://nayaxvend.com","https://my.nayax.com",
  255. "https://beta.nayax.net", "https://m.nayax.net", "https://prepaid.nayax.com","https://gitlab.nayax.net","https://bi.nayax.com/QvAjaxZfc/QvsStatus.aspx",
  256. "https://api.pay.nayax.net/sslcheck.html", "https://static.nayax.com/logo/dually-logo_50.png","https://beta.nayax.net","https://prepaid.nayax.com" )
  257.  
  258. ##################################EDIT THESE########################################################
  259. ####################################################################################################
  260.  
  261.  
  262. $Header = @"
  263. <style>
  264. TABLE {border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
  265. TH {border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color: #6495ED;}
  266. TD {border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
  267. </style>
  268. <title>
  269. Title of my Report
  270. </title>
  271. "@
  272.  
  273.  
  274.  
  275.  
  276.  
  277. $certs = @()
  278.  
  279. #disabling the cert validation check. This is what makes this whole thing work with invalid certs...
  280. #[Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
  281.  
  282. foreach ($url in $urls) { checkURL $url }
  283.  
  284.  
  285.  
  286. $statusError = $false
  287. $statusExpired = $false
  288. $StatusAboutToExpire = $false
  289. $sub = "SUCCESS: All certs are VALID!"
  290.  
  291. foreach ( $stat in $certs.status ){
  292.  
  293. switch ($stat){
  294.  
  295. "ERROR!" { $sub = "CRITICAL: Unable to determine some certs!";
  296. $statusError = $true
  297. ;break; }
  298. "EXPIRED!" { $sub = "CRITICAL: Some certs EXPIRED!";
  299. $statusExpired = $true
  300.  
  301. break; }
  302. "ABOUT TO EXPIRE!" { $sub = "WARNING: Some certs are about to EXPIRE!";
  303. $StatusAboutToExpire = $true
  304. break; }
  305.  
  306. }
  307.  
  308. }
  309.  
  310. $bod = $certs | ConvertTo-Html -Head $Header -Body "<h1>Certificate Report</h1>" -PostContent "Threshold: $minimumCertAgeDays days" | Set-CellColor -Property Status -color green -filter "status -eq 'VALID'"
  311.  
  312. if ($statusAboutToExpire){ $bod = $bod | Set-CellColor -Property status -color yellow -filter "status -eq 'ABOUT TO EXPIRE!'" }
  313. if ($statusError){$bod = $bod | Set-CellColor -Property status -color red -filter "status -eq 'ERROR!'" }
  314. if ($statusExpired){$bod = $bod | Set-CellColor -Row -Property status -color red -filter "status -eq EXPIRED!'" }
  315.  
  316. Write-Host "Sending report..." -f green
  317. sendemail "$sub" "$bod" "$recipients" "$smtpUsername" "$smtpPassword" "$smtpServer" "$fromAddress" $useCredentials $useSSL "$port"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement