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 ?
Problem when creating an .exe file for multiple ps1
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.
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.
Re: Problem when creating an .exe file for multiple ps1
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:
To change the project file's properties, select the file in the Project panel and use the Properties panel to change the Build setting.
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
David
SAPIEN Technologies, Inc.
SAPIEN Technologies, Inc.
Re: Problem when creating an .exe file for multiple ps1
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.
SAPIEN Technologies, Inc.
Re: Problem when creating an .exe file for multiple ps1
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
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
Re: Problem when creating an .exe file for multiple ps1
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.
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.
SAPIEN Technologies, Inc.
Re: Problem when creating an .exe file for multiple ps1
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')
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')