Advertisement
Guest User

Untitled

a guest
Feb 8th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.74 KB | None | 0 0
  1. function get-ExpiringCerts ($duedays=45,$CAlocation="Server\SubCA1") {
  2. $certs = @()
  3. $now = get-Date;
  4. $expirationdate = $now.AddDays($duedays)
  5. $CaView = New-Object -Com CertificateAuthority.View.1
  6. [void]$CaView.OpenConnection($CAlocation)
  7. $CaView.SetResultColumnCount(6)
  8. $index0 = $CaView.GetColumnIndex($false, "Issued Common Name")
  9. $index1 = $CaView.GetColumnIndex($false, "Certificate Expiration Date")
  10. $index2 = $CaView.GetColumnIndex($false, "Issued Email Address")
  11. $index3 = $CaView.GetColumnIndex($false, "Certificate Template")
  12. $index4 = $CaView.GetColumnIndex($false, "Request Disposition")
  13. $index5 = $CaView.GetColumnIndex($false, "Request ID")
  14. $index0, $index1, $index2, $index3, $index4, $index5 | %{$CAView.SetResultColumn($_) }
  15. $report = $null
  16. $report = @()
  17. $head = @'
  18. <Title>AI-SUBCA1 Certificate Expiration Report</Title>
  19. <style>
  20. body
  21. {
  22. background-color:#FFFFFF;
  23. font-family:Tahoma;
  24. font-size:10pt;
  25. }
  26. td, th
  27. {
  28. border:1px solid black;
  29.  
  30. }
  31. th
  32. {
  33. color:white;
  34. background-color:#015294;
  35. }
  36. table, tr, td, th {padding: 1px; border-width: 1px; margin: 1px
  37. </style>
  38. '@
  39.  
  40. # CVR_SORT_NONE 0
  41. # CVR_SEEK_EQ 1
  42. # CVR_SEEK_LT 2
  43. # CVR_SEEK_GT 16
  44.  
  45.  
  46. $index1 = $CaView.GetColumnIndex($false, "Certificate Expiration Date")
  47. $CAView.SetRestriction($index1,16,0,$now)
  48. $CAView.SetRestriction($index1,2,0,$expirationdate)
  49.  
  50. # brief disposition code explanation:
  51. # 9 - pending for approval
  52. # 15 - CA certificate renewal
  53. # 16 - CA certificate chain
  54. # 20 - issued certificates
  55. # 21 - revoked certificates
  56. # all other - failed requests
  57. $CAView.SetRestriction($index4,1,0,20)
  58.  
  59. $RowObj= $CAView.OpenView()
  60.  
  61.  
  62. $report += "<H3>Certificate Expiration Status</H3>"
  63. $report += "<br>"
  64. $report += "The following certificates are expiring in the next 45 days:"
  65. $report += "<br><br><style>
  66. table {
  67. border: 1px solid black;
  68.  
  69.  
  70. }
  71. </style><table><tr>
  72. <th>Request ID</th>
  73. <th>Common Name</th>
  74. <th>Days until Expiration</th>
  75. <th>Expiration</th>
  76. </tr>
  77. "
  78.  
  79.  
  80. while ($Rowobj.Next() -ne -1){
  81. $Cert = New-Object PsObject
  82. $ColObj = $RowObj.EnumCertViewColumn()
  83. [void]$ColObj.Next()
  84. do {
  85. $current = $ColObj.GetName()
  86. $Cert | Add-Member -MemberType NoteProperty $($ColObj.GetDisplayName()) -Value $($ColObj.GetValue(1)) -Force
  87. } until ($ColObj.Next() -eq -1)
  88. Clear-Variable ColObj
  89. $datediff = New-TimeSpan -Start ($now) -End ($cert."Certificate Expiration Date")
  90.  
  91.  
  92. $report += "<tr #bgcolor><td>" + $cert."Request ID" + "</td><td>" + $cert."Issued Common Name" + "</td><td>" + $dateDiff.Days + "</td><td>" + $cert."Certificate Expiration Date" +"</td></tr>"
  93. #"Send email to : " + $cert."Issued Email Address"
  94.  
  95. if(($dateDiff.Days -le 35) -and ($dateDiff.Days -ge 15)){
  96. $report = $report -replace "#bgcolor","bgcolor='MediumOrchid'"
  97. }
  98.  
  99. if(($dateDiff.Days -le 15) -and ($dateDiff.Days -ge 2)){
  100. $report = $report -replace "#bgcolor","bgcolor='khaki'"
  101. }
  102.  
  103. if($dateDiff.Days -le 1){
  104. $report = $report -replace "#bgcolor","bgcolor='IndianRed'"
  105. }
  106.  
  107. }
  108. $RowObj.Reset()
  109. $CaView = $null
  110. [GC]::Collect()
  111. $bodynocerts += "<H3>Certificate Status</H3><br><br>There are no cerficates expiring within the next 45 days<br><br> <br>__________________________________________________________________________________________________________<br>This message was sent by an automated system. If you believe you received this in error, or you have any questions or concerns, please contact the NIAID OEB Server Development Team at email Thank you.
  112. E-mail sent from $($ENV:Computername)."
  113. $report += "</table><br> <br> <br>__________________________________________________________________________________________________________<br>This message was sent by an automated system. If you believe you received this in error, or you have any questions or concerns, please contact the NIAID OEB Server Development Team at emali Thank you.
  114. E-mail sent from $($ENV:Computername)."
  115.  
  116. $body = @"
  117. $Report
  118. "@
  119.  
  120. $MessageBody = ConvertTo-Html -head $head -Body $body
  121. $MessageNoCerts = ConvertTo-Html -head $head -Body $bodynocerts
  122.  
  123.  
  124. $recipient = "@email"
  125.  
  126. $fromemail = $Env:COMPUTERNAME + "@niaid.nih.gov"
  127. $server = "post.niaid.nih.gov"
  128. if($cert."Request ID" -eq $null)
  129. {
  130. send-mailmessage -from $fromemail -to $recipient -subject "Certificate Expiration Report" -BodyAsHTML -body ($MessageNoCerts | Out-String) -smtpServer $server
  131. }
  132. else{
  133. send-mailmessage -from $fromemail -to $recipient -subject "Certificate Expiration Report" -BodyAsHTML -body ($MessageBody | Out-String) -smtpServer $server
  134. }
  135.  
  136.  
  137.  
  138.  
  139.  
  140. }
  141.  
  142. get-ExpiringCerts -duedays 45 -CAlocation "SUBCA"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement