【问题标题】:Can't load Powershell module when executing from installer从安装程序执行时无法加载 Powershell 模块
【发布时间】:2017-12-17 16:09:50
【问题描述】:

我有一个导入证书的简单 PowerShell 脚本:

$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
}

手动运行脚本时 - 它工作正常。

此外,我有一个 installshield 项目,它有 1 个操作项 - 执行上述脚本。

构建安装程序并运行它后,我收到错误“在模块“PKI”中找到“导入证书”命令,但无法加载该模块。如需更多信息,请运行“Import-Module PKI”。

为什么会这样?如何解决? `

【问题讨论】:

  • 我建议您尝试从 cmd 提示符启动您的代码,将命令传递给 powershell.exe,以使其正常工作。我怀疑这就是 installsheild 正在做的事情或类似的事情。

标签: powershell windows-installer installshield


【解决方案1】:

我找到了一个解决方法: 我没有使用 Powershell 添加我的证书,而是使用批处理来完成,这使它在手动运行和作为安装过程的一部分调用时都可以工作。

批处理脚本超级简单:

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

由于 Installshield 只支持运行 powershell 脚本,我用调用它的 powershell 包装了批处理:

cmd.exe /c 'path\to\batch\script\addCertToRootAndPublisher.bat'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2015-11-05
    • 1970-01-01
    相关资源
    最近更新 更多