ps/Modules/Alkami.DevOps.Certificates/Private/Get-Cert.ps1
2023-05-30 22:51:22 -07:00

36 lines
1.1 KiB
PowerShell

function Get-Cert {
<#
.SYNOPSIS
Fetches a Certificate.
#>
[CmdletBinding()]
param(
[string]$Thumbprint,
[string]$StoreName,
[string]$FriendlyName
)
$certStore = @{ }
$certStores = Get-ChildItem Cert:\LocalMachine\ | ForEach-Object { "Cert:\LocalMachine\$($_.Name)" }
if ($StoreName) {
$certStores = $certStores | Where-Object { $_ -Match "\\$StoreName" }
}
foreach ($store in $certStores) {
Get-ChildItem $store | Where-Object { $_.NotAfter -gt (Get-Date) } | ForEach-Object {
if ($certStore.ContainsKey($_.Thumbprint)) {
$certStore[$_.Thumbprint].Add($_)
} else {
$list = [System.Collections.Generic.List[System.Security.Cryptography.X509Certificates.X509Certificate]]::new()
$list.Add($_)
$certStore.Add($_.Thumbprint, $list)
}
}
}
if ($Thumbprint) {
return $certStore[$Thumbprint]
} elseif ($FriendlyName) {
return $certStore.Values | Where-Object { $_.FriendlyName -match $FriendlyName }
}
return $certStore.Values
}