Problem when creating an .exe file for multiple ps1

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
lorry57
Posts: 3
Last visit: Fri Sep 29, 2023 7:54 am

Problem when creating an .exe file for multiple ps1

Post by lorry57 »

Product, version and build: powershell studio 2017 v 5.4.145 64 bits

I want to create an .exe containing main.ps1 calling sub.ps1. I created an empty project called MultiLevel and add the files main.ps1 and sub.ps1

startup.pss contains :
#TODO: Add initialization script here (Load modules and check requirements)
& .\main.ps1

main.ps1 contains :
[void][System.Windows.Forms.MessageBox]::Show('program main.ps1', 'Press a key')
& .\sub.ps1
[void][System.Windows.Forms.MessageBox]::Show('Back to program Main.ps1', 'Press a key')

sub.ps1 contains :
[void][System.Windows.Forms.MessageBox]::Show('Program Sub.ps1' , 'Press a key')

When I run the project from the GUI, all is OK. If I build an .exe (option "resolve and include external scripts" checked) it seems to be OK :

>> Package 'C:\Users\cf1256\Documents\SAPIEN\PowerShell Studio\Projects\multilevel\multilevel.psproj'
>> Building (multilevel) Project...
>> Merging 'Startup.pss' ...
>> Merging 'Sub.ps1' ...
>> Merging 'Main.ps1' ...
>> Writing 'multilevel.Package.ps1' ...
SAPIEN Package and Deploy Tool 4.1 (c) 2005 - 2017 SAPIEN Technologies, Inc.

------ Build started: multilevel, Configuration: x64 ------
Packaging with SAPIEN PowerShell V2 Host (Command line) x64
Adding C:\Users\cf1256\Documents\SAPIEN\PowerShell Studio\Projects\multilevel\multilevel.Package.ps1
Writing scripts to bin\x64\multilevel.exe
Embedding default manifest...
Package completed

When I execute Multilevel.exe , sub.ps1 is not recognized and I get the error message
The term '.\sub.ps1' is not recognized as the name of a cmdlet

Can you help me ?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Problem when creating an .exe file for multiple ps1

Post by davidc »

We will need to investigate this on our end.

In the meantime, have you tried merging the files by using the Build setting within PowerShell Studio?

If you set the project file's Build property to Include, you can invoke the ps1 contents as a function:

Code: Select all

Invoke-Sub_ps1
To change the project file's properties, select the file in the Project panel and use the Properties panel to change the Build setting.
David
SAPIEN Technologies, Inc.
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Problem when creating an .exe file for multiple ps1

Post by davidc »

The packager will not resolve the & invokes. Instead try using dot sourcing and see if that resolves the issue:

Code: Select all

. .\Subs.ps1
David
SAPIEN Technologies, Inc.
User avatar
lorry57
Posts: 3
Last visit: Fri Sep 29, 2023 7:54 am

Re: Problem when creating an .exe file for multiple ps1

Post by lorry57 »

The files property "Build" is set to Include

Invoke-Sub_ps1 runs Ok in the GUI and with the .exe .

. .\sub.ps1 runs Ok in the GUI but do nothing in the .exe

My concern is that I would keep native Powershell command in my scripts and use dot notation instead of custom command from PowershellStudio. My goal is to use it to compile but let the source code accessible and usable to anyone without Studio installed. Any idea ?

Thanks in advance
Laurent
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Problem when creating an .exe file for multiple ps1

Post by davidc »

If you have the property set to Include, then just invoke the reference function.

For the executable, you have to make sure the dot sourced script is in the same directory as the executable. Usually when you package a script, it is created in a "Bin" sub-folder and not at the root folder.
David
SAPIEN Technologies, Inc.
User avatar
lorry57
Posts: 3
Last visit: Fri Sep 29, 2023 7:54 am

Re: Problem when creating an .exe file for multiple ps1

Post by lorry57 »

Here is the trick I found to run my programs as .ps1 or .exe

Startup.pss:
#TODO: Add initialization script here (Load modules and check requirements)
Invoke-Main_ps1

Main.ps1 :
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Windows.Forms.MessageBox]::Show( ('program main.ps1') , 'Press a key')

if ($hostinvocation -eq $null) {
.\sub.ps1
} else {
Invoke-Sub_ps1
}
[void][System.Windows.Forms.MessageBox]::Show('Back to program Main.ps1', 'Press a key')

Sub.ps1 :
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Windows.Forms.MessageBox]::Show('Program Sub.ps1' , 'Press a key')
This topic is 6 years and 2 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.