Powershell 使用PersistKeySet和Exportable选项导入p12收集证书

Powershell 使用PersistKeySet和Exportable选项导入p12收集证书,powershell,x509certificate,p12,Powershell,X509certificate,P12,我需要使用PersistKeySet和Exportable选项导入p12证书集合。只能使用一个选项。 [System.Security.Cryptography.X509Certificates.x509keystrageflags]::“可导出,持久化密钥集”)不起作用。如何正确地做 我的职能: function ImportEASCert($strCertPath, $strCertPass) { $fOk = Test-Path "$strCertPath" if ($fO

我需要使用PersistKeySet和Exportable选项导入p12证书集合。只能使用一个选项。
[System.Security.Cryptography.X509Certificates.x509keystrageflags]::“可导出,持久化密钥集”)
不起作用。如何正确地做

我的职能:

function ImportEASCert($strCertPath, $strCertPass)
{
    $fOk = Test-Path "$strCertPath"
    if ($fOk)
    {
        $bytes = [System.IO.File]::ReadAllBytes($strCertPath)
        $cert  = New-Object system.security.cryptography.x509certificates.X509Certificate2Collection
        $store = New-Object system.security.cryptography.X509Certificates.X509Store "My", "CurrentUser"

        try
        {
            $cert.Import($bytes, $strCertPass, [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet)
            $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)

            foreach ($D in $cert)
            {
                $store.Add($d)
                $d.SerialNumber
            }

            $store.Close()
        }
        catch
        {
            return "0"
        }
    }
    else
    {
        return "0"
    }
}

只需删除双冒号并在引号中键入标志:

[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]"Exportable, PersistKeySet"

您问题的答案已在代码中。请仔细查看
MachineKeySet
PersistKeySet
标志是如何组合的。