从{app}执行PowerShell脚本在Inno Setup中失败,而从{tmp}执行执行

问题描述

我的安装程序运行PowerShell脚本以在安装时创建证书。在[Files]部分中,我复制了两个PowerShell脚本(一个用于安装,一个用于卸载):

[Files]
Source: "MyApp\Certs.ps1"; DestDir: "{tmp}\Neogen"; Flags: ignoreversion; \
    Permissions: everyone-full              
Source: "MyApp\UninstallCerts.ps1"; DestDir: "{app}"; Flags: ignoreversion; \
    Permissions: everyone-full

用于安装证书的脚本使用一个临时目录,而卸载脚本使用该应用程序目录,该目录通常将其放入C:\Program Files (x86)\MyApp文件夹中。我可以确认卸载脚本已放入正确的文件夹中。

当我卸载应用程序时,证书没有被卸载。脚本如下所示:

Get-ChildItem Cert:\LocalMachine\My | Where-Object { $_.FriendlyName -match 'MyCert' } | Remove-Item
Get-ChildItem Cert:\LocalMachine\Root | Where-Object { $_.FriendlyName -match 'MyCert' } | Remove-Item

如果我打开具有管理员权限的PowerShell窗口并转到C:\Program Files (x86)\MyApp文件夹以手动运行脚本,则该脚本可以正常工作-证书将被删除。它只是从“添加/删除程序”窗口运行卸载过程,失败了。

我在Inno Setup中的卸载代码如下:

[Uninstallrun]
Filename: "powershell.exe"; \
  Parameters: "-ExecutionPolicy Bypass -File """"{app}\UninstallCerts.ps1 ""{app}"" """""; \
  Flags: runhidden; RunOnceId: "MyAppId"

在卸载时尝试运行此PowerShell脚本时我缺少什么?

以下是执行证书创建脚本(确实运行成功)的语法:

[Run]
Filename: "powershell.exe"; \
  Parameters: "-ExecutionPolicy Bypass -File """"{tmp}\Certs.ps1 ""{commonappdata}"" """""; \
   Flags: runhidden

解决方法

问题可能是{app}包含空格(Program Files ?,而{tmp}没有空格。您的语法无法正确处理带有空格的路径。

这是correct syntax

[Run]
Filename: "powershell.exe"; \
  Parameters: "-ExecutionPolicy Bypass -File ""{tmp}\Certs.ps1"" ""{commonappdata}""";\ 
  Flags: runhidden

[UninstallRun]
Filename: "powershell.exe"; \
  Parameters: "-ExecutionPolicy Bypass -File ""{app}\UninstallCerts.ps1"" ""{app}"""; \
  Flags: runhidden; RunOnceId: "MyAppId"

语法中的"""""是noop。它将""传递给PowerShell,PowerShell视其为空。