Page 1 of 2

Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 8:32 am
by MaureenC
Product, version and build: PowerShell Studio 2019 version 5.6.267 (64 bit vsn)
Operating system: Windows 10 64bit

I want to run powershell scripts to make changes to a target system. I know I can bundle them in an EXE file or in an MSI file.

I know I can use the Packager to create the exe file containing my powershell script, which will execute when I double click it on the target system for the simple patches/updates.

I also want to use the MSI capability, because it allows me to create an Uninstall script for situations when I need to uninstall.

My issue: I can't seem to get a powershell script to be executed in the MSI unless I bundle it as an EXE or possibly deliver a VBS script that will execute it. Is there a way to just deliver a Powershell script that will be executed when I double click the MSI file?

I tried playing around with the Custom actions section to just add an Install Action for my powershell script, but had no luck. I am going to try adding a VBS script to the MSI file which will execute my PS1 this afternoon.

I appreciate any suggestions.

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 9:00 am
by mxtrinidad
Can you please provide more information on what errors are you getting?
It will help us understand what's going on wrong.

In the meantime, have you looked at the following article:
https://www.sapien.com/blog/2019/08/15/ ... xecutable/

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 10:10 am
by MaureenC
No errors, my script is just not getting executed.
1. P1.ps1 script writes "hello world" to Windows Event Viewer; successfully runs in debugger

2. Create P1.msi in Powershell Studio Installer function; P1.ps1 is added to MSI via the Files/Folders tab

3. Build P1.msi in PowerShell Studio
4. Double clicking P1.msi file does NOT write "hello world" in Event Viewer
5. However, P1 does show up in the Add/Remove programs

How can I get "Hello World" to be printed by double clicking the MSI file.

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 10:51 am
by MaureenC
The blog below has a set of directions to define a different script engine in the Custom Actions field, but it does not tell you how to specify this engine.

Does anyone know how to specify Wscript.exe in Custom Actions?

https://www.sapien.com/blog/2018/02/26/ ... i-builder/

"For any other type of script you will need to specify the entire command, including what script engine you want to use (e.g., WScript.exe or JScript.exe for Windows Script Host.)
If you package your custom action scripts as executable files, you need to add the .exe file and reference that rather than the script."

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 12:05 pm
by mxtrinidad
If you don't want to package the PowerShell Script then you need to create a shortcut pointing to the file to execute.

MSI installer will only install the files in the system with the optional ability to use "Custom Actions" to execute additional commands during the different stages of the installer: Install, Uninstall, and Maintenance.

Before building the MSI installer, you need to create the shortcut script which will point to the location of the file to execute.
All this need to be included in the "Files and Folders" area of the MSI settings.

The blog post I pasted previously shows the steps of including a shortcut script file which could be modified to your need.

In the case of using WScript.exe and JScript.exe, you'll probably need to create a cmd line script file (*.bat, *.cmd). These files will be added to the appropriate steps in the MSI Settings.

At the end the shortcut could run the cmd line script: which could be:
PowerShell.exe c:\...\Myscripts\Script1.ps1 ...
Wscript.exe c:\...\MyScripts\Script.vbs ...
JScript.exe c:\...\MyScripts\Script3.js ...

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 12:29 pm
by MaureenC
I was under the impression that the shortcuts got added to the Start Menu which I do not want.

Can I prohibit the shortcut being added to Start Menu? I guess I could delete it in my install script, but that feels problematic.

It could be I'm thinking of an older version of the tool, as I have read a lot of blogs today.

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 1:49 pm
by mxtrinidad
No need to add another script. Just select "(No Shortcut)" in the MSI "Files/Folder - Create Shortcut to" section.
This will be enough to not to create the Shortcut on the Start Menu.
NoShortcut_01_2019-10-16_16-44-36.png
NoShortcut_01_2019-10-16_16-44-36.png (38.8 KiB) Viewed 1266 times

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 2:22 pm
by Alexander Riedel
This thread has gotten way off track. You originally wanted to use a PowerShell Script as a custom action.

"I tried playing around with the Custom actions section to just add an Install Action for my powershell script, but had no luck."

That would be the exact location to do that. Can you elaborate on the "no luck" part? I have never seen PowerShell or MSI output a "No Luck" message box. Albeit some are close :D
Jokes aside, this might be helpful: https://www.sapien.com/blog/2018/02/26/ ... si-builder

It would help if you would show the configuration you have for your custom action as a screenshot.

As for using WScript, you just specify the path to WScript.exe and the script to execute in arguments. I am probably missing where the problem is, so please feel free to elaborate.

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 3:19 pm
by MaureenC
I have included the trivial Hello world scripts and a screen shot of the Custom Actions screen. The other screens had no real changes. I can build the MSI file and when I double click it I don't get Hello world in the text file.
I can see my HelloWorld product in my Apps and Features, so part of it works. It just doesn't run the Install script custom action.

Re: Can I get a Powershell script to execute in MSI file

Posted: Wed Oct 16, 2019 4:06 pm
by Alexander Riedel
Change it to deferred. Immediately will try to execute the script when the MSI is launched, at which point the script hasn't even been copied yet.