forked from proxb/PowerShell_Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-Certificate.ps1
134 lines (117 loc) · 5.02 KB
/
Get-Certificate.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
Function Get-Certificate {
<#
.SYNOPSIS
Retrieves certificates from a local or remote system.
.DESCRIPTION
Retrieves certificates from a local or remote system. Also includes the
time until expiration and allows for filtering of certificates and includes
archived certificates.
.PARAMETER Computername
A single or list of computernames to perform search against
.PARAMETER StoreName
The name of the certificate store name that you want to search
.PARAMETER StoreLocation
The location of the certificate store.
.PARAMETER IncludeArchive
Includes certificates that have been archived
.PARAMETER Issuer
Filter by certificate Issuer
.PARAMETER Subject
Filter by certificate Subject
.PARAMETER Thumbprint
Filter by certificate Thumbprint
.NOTES
Name: Get-Certificate
Author: Boe Prox
Version History:
1.3 //Boe Prox
-Added parameters for filtering
-Removed parametersetnames
-Fixed computername output in verbose streams
1.0 //Boe Prox
-Initial Version
.EXAMPLE
Get-Certificate -Computername 'boe-pc' -StoreName My -StoreLocation LocalMachine
Thumbprint Subject
---------- -------
F29B6CB248E3395B2EB45FCA6EA15005F64F2B4E CN=SomeCert
B93BA840652FB8273CCB1ABD804B2A035AA39877 CN=YetAnotherCert
B1FF5E183E5C4F03559E80B49C2546BBB14CCB18 CN=BOE
65F5A012F0FE3DF8AC6B5D6E07817F05D2DF5104 CN=SomeOtherCert
63BD74490E182A341405B033DFE6768E00ECF21B CN=www.example.com
Description
-----------
Lists all certificates
.EXAMPLE
Get-Certificate -Computername 'boe-pc' -StoreName My -StoreLocation LocalMachine -Subject '*Boe*'
Thumbprint Subject
---------- -------
B1FF5E183E5C4F03559E80B49C2546BBB14CCB18 CN=BOE
Description
-----------
Lists certificates that contain the subject: boe
#>
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias('PSComputername','__Server','IPAddress')]
[string[]]$Computername = $env:COMPUTERNAME,
[parameter()]
[System.Security.Cryptography.X509Certificates.StoreName]$StoreName = 'My',
[parameter()]
[System.Security.Cryptography.X509Certificates.StoreLocation]$StoreLocation = 'LocalMachine',
[parameter()]
[switch]$IncludeArchive,
[parameter()]
[string]$Issuer,
[parameter()]
[string]$Subject,
[parameter()]
[string]$Thumbprint
)
Begin {
$WhereList = New-Object System.Collections.ArrayList
If ($PSBoundParameters.ContainsKey('Issuer')) {
[void]$WhereList.Add('$_.Issuer -LIKE $Issuer')
}
If ($PSBoundParameters.ContainsKey('Subject')) {
[void]$WhereList.Add('$_.Subject -LIKE $Subject')
}
If ($PSBoundParameters.ContainsKey('Thumbprint')) {
[void]$WhereList.Add('$_.Thumbprint -LIKE $Thumbprint')
}
If ($WhereList.count -gt 0) {
$Where = [scriptblock]::Create($WhereList -join ' -AND ')
Write-Debug "WhereBlock: $($Where)"
}
}
Process {
ForEach ($Computer in $Computername) {
Try {
Write-Verbose ("Connecting to \\{0}\{1}\{2}" -f $Computer,$StoreLocation,$StoreName)
$CertStore = New-Object System.Security.Cryptography.X509Certificates.X509Store -ArgumentList "\\$($Computer)\$($StoreName)", $StoreLocation
If ($PSBoundParameters.ContainsKey('IncludeArchive')) {
$Flags = [System.Security.Cryptography.X509Certificates.OpenFlags]'ReadOnly','IncludeArchived'
} Else {
$Flags = [System.Security.Cryptography.X509Certificates.OpenFlags]'ReadOnly'
}
$CertStore.Open($Flags)
If ($WhereList.count -gt 0) {
$Certificates = $CertStore.Certificates | Where $Where
} Else {
$Certificates = $CertStore.Certificates
}
$Certificates | ForEach {
$Days = Switch ((New-TimeSpan -End $_.NotAfter).Days) {
{$_ -gt 0} {$_}
Default {'Expired'}
}
$_ | Add-Member -MemberType NoteProperty -Name ExpiresIn -Value $Days -PassThru |
Add-Member -MemberType NoteProperty -Name Computername -Value $Computer -PassThru
}
} Catch {
Write-Warning "$($Computer): $_"
}
}
}
}