Skip to content

Instantly share code, notes, and snippets.

@kuan51
Last active April 6, 2021 23:49
Show Gist options
  • Save kuan51/ffd59dae032712460bec5c8050397b4f to your computer and use it in GitHub Desktop.
Save kuan51/ffd59dae032712460bec5c8050397b4f to your computer and use it in GitHub Desktop.
Download all certificates from a Digicert account and save to current users downloads folder
# Set to TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;
# Create Headers variable for API call
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-DC-DEVKEY", 'api_key_here')
# Create array containing all order #'s
$ordersList = Invoke-RestMethod 'https://www.digicert.com/services/v2/order/certificate' -Headers $headers
$orderNumbers = @()
foreach ($order in $ordersList.orders) {
$orderNumbers+=$order.id
}
# Loop through all orders and get array of cert id's.
$reissueCertIds = @()
$duplicateCertIds = @()
foreach ($order in $orderNumbers) {
# Get all reissue cert id's
$reissueList = Invoke-RestMethod "https://www.digicert.com/services/v2/order/certificate/$order/reissue" -Headers $headers
foreach ($certId in $reissueList.certificates) {
$reissueCertIds += $certId
}
# Get all duplicate cert id's
$duplicateList = Invoke-RestMethod "https://www.digicert.com/services/v2/order/certificate/$order/duplicate" -Headers $headers
foreach ($certId in $duplicateList.certificates) {
$duplicateCertIds += $certId
}
}
# Combine certIds to a single array
$certIds = $reissueCertIds + $duplicateCertIds
# Download certificates for all cert ids
foreach ($cid in $certIds){
$url = "https://www.digicert.com/services/v2/certificate/" + $cid.id + "/download/format/pem_all"
$filename = $cid.common_name + "_" + $cid.id + ".txt"
if ($filename.Contains('*')){
$wildcardFilename = $filename -replace [System.Text.RegularExpressions.Regex]::Escape('*'),'star'
Invoke-RestMethod $url -Headers $headers | Out-File "$env:USERPROFILE\Downloads\$wildcardFilename"
}
else{
Invoke-RestMethod $url -Headers $headers | Out-File "$env:USERPROFILE\Downloads\$filename"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment