- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get website SSL certificate validity dates with PowerShell?
SSL certificates are a very crucial part of the website. They play a key role in securing the exchange of information on both client and server sides by activating an HTTPS secure connection. In the below article with the PowerShell, we will get the certificate validity date (starting and expiry date) for the certificate using PowerShell.
To achieve this, we need to make httpwebrequest but before that, we will ignore SSL warning by the below command.
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
And then we wil make the HTTP web request by calling a .Net class.
$url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url)
When we check the $req there are few properties displayed but as we are interested in the certificate date we will use the specific property ServicePoint to retrieve the related information.
$req.ServicePoint
The output of the above command.
PS C:\WINDOWS\system32> $req.ServicePoint BindIPEndPointDelegate : ConnectionLeaseTimeout : -1 Address : https://www.microsoft.com/ MaxIdleTime : 100000 UseNagleAlgorithm : True ReceiveBufferSize : -1 Expect100Continue : True IdleSince : 23-06-2020 07:02:36 ProtocolVersion : 1.1 ConnectionName : https ConnectionLimit : 2 CurrentConnections : 0 Certificate : ClientCertificate : SupportsPipelining : True
As you can see in the above property that certificate field is null so to retrieve the information, we need to use GetResponse() method.
$req.GetResponse()
The output of the above command.
IsMutuallyAuthenticated : False Cookies : {} Headers : {Pragma, X-Activity-Id, MS-CV, X-AppVersion...} SupportsHeaders : True ContentLength : -1 ContentEncoding : ContentType : text/html; charset=utf-8 CharacterSet : utf-8 Server : LastModified : 23-06-2020 07:06:44 StatusCode : OK StatusDescription : OK ProtocolVersion : 1.1 ResponseUri : https://www.microsoft.com/en-in/ Method : GET IsFromCache : False
Now, we will run the previous command and check if we can retrieve the certificate information.
PS C:\WINDOWS\system32> $req.ServicePoint BindIPEndPointDelegate : ConnectionLeaseTimeout : -1 Address : https://www.microsoft.com/en-in/ MaxIdleTime : 100000 UseNagleAlgorithm : True ReceiveBufferSize : -1 Expect100Continue : True IdleSince : 23-06-2020 07:06:44 ProtocolVersion : 1.1 ConnectionName : https ConnectionLimit : 2 CurrentConnections : 1 Certificate : System.Security.Cryptography.X509Certificates.X509Cer tificate ClientCertificate : SupportsPipelining : True
Yes, we could retrieve the certificate information. You can use try/catch block in case the GetResponse() command throws an exception and that I will be using it in the final script. But for this moment, we are interested in retrieving certificate dates.
$req.ServicePoint.Certificate
You will see the output as shown below.
In the above output still, the dates are missing, so we will check if there are any properties or methods to retrieve dates. We will check the properties and methods available for the Date.
$req.ServicePoint.Certificate | gm | where{$_.Name -like "*Date*"} TypeName: System.Security.Cryptography.X509Certificates.X509Certificate Name MemberType Definition ---- ---------- ---------- GetEffectiveDateString Method string GetEffectiveDateString() GetExpirationDateString Method string GetExpirationDateString()
Here we have both the methods to get the Expiration and Effective start date.
Start Date −
PS C:\WINDOWS\system32> $req.ServicePoint.Certificate.GetEffectiveDateString() 24-06-2019 06:25:35
End Date −
PS C:\WINDOWS\system32> $req.ServicePoint.Certificate.GetExpirationDateString() 22-10-2021 03:34:04
The entire script is mentioned as below.
[Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } $url = "https://www.microsoft.com/" $req = [Net.HttpWebRequest]::Create($url) $req.GetResponse() | Out-Null $output = [PSCustomObject]@{ URL = $url 'Cert Start Date' = $req.ServicePoint.Certificate.GetEffectiveDateString() 'Cert End Date' = $req.ServicePoint.Certificate.GetExpirationDateString() } $output URL Cert Start Date Cert End Date --- --------------- ------------- https://www.microsoft.com/ 26-06-2019 09:10:38 22-10-2021 03:34:04
- Related Articles
- How to get the Windows certificate details using PowerShell?
- How to handle SSL certificate error using Selenium WebDriver?
- nginx ssl certificate installation in linux
- How to install a certificate to the certificate store using PowerShell?
- How to get website links using Invoke-WebRequest in PowerShell?
- How does Selenium Webdriver handle SSL certificate in Firefox?
- How does Selenium Webdriver handle SSL certificate in Chrome?
- How to Export a certificate from a certificate store using PowerShell?
- How to retrieve certificate thumbprint using PowerShell?
- How to get the certificate's start and expiry date using PowerShell?
- How does Selenium Webdriver handle the SSL certificate in Safari?
- How does Selenium Webdriver handle the SSL certificate in Edge?
- What is an SSL certificate and how does it work?
- How to delete the windows certificate using PowerShell?
- How to take care of SSL certificate issues in chrome browser in Selenium?
