0

In test.ps1: Remove-Item -Path  "E:\temp\123.txt" -Force -ErrorAction Continue

In example.iss:

#define path "..\functions\test.ps1"
;#define path "E:\source\functions\test.ps1"

[Code]
var 
    ResultCode : Integer;
function PrepareToInstall(var NeedsRestart: Boolean): String;
    begin
        Exec('cmd', '/C powershell.exe -ExecutionPolicy bypass -File "{#path}"', '', SW_SHOW, 
                ewWaitUntilTerminated, ResultCode);
    end

The example.iss file is in E:\source\installer. The powershell task is simple, which works fine in command line with both full path and the relative path. However in Inno setup, only the full path works (123.txt gets removed). It didn't work with the relative path.

I tried to place test.ps1 in the current folder with .\test.ps1, it didn't work either. The output folder(where the install exe was) was also tried, no luck as well.

Why it only works with the full path E:\source\functions\test.ps1?

Wayne
  • 1
  • 1
  • It sounds like the working directory for the `cmd` process isn't what you expect it to be - you'll have to pass the desired one via the 3rd `Exec()` argument, such as to `'{app}'` for the application folder and `'{src}'` for the folder with the setup files - see https://jrsoftware.org/ishelp/index.php?topic=consts – mklement0 Sep 20 '21 at 21:44
  • 1
    Thanks @mklement0. just tried, if the full path is in the 3rd argument, it works. However it still didn't work with a relative path. I also tried using a variable: wk := ExpandConstant('{src}'); Insert('\test.ps1', wk, 200); Exec('cmd', '/C powershell.exe -ExecutionPolicy bypass -File "test.ps1"', '{wk}', SW_SHOW, ewWaitUntilTerminated, ResultCode); – Wayne Sep 20 '21 at 23:12
  • It looks when the path is relative or a variable, it didn't work to run the powershell task. – Wayne Sep 20 '21 at 23:20
  • It looks like you are expecting `..\functions\test.ps1` to get included in the installer. Re-evaluate that expectation and look in the documentation for how to include files that you only want to run at installation time. Also you don't need a PowerShell script to delete file(s) (Inno Setup has the `[InstallDelete]` section for that). – Bill_Stewart Sep 21 '21 at 21:51
  • Thanks @Bill_Stewartm that removing file task in the ps1 was just a test. I was trying to get a ps1 working. You are right. Eventually we need to include the ps1 script in the installer and run at install time. I think I've got it now. Thanks. – Wayne Sep 24 '21 at 01:36

0 Answers0