Reputation: 83
I am trying to import certificates and CA to store using certutil.exe in PowerShell. This is my script:
$appDir="C:\Program Files\My App"
$certFilespec = $appDir + "\mycert.pfx"
certutil -p '""' -importPFX $certFilespec
$certFilespec = $appDir + "\myca.crt"
certutil -f -addStore Root $certFilespec
Everything but third line executes successfully. The error is:
PS C:\> certutil -p '""' -importPFX $certFilespec
CertUtil: -importPFX command FAILED: 0x80070002 (WIN32: 2)
CertUtil: The system cannot find the file specified.
PS C:\>
When I use string instead of $certFilespec
certutil -p '""' -importPFX "C:\Program Files\My App\mycert.pfx"
certutil -f -addStore Root "C:\Program Files\My App\myca.crt"
everything executes successfully. What I also found out is that when I use relative paths it works fine
PS C:\> cd '.\Program Files\My App'
$certFilespec=".\mycert.pfx"
certutil -p '""' -importPFX $certFilespec
CertUtil: -importPFX command completed successfully
PS C:\Program Files\My App>
everything works fine. So I guess there is some problem with quoting when absolute path is used. What I don't understand is how come that it works differently for the same command just different options (-addStore/-importPFX).
The files I am importing are PKCS12 certificate + private key (the .pfx file). And certificate of CA (the .crt file). But that shouldn't play any role.
Upvotes: 2
Views: 1690
Reputation: 11
Try modifying this line:
certutil -p '""' -importPFX $certFilespec
to
certutil -p '""' -importPFX "$certFilespec"
Since you have a space in your path, it's breaking the single path parameter into multiple parameters.
Upvotes: 1
Reputation: 16
Best guess. When it is producing the $certFilespec it is generating the string without comments, so the command will see C:\Program and kick out an error.
$appDir="C:\Program Files\My App"
$certFilespec = $appDir + "\mycert.pfx"
$certFilespec
C:\Program Files\My App\mycert.pfx
You could try
$appDir='"C:\Program Files\My App\'
$certFilespec = $appDir + 'mycert.pfx"'
when run produces
$certFilespec
"C:\Program Files\My App\mycert.pfx"
Upvotes: 0