-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcertvalidator.ps1
62 lines (48 loc) · 2.51 KB
/
certvalidator.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# PowerShell-based X.509 Certificate Expiration Tool
param (
[string]$file = $(throw "-File parameter is required")
)
$remoteURLList = get-content -path $file
$remoteCertificateList = @()
foreach ($remoteURL in $remoteURLList) {
# Define our Socket Object
$socket = new-object Net.Sockets.TcpClient
# Define the Output object of the information we want.
$remotecertificateOutput = new-object -typename PSObject
add-member -InputObject $remotecertificateOutput -MemberType NoteProperty -name Host -value $null
add-member -InputObject $remotecertificateOutput -MemberType NoteProperty -name Subject -value $null
add-member -InputObject $remotecertificateOutput -MemberType Noteproperty -name ExpireDays -value $null
Try {
$socket.connect($remoteURL,443)
$sslStream = new-object Net.Security.SslStream($socket.GetStream(),$false)
$sslStream.AuthenticateAsClient($remoteURL)
$remoteCertificate = $sslStream.RemoteCertificate
$remoteCertExpirationDateTime = [DateTime]$remoteCertificate.GetExpirationDateString()
$remoteCertExpirationLength = new-timespan -start (Get-Date) -End $remoteCertExpirationDateTime
$remotecertificateOutput.Host = $remoteURL
$remotecertificateOutput.Subject = $remoteCertificate.Subject
$remotecertificateOutput.ExpireDays = $remoteCertExpirationLength.Days
}
Catch [System.Security.Authentication.AuthenticationException] {
# This will occur if the certificate is invalid.
$remotecertificateOutput.Host = $remoteURL
$remotecertificateOutput.Subject = "Invalid Certificate"
$remotecertificateOutput.ExpireDays = "N/A"
}
Catch [System.Net.Sockets.SocketException] {
# Unknown host exception
$remotecertificateOutput.Host = "Unknown Host"
$remotecertificateOutput.Subject = $null
$remotecertificateOutput.ExpireDays = $null
}
#$htmlStyle = "<Style>"
#$htmlStyle = $htmlStyle + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
#$htmlStyle = $htmlStyle + "TH{border-width: 1px;padding: 10px;border-style: solid;border-color: black}"
#$htmlStyle = $htmlStyle + "TD{border-width: 1px;padding: 10px;border-style: solid;border-color: black}"
#$htmlStyle = $htmlStyle + "</style>"
#$certificateOutput | convertto-html -head $htmlStyle | o
$remoteCertificateList += $remotecertificateOutput
$sslStream.close()
$socket.Close()
}
$remotecertificateList | fl *