-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-SSLCertificate.ps1
48 lines (41 loc) · 1.54 KB
/
Get-SSLCertificate.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
#Forked from https://github.com/JustinGrote/Scripts/blob/master/Get-SSLCertificate.ps1
Function Get-SSLCertificate {
[CmdletBinding()]
param(
[parameter(Mandatory,ValueFromPipeline)][string[]]$ComputerName,
[int]$Port=443,
[int]$Timeoutms=3000
)
Process {
ForEach ($Computer in $ComputerName) {
Write-Verbose "$Computer`: Connecting on port $Port"
[Net.ServicePointManager]::ServerCertificateValidationCallback = {$True}
$Req = [Net.HttpWebRequest]::Create("https://$Computer`:$Port/")
$Req.KeepAlive = $False
$Req.Timeout = $Timeoutms
Try {
$Req.GetResponse() | Out-Null
} Catch {
Write-Error "Couldn't connect to $Computer on port $Port - $($Error[0].Exception.Message)"
Continue
}
If (!($Req.ServicePoint.Certificate)) {
Write-Error "No Certificate returned from $Computer"
Continue
}
$CertInfo = $Req.ServicePoint.Certificate
$Returnobj = [ordered]@{
ComputerName = $Computer
Port = $Port
Subject = $CertInfo.Subject
Thumbprint = $CertInfo.GetCertHashString()
Issuer = $Certinfo.Issuer
SerialNumber = $Certinfo.GetSerialNumberString()
Issued = [DateTime]$Certinfo.GetEffectiveDateString()
Expires = [DateTime]$Certinfo.GetExpirationDateString();
DaysTilExp = New-TimeSpan $(Get-Date) $([DateTime]$Certinfo.GetExpirationDateString();) | Select-Object -ExpandProperty Days
}
New-Object PSCustomObject -Property $Returnobj
}
}
} #End Get-SSLCertificate