Skip to content

Instantly share code, notes, and snippets.

@mardahl
Created July 25, 2020 14:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mardahl/b2a3b0f74e6fd73ab0878085ed1ecc70 to your computer and use it in GitHub Desktop.
Save mardahl/b2a3b0f74e6fd73ab0878085ed1ecc70 to your computer and use it in GitHub Desktop.
Function to verify that a certificate by specific template exists, using powershell
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