How to import a certificate using powershell

We create a script to automatically generate a certificate. We can go through all the steps, but when we try to import the certificate into the store, we have problems. Certificate installed in WSUS -> Certificate Store , but the private key is not associated. If we do it manually, everything will be fine.

The following code represents the section for importing a certificate into the store:

 $cert = new-object system.security.cryptography.x509certificates.x509certificate2 c:\lup.crt $store = New-Object System.Security.Cryptography.X509Certificates.X509Store WSUS, LocalMachine $store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite) $store.Add($cert) $store.Close() 

I am still on the powershell learning curve, and adding certificates made this difficult. What am I doing wrong that prevents the private key from binding when the certificate is imported into the wsus repository?


UPDATE

So, I updated my code with what Neossian suggested, and it works. However, I noticed that it does not remove the secret key from Enrollment Requests . I can delete it manually, but why it wasn’t deleted, as it would be if I manually imported the certificate?

+4
source share
1 answer

Try to run:

 certutil -repairstore WSUS "SerialNumber" 

Where "SerialNumber" is the serial number of the imported certificate.

+2
source

All Articles