Get-IssuedCertificates.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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
function Get-IssuedCertificate { <# .SYNOPSIS Get Issued Certificate data from one or more certificate athorities. .DESCRIPTION Can get various certificate fileds from the Certificate Authority database. Usfull for exporting certificates or checking what is about to expire .PARAMETER ExpireInDays Maximum number of days from now that a certificate will expire. (Default: 21900 = 60 years) Can be a negative numbe to check for recent expirations .PARAMETER CAlocation Certificate Authority location string "computername\CAName" (Default gets location strings from Current Domain) .PARAMETER Properties Fields in the Certificate Authority Database to Export .PARAMETER CertificateTemplateOid Filter on Certificate Template OID (use Get-CertificateTemplateOID) .PARAMETER CommonName Filter by Issued Common Name .EXAMPLE Get-IssuedCertificate -ExpireInDays 14 Gets all Issued Certificates Expireing in the next two weeks .EXAMPLE Get-IssuedCertificate -ExpireInDays -7 Gets all Issued Certificates that Expired last week .EXAMPLE Get-IssuedCertificate -CAlocation CA1\MyCA Gets all Certificates Issued by CA1 .EXAMPLE Get-IssuedCertificate -Properties 'Issued Common Name', 'Certificate Hash' Gets all Issued Certificates and outputs only the Common name and thumbprint .EXAMPLE Get-IssuedCertificate -CommonName S1, S2.contoso.com Gets Certificats issued to S1 and S2.contoso.com .EXAMPLE $DSCCerts = Get-IssuedCertificate -CertificateTemplateOid (Get-CertificateTemplateOID -Name 'DSCTemplate') -Properties 'Issued Common Name', 'Binary Certificate' foreach ($cert in $DSCCerts) { set-content -path "c:\certs\$($cert.'Issued Common Name').cer" -Value $cert.'Binary Certificate' -Encoding Ascii } Get all certificates issued useing the DSCTemplate template and save them to the folder c:\certs named for the Common name of the certificate #> [CmdletBinding()] Param ( # Maximum number of days from now that a certificate will expire. (Default: 21900 = 60 years) [Int] $ExpireInDays = 21900, # Certificate Authority location string "computername\CAName" (Default gets location strings from Current Domain) [String[]] $CAlocation = (Get-CaLocationString), # Fields in the Certificate Authority Database to Export [String[]] $Properties = ( 'Issued Common Name', 'Certificate Expiration Date', 'Certificate Effective Date', 'Certificate Template', #'Issued Email Address', 'Issued Request ID', 'Certificate Hash', #'Request Disposition', 'Request Disposition Message', 'Requester Name', 'Binary Certificate' ), # Filter on Certificate Template OID (use Get-CertificateTemplateOID) [AllowNull()] [String] $CertificateTemplateOid, # Filter by Issued Common Name [AllowNull()] [String] $CommonName, # Credential to with permissions to query ADSC [AllowNull()] [System.Management.Automation.Credential()][PSCredential] $Credential ) #endregion $ScriptBlock = { try { $CaView = New-Object -ComObject CertificateAuthority.View } catch { throw "Unable to create Certificate Authority View. $ENV:COMPUTERNAME Not not have ADSC Installed" } $null = $CaView.OpenConnection($using:Location) $CaView.SetResultColumnCount($using:Properties.Count) #region SetOutput Colum foreach ($item in $using:Properties) { $index = $CaView.GetColumnIndex($false, $item) $CaView.SetResultColumn($index) } #endregion #region Filters $CVR_SEEK_EQ = 1 $CVR_SEEK_LT = 2 $CVR_SEEK_GT = 16 #region filter expiration Date $index = $CaView.GetColumnIndex($false, 'Certificate Expiration Date') $now = Get-Date $expirationdate = $now.AddDays($using:ExpireInDays) if ($using:ExpireInDays -gt 0) { $CaView.SetRestriction($index,$CVR_SEEK_GT,0,$now) $CaView.SetRestriction($index,$CVR_SEEK_LT,0,$expirationdate) } else { $CaView.SetRestriction($index,$CVR_SEEK_LT,0,$now) $CaView.SetRestriction($index,$CVR_SEEK_GT,0,$expirationdate) } #endregion filter expiration date #region Filter Template if ($using:CertificateTemplateOid) { $index = $CaView.GetColumnIndex($false, 'Certificate Template') $CaView.SetRestriction($index,$CVR_SEEK_EQ,0,$using:CertificateTemplateOid) } #endregion #region Filter Issued Common Name if ($using:CommonName) { $index = $CaView.GetColumnIndex($false, 'Issued Common Name') $CaView.SetRestriction($index,$CVR_SEEK_EQ,0,$using:CommonName) } #endregion #region Filter Only issued certificates # 20 - issued certificates $CaView.SetRestriction($CaView.GetColumnIndex($false, 'Request Disposition'),$CVR_SEEK_EQ,0,20) #endregion #endregion #region output each retuned row $CV_OUT_BASE64HEADER = 0 $CV_OUT_BASE64 = 1 $RowObj = $CaView.OpenView() while ($RowObj.Next() -ne -1) { $Cert = New-Object -TypeName PsObject $ColObj = $RowObj.EnumCertViewColumn() $null = $ColObj.Next() do { $displayName = $ColObj.GetDisplayName() # format Binary Certificate in a savable format. if ($displayName -eq 'Binary Certificate') { $Cert | Add-Member -MemberType NoteProperty -Name $displayName -Value $($ColObj.GetValue($CV_OUT_BASE64HEADER)) -Force } else { $Cert | Add-Member -MemberType NoteProperty -Name $displayName -Value $($ColObj.GetValue($CV_OUT_BASE64)) -Force } } until ($ColObj.Next() -eq -1) Clear-Variable -Name ColObj $Cert } #endregion } foreach ($Location in $CAlocation) { $ComputerName = $Location -split '\\' | Select-Object -First 1 $Params = @{ 'ScriptBlock' = $ScriptBlock 'ComputerName' = $ComputerName } if ($Credential) { $Params.Add('Credential',$Credential) } Invoke-Command @Params } } |