Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Importing certificate into Root Store using powershell v 1.0

$
0
0

Hello Experts,

I have a requirement to import one .pfx certificate into personal store & .p7b certificate into root store. 

I have accomplished this using import-pfxcertificate & import-certificate in higher version of PS than 1.0

For achieving the same in v 1.0 i have written the below code.

For .pfx into personal store :

$pfx = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2
$pfx.Import("c:\temp\test.pfx","password", "Exportable,PersistKeySet")
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "LocalMachine")
$store.open("MaxAllowed")
$store.add($pfx)
$store.close()

This works fine.

For .p7b into root store :

$certs = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2Collection
$certs.Import("c:\temp\test1.p7b")
$store1 = New-Object System.Security.Cryptography.X509Certificates.X509Store("root", "LocalMachine")
$store1.open("MaxAllowed")
$store1.add($certs)
$store1.close()

This doesnt :( 
Getting an error in line "$store1.add($certs)" as below

Cannot convert argument "certificate", with value: "[Subject]
  CN=XXX, DC=YYY, DC=local
[Issuer]
  CN=XXX, DC=YYY, DC=local
[Serial Number]
  XXXX
[Not Before]
  8/14/2015 5:09:43 PM
[Not After]
  8/14/2025 5:19:43 PM
[Thumbprint]
  XXXXXX
", for "Add" to type "System.Security.Cryptography.X509Certificates.X509Certificate2": "Cannot convert the
"System.Security.Cryptography.X509Certificates.X509Certificate2Collection" value of type
"System.Security.Cryptography.X509Certificates.X509Certificate2Collection" to type
"System.Security.Cryptography.X509Certificates.X509Certificate2"."
At line:1 char:1
+ $store1.add($certs)
+ ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

Please help.

Regards,

Prajul Nambiar


Viewing all articles
Browse latest Browse all 21975

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>