2016-10-02 45 views
-2

我需要使用PersistKeySet和Exportable選項導入p12證書集合。只獲得一個選項。 [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::"Exportable, PersistKeySet")不起作用。如何正確地做到這一點?使用PersistKeySet和Exportable選項導入p12收集證書

我的功能:

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" 
    } 
} 
+0

的回答你的問題已經在你的代碼。請仔細看看「MachineKeySet」和「PersistKeySet」標記是如何組合的。 –

回答

0

只是刪除雙冒號和類型標誌引號:

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

它現在有效。非常感謝你的幫助 –