Created
July 25, 2020 14:41
-
-
Save mardahl/b2a3b0f74e6fd73ab0878085ed1ecc70 to your computer and use it in GitHub Desktop.
Function to verify that a certificate by specific template exists, using powershell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function VerifyCertificateExists($issuer, $templateName){ | |
#function that validates existence of required certificate. | |
#input issuer and template name (wildcards will automatically be added). | |
#example : VerifyCertificateExists -issuer "myCompany" -templateName "myCertTemplate" | |
#MIT license - github.com/mardahl | |
try { | |
Write-Verbose "Verifying existence of required certificate..." -Verbose | |
$certs = Get-ChildItem -Path cert:\currentuser\my | Where-Object Issuer -like "*$issuer*" -ErrorAction Stop | |
$cert = $certs | Where-Object{ $_.Extensions | Where-Object{ ($_.Oid.Value -eq '1.3.6.1.4.1.311.21.7') -and ($_.Format(0) -like "*$templateName*") }} -ErrorAction Stop | |
if(!$cert){throw} | |
Write-Verbose "Required certificate found!" -Verbose | |
} catch { | |
Write-Error "Missing certificate issued with the `"$templateName`" certificate template from the issuer `"$issuer!`"" | |
exit 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment