Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Script after install

Tags:

inno-setup

Newbie question: I would like to run a powershell script (.ps1) at the end of the inno-setup install. Can anyone give me a tip on where to put this? I want the user prompted to be asked if he wants to run this script.

Oh yes, what this script does is run netsh.exe to open up a port, the script is clever and it grabs Env:username and Env:userdomain from the current context. Would the context be the admin who is running the setup? or would it be the original user that ran the setup.exe?

like image 881
Dr.YSG Avatar asked Jan 15 '23 20:01

Dr.YSG


2 Answers

Another way is to run the script using the ShellExec from the code.

[Files]
Source: "yourPowershell.ps1"; DestDir: "{app}"; Flags: overwritereadonly replacesameversion promptifolder;

[Tasks]
Name: "runpowershell"; Description: "Do you want to run Powershell script?"

[Code]

procedure CurStepChanged(CurStep: TSetupStep);
var
  ErrorCode: Integer;
  ReturnCode: Boolean;
begin
  if CurStep = ssPostInstall then begin

    if(IsTaskSelected('runpowershell')) then begin
      ExtractTemporaryFile('yourPowershell.ps1');
      ReturnCode := ShellExec('open', '"PowerShell"', ExpandConstant(' -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden  -File "{tmp}\YourPowershell.ps1"'), '', SW_SHOWNORMAL, ewWaitUntilTerminated, ErrorCode);

    if (ReturnCode = False) then
        MsgBox('Message about problem. Error code: ' + IntToStr(ErrorCode) + ' ' + SysErrorMessage(ErrorCode), mbInformation, MB_OK);

  end;
end;
like image 139
Adam Heffelbower Avatar answered Jan 24 '23 11:01

Adam Heffelbower


[Run]
.....; Description: Run Script; Flags: postinstall

(See the help for more details.) By default this will display a checkbox and run under the original user's context (although it depends a bit on how the installer is run).

You might want to reconsider this approach, though; if you are performing a machine-wide install then you should probably open the port machine-wide too. You can do this with pure Inno code calling WinAPIs -- no powershell required. (Which is a good thing, because it might not be installed.)

Alternatively if you want to keep it a per-user setting you should consider making your application prompt the user for a decision on first run. After all, why give an option to only one of the many possible users of your app?

like image 38
Miral Avatar answered Jan 24 '23 11:01

Miral