4

We are creating a script to automatically create a certificate. We are able to get through all of the stages but when we try to import the certificate into the store we are having issues. The certificate does get installed into the WSUS -> Certificate Store but the private key is not associated. If we do it manually it works just fine.

The following code represents the section to import the 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 learning curve of powershell and the addition of certificates has made this difficult. What am I doing wrong that does not allow the private key to be associated when the certificate is imported to the wsus store?


UPDATE

So I have updated my code with what Neossian has suggested and it does work. However, I noticed it does not delete the private key out of Enrollment Requests I can delete this manually but why is it not deleted as it would be if I were to import the certificate manually?

1 Answer 1

3

Try running:

certutil -repairstore WSUS "SerialNumber"

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

Sign up to request clarification or add additional context in comments.

2 Comments

That worked, but do you know why the repair needs to be run??? If it needs to be run every single time the code kicks off I will have to find a way to grab the serial number of the certificate so that command can be run.
This doesn't seem like it should work, but it does... Where does it get the private key from?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.