0

I'm having a simple PowerShell script which import a certificate:

$certificate_file_name = "C:\Files\Plugins\certi1.cer"
$root_cert_folder_path = 'Cert:\LocalMachine\Root'
try
{
  $certificate_obj = Import-Certificate -FilePath $certificate_file_name -CertStoreLocation $root_cert_folder_path
}
catch
{
  $ErrorMessage = $_.Exception.Message
  write-output $ErrorMessage
}

When running the script manually - it's working fine.

In addition, I have an installshield project that have 1 action item - executing the above script.

After building the installer and running it I get the error `The 'Import-Certificate' command was found in the module 'PKI', but the module could not be loaded. For more information, run 'Import-Module PKI'.

Why does it happens? How can it be solved? `

Vincent K
  • 1,326
  • 12
  • 19
Noam
  • 1,640
  • 4
  • 26
  • 55
  • 1
    I would suggest to get this to work that you try to launch your code from a cmd prompt, passing the command to powershell.exe. I suspect this is what installsheild is doing or something very similar. – Thom Schumacher Dec 14 '17 at 14:18

1 Answers1

0

I found a workaround: Instead of adding my certificate with Powershell, I did it with batch, that made it work both when running manually and when called as part of installation process.

The batch script is super simple:

Certutil -addstore -f "TrustedPublisher" "path\to\cert\file.cer"
Certutil -addstore -f "Root" "path\to\cert\file.cer"

Since Installshield only supports running powershell scripts , I wrapped the batch with powershell that calls it:

cmd.exe /c 'path\to\batch\script\addCertToRootAndPublisher.bat'
Noam
  • 1,640
  • 4
  • 26
  • 55