Documentation on Packager and Modules

Use this forum to ask questions after your subscription maintenance expires or before you buy. Need information on licensing or pricing? Questions about a trial version? This is the right place for you. No scripting questions, please.
Forum rules
DO NOT POST SUBSCRIPTION NUMBERS, LICENSE 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.
This topic is 6 years and 1 month 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
JCRUIZ
Posts: 4
Last visit: Fri May 01, 2020 6:32 am

Documentation on Packager and Modules

Post by JCRUIZ »

Product, version and build : PowerShell Studio 2018
32 or 64 bit version of product : 64 bits
Operating system : Windows 10
32 or 64 bit OS : 64 bit

Sorry if this is a FAQ, I've not been able to find anything on the blog.
I'm evaluating PowerShell Studio 2018 for my company, especially the functionality to package (Script Packager ?), and especially modules.
I've started with a very simple script which loads one of my test modules :

Write-Host "This is a test - MAINTESTCOMPILE.ps1"
Import-Module "C:\1\TestModule.PSM1"
FunctionInModule
Write-Host "Ended"

The module C:\1\testmodule.psm1 has just a test : write-host "something from the module"

This of course works. I setup a project with both files, configure Script Packager settings to "Resolve and Include External Scripts", and package everything. The EXE runs, and all write-hosts are displayed.
But now I remove the module from its original location. When running the script, I get the standard error "I cannot find the module C:\1\testmodule.psm1"... which means it was not embedded in the EXE file.

My questions :
1. Where's the official documentation on this ? I don't like to post in blogs without having read the official docs.
2. Can this be done ? I'd like to embed some (many) of my custom modules with a main.ps1 in a single EXE.
3. Is it possible to select which modules to embed ? I'd like to package my own modules, but leaving out some standard MS modules as AzureRM or AzureAD.

Many thanks in advance,
jc











*** Please add details and screenshots as needed below. ***

DO NOT POST SUBSCRIPTIONS, KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Documentation on Packager and Modules

Post by Alexander Riedel »

This will be a bit of a longer answer. There is no documentation on including modules in executables because, well, it doesn't make sense to do that.
I apologize if that sounds a bit harsh :D but bear with me. A PowerShell module is basically the same as a DLL in principle. It contains code that is to be shared between a number of scripts, a single source of shared functionality which does not need to debugged again and again.
Embedding a module within an exe, if that were possible, would negate that shared aspect of the module.
Additionally an executable file is not some kind of container where you embed all sorts of auxiliary files in, like a zip file. Its a file that contains code that gets executed when you launch the file as a process. That's its purpose.

The answer to your most likely next question of "But how do I get my modules to the target machine then?" depends on what you actually want to do.
Do you want all your code to be in a monolithic exe or do you want to keep a module structure for better code management?
If you just want all your code included in the exe, dot source .PS1 files at the proper locations to reference the code in those files and select "Resolve and include external scripts" in the output settings.

If you want to keep using modules, create an installer for your application with PowerShell Studio and include your main exe file as well as your own custom modules. Once installed on a target machine, you can load your modules using your executable's path as a reference with a path name.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
JCRUIZ
Posts: 4
Last visit: Fri May 01, 2020 6:32 am

Re: Documentation on Packager and Modules

Post by JCRUIZ »

Thanks Alex,

Just to set the expectations, I know this is difficult by mail/post.
I know what a PowerShell module is, and understand all the implications for packaging and distribution. Distribution is not the issue, but obfuscating the code.

What's new for me is the functionality you provide to package a PS1 inside an EXE file (I know it's not compiled) to protect the source code (minimally, I also know it can be reverse-engineered). But at least provide an initial deterrent level.

I expected the "Resolve and Include External Scripts" options would do so, at least including Script Modules (*.psm1) in the final EXE. It seems that's not the case, please forward the suggestion to the product group.

There is another tool called PowerShell Pro Tools from Adam Driscoll. It contains Merge-Script.ps1 which merges all desired modules into the final EXE. Adam is writing the option to select which modules to include, so you can even choose.

Anyway, thanks for your help. Any comments will be welcome.

jc
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Documentation on Packager and Modules

Post by Alexander Riedel »

I see how this might be convenient to cover up what modules you use, which is probably why Adam Driscoll created his little script, but it has a plethora of side effects and potential problems. Not even thinking about potential copyright implications. Additonally, how will you want to resolve binary modules or modules exporting powershell classes? What if a module needs outside resources, like additional files or othe rmodules? Where will those be 'included'? Allowing to simply replace an Import-Module call with the code form the ps1m file is a simple task. But it sets an expectation that it will work for ALL modules. And that will never work.
I will make a note and evaluate the options, but in general, if that is what you want to do, you shouldn't have created a module in the first place.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
JCRUIZ
Posts: 4
Last visit: Fri May 01, 2020 6:32 am

Re: Documentation on Packager and Modules

Post by JCRUIZ »

Yes, I would of course expect and admit some design limitations.

For us, it would be enough to be able to import and package all user script modules (psm1), with no further dependencies or binary code.

Thinking aloud, what would happen if we renamed all our psm1 modules to plain ps1 files (as they just contain functions), of course dot-source them instead ? Would they be packaged in the final EXE ?

(Remove-Module functionality is not needed in this case)

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

Re: Documentation on Packager and Modules

Post by davidc »

If these modules contain script files, then you can just add them directly to a PowerShell Studio project.

Set the project file's Build property to 'Content' if you want to dot source it.
Build Property Content.png
Build Property Content.png (12.69 KiB) Viewed 22934 times
Note: To modify the project file properties, select the file in the Project Panel, then go to the Properties Panel.

Set the project files Build property to Include if you want to merge it into the final script.
Set the Shared property to True, to reference the functions within.
Set the Shared property to False, to invoke the file like a function.

Refer to the following article for more details:

https://www.sapien.com/blog/2016/11/15/ ... -v5-3-130/

You can also use the Import Function feature:

https://www.sapien.com/blog/2016/03/07/ ... ew-part-3/
David
SAPIEN Technologies, Inc.
User avatar
JCRUIZ
Posts: 4
Last visit: Fri May 01, 2020 6:32 am

Re: Documentation on Packager and Modules

Post by JCRUIZ »

Thanks David,
Trying your suggestions. I'll keep you informed.
jc
This topic is 6 years and 1 month 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.