[英]upload X509 certificate using powershell - error 'Cannot find the requested object'
[英]Retrieve x509 Certificate 'Description' property using Powershell
我試圖拉回Windows證書的description屬性。 它不是標准的x509證書屬性。
我發現的唯一參考是使用capicom( 如何使用powershell訪問證書擴展屬性? ),現在不支持該方法,並且無論如何也無法幫助我,因為我將在遠程運行它。
有人知道其他任何訪問此屬性的方法嗎?
謝謝
好吧,在發布之時,任何評論均不正確或不相關。 描述不是X.509證書對象的一部分,它是特定於供應商的(在當前情況下為Microsoft)附加屬性。 該屬性是通過證書存儲附加的,在其外部不存在。
既不是PowerShell也不是.NET提供了從證書讀取存儲附加屬性的本地方法(盡管,諸如友好名稱之類的某些東西可用)。 相反,您需要通過p / invoke調用CertGetCertificateContextProperty非托管函數:
$Cert = gi Cert:\CurrentUser\My\510F2809B505D9B32F167F6E71001B429CE801B8
$signature = @"
[DllImport("Crypt32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern bool CertGetCertificateContextProperty(
IntPtr pCertContext,
uint dwPropId,
Byte[] pvData,
ref uint pcbData
);
"@
Add-Type -MemberDefinition $signature -Namespace PKI -Name Crypt32
$pcbData = 0
# if the function returns False, then description is not specified.
$CERT_DESCRIPTION_PROP_ID = 13
if ([PKI.Crypt32]::CertGetCertificateContextProperty($Cert.Handle,$CERT_DESCRIPTION_PROP_ID,$null,[ref]$pcbData)) {
# allocate a buffer to store property value
$pvData = New-Object byte[] -ArgumentList $pcbData
# call the function again to write actual data into allocated buffer
[void][PKI.Crypt32]::CertGetCertificateContextProperty($Cert.Handle,$CERT_DESCRIPTION_PROP_ID,$pvData,[ref]$pcbData)
# Description is null-terminated unicode string
$description = [Text.Encoding]::Unicode.GetString($pvData).TrimEnd()
}
Write-Host $description
將第一行更改為用於檢索證書的行。 證書對象必須存儲在$cert
變量中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.