PowerShellで証明書の中身データ（不規則な文字列）からthumbprint（サムプリント・指紋）有効期限・発行者などを取り出す

# どんな人向けか
証明書のテキストファイルから証明書の発行者情報、サムプリント、有効期限などをPowerShellコマンドで取得したい方向け
具体的には
以下の画像の
-----BEGIN CERTIFICATE-----から-----BEGIN CERTIFICATE-----までにある不規則な文字列からサムプリント、有効期限、発行者情報などをpowershellで取得する。

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3373520/63b8c0a2-cafe-1215-6de4-d0d78f9070b6.png)
画像は以下から引用

https://jp.globalsign.com/support/ssl/confinfo/openssl.html
# プログラム
```text:.今回使うクライアント証明書(kingoftime)
-----BEGIN CERTIFICATE-----
MIIF0TCCBLmgAwIBAgIQB8CRaQmqZ3HvzF6aPKOGtDANBgkqhkiG9w0BAQsFADA8
MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRwwGgYDVQQDExNBbWF6b24g
UlNBIDIwNDggTTAyMB4XDTIzMDIwNDAwMDAwMFoXDTI0MDMwNDIzNTk1OVowHjEc
MBoGA1UEAxMTczMudGEua2luZ29mdGltZS5qcDCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAOr8NWQIqXM58QSCvSQ8cNhTbe83/VbCbyXryf+InmvQcpzY
dPbZmImfavQ/cDQIFshlF+1myXlzR4wiTiPj1lUXb3PuMS00U7Gud6MplWLRfoiR
86uFQiEeK5lDejvwqnAbdOuWCYY4FPn9pSVBbZIgh0e8SQMQXkKFeMlp2/0oCI1u
y651Bip34up789YKC/X9a0T7XdpOM2jls/uDVvQAARw8RJUI8H6vXexN0vuXOGrY
Yt1SZ5OF30uofVJXteVJG98smZv2BaFETBgwZ8RvdusKDBjqmw8j0NRDMlXluNl1
8qjg1tozzCGY7qeSaibiYgrKMRmr1yjrun0Gp2UCAwEAAaOCAuswggLnMB8GA1Ud
IwQYMBaAFMAxUs1aUMOCfHRxzsvpnPl664LiMB0GA1UdDgQWBBRX7/nThcDZot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xyp6aqDd4nrcYO/s9rGPI31/nbacBTbS/5E1PI
1Nklppy6cGzyCTF+ITCuxjEIO3MSfvOMeYC8fx6oTiNEmyzFcwRAYh0Q8h30StUX
H3n5N6ouvuIvHDAd1rc5ejo4av8akESfFrkzR+8dUv5BvGbxtTn1nq/Mr3L4X6Hh
KE07s+ZGf8qfqER4PVdmdQtOjTaZZobf8ucbW5qGPEtE8pTVnn73vvylY/qPrGpk
BvkppYk=
-----END CERTIFICATE-----
```
```powershell:main.ps1
# 証明書の文字列データ
$Settingcertraw = 'MIIF0TCCBLmgAwIBAgIQB8CRaQmqZ3HvzF6aPKOGtDANBgkqhkiG9w0BAQsFADA8
MQswCQYDVQQGEwJVUzEPMA0GA1UEChMGQW1hem9uMRwwGgYDVQQDExNBbWF6b24g
UlNBIDIwNDggTTAyMB4XDTIzMDIwNDAwMDAwMFoXDTI0MDMwNDIzNTk1OVowHjEc
MBoGA1UEAxMTczMudGEua2luZ29mdGltZS5qcDCCASIwDQYJKoZIhvcNAQEBBQAD
ggEPADCCAQoCggEBAOr8NWQIqXM58QSCvSQ8cNhTbe83/VbCbyXryf+InmvQcpzY
dPbZmImfavQ/cDQIFshlF+1myXlzR4wiTiPj1lUXb3PuMS00U7Gud6MplWLRfoiR
86uFQiEeK5lDejvwqnAbdOuWCYY4FPn9pSVBbZIgh0e8SQMQXkKFeMlp2/0oCI1u
y651Bip34up789YKC/X9a0T7XdpOM2jls/uDVvQAARw8RJUI8H6vXexN0vuXOGrY
Yt1SZ5OF30uofVJXteVJG98smZv2BaFETBgwZ8RvdusKDBjqmw8j0NRDMlXluNl1
8qjg1tozzCGY7qeSaibiYgrKMRmr1yjrun0Gp2UCAwEAAaOCAuswggLnMB8GA1Ud
IwQYMBaAFMAxUs1aUMOCfHRxzsvpnPl664LiMB0GA1UdDgQWBBRX7/nThcDZotuc
b3e74WJEyJT5NzAeBgNVHREEFzAVghNzMy50YS5raW5nb2Z0aW1lLmpwMA4GA1Ud
DwEB/wQEAwI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TiNEmyzFcwRAYh0Q8h30StUX
H3n5N6ouvuIvHDAd1rc5ejo4av8akESfFrkzR+8dUv5BvGbxtTn1nq/Mr3L4X6Hh
KE07s+ZGf8qfqER4PVdmdQtOjTaZZobf8ucbW5qGPEtE8pTVnn73vvylY/qPrGpk
BvkppYk='

$Settingpfx= New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$Settingpfx.Import([System.Convert]::FromBase64String($Settingcertraw),$null,[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable)

foreach($pfx in $Settingpfx){
    $pfx.Subject# 証明書の発行者情報
    $pfx.Thumbprint # 証明書のサムプリント
    $pfx.NotAfter # 証明書の有効期限
}
```
```powershell:実行結果
CN=s3.ta.kingoftime.jp
2F13CE7702B39565BE8043DC332CDAEBC6F4ADD4

```
※今回はクライアント側の証明書なので有効期限は記述されてません。サーバ側の証明書は有効期限が出ます。
ほかにも証明書情報を取り出せそうなので、デバッグ機能などで確かめてみてください。
# 参考文献
https://github.com/isshukla/AppGW-Certs/blob/master/AppGW-V1-V2-Cert-Export.ps1

https://www.powershellgallery.com/packages/GetAppGWCerts/2.3