How to include "custom" project files in exe package?

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 1 year and 10 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
mipodaril
Posts: 4
Last visit: Wed Mar 13, 2024 10:42 pm

How to include "custom" project files in exe package?

Post by mipodaril »

Product, version and build: Sapien PowerShell Studio 2021
Operating system: Windows 10
PowerShell version(s): any

Hello,
I have a PowerShell Studio project which contains some custom files, like test document or XML configuration file.
I want to create package (exe file), which will be used without installation (will not create installation MSI) with these files included.
Is it possible?
If so, how to reference these files?

The idea behind is, for example with xml config file:
when "APP" is executed, it will check if xml configuration file is in "current directory" for the app. And if not, it will use "default" xml file which was added to the project...

The code might look like:
  1. Write-LogFile "Testing external configuration file 'Config.xml'..." $dbgVerbose
  2. if (Test-Path -Path "$ScriptDirectory\Config.xml")
  3. {
  4.     Write-LogFile "External configuration file exists..." $dbgVerbose
  5.     $global:configFile = "$ScriptDirectory\Config.xml"
  6.     Write-LogFile "Configuration file changed to: $configFile ..." $dbgVerbose
  7. }
  8. else
  9. {
  10.     Write-LogFile "External configuration file doesn't exist..." $dbgVerbose
  11.     Write-LogFile "Internal configuration file will be used..." $dbgVerbose
  12.     if (Test-Path $configFile)
  13.     {
  14.         Write-LogFile "Internal configuration file exists..." $dbgVerbose
  15.     }
  16.     else
  17.     {
  18.         Write-LogFile "Internal configuration file '$configFile' doesn't exist..." $dbgError
  19.     }
  20. }
and the question is how "$configFile" should look like to point to "default" config file added into the project...

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

Re: How to include "custom" project files in exe package?

Post by Alexander Riedel »

Typically, to make your code resilient, you should always have default settings that are applied if a config file is not present.
If you absolutely need a config file for reasons of practicality you should create it if it does not exist rather than juggling an 'external' and an 'internal' file.
The simplest way is to store the default XML in a string in your script.
If the config file exists, load it.
If not, create it from the string, then load it.

Please keep in mind though that depending on where your executable is located, you may not have sufficient rights to write in the same folder.
Generally it is best practice to store such things in an AppData folder either for the machine or the particular user, as needed. You must have write access there by definition.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
mipodaril
Posts: 4
Last visit: Wed Mar 13, 2024 10:42 pm

Re: How to include "custom" project files in exe package?

Post by mipodaril »

Thank you for your ideas, appreciate...
But config.xml was just an example of file. I need to add also dll file, PDF file and others, because what this "utility" tries to do is print some specific types of documents...
So this script "requires" a lot of different "external" files (to be printed), but as it must be practical, handy and easy to run utility, it cannot use "installer". So that is why I am thinking about all these files into compiled exe file. Nobody can see/modify these files as they are "invisible" and my script can use them for testing printing...
So I would say the question is, whether files, which I have added to the Project can be/are compiled into "EXE" file and if so, how I can reference them in my script...
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: How to include "custom" project files in exe package?

Post by Alexander Riedel »

The short answer is, there is no such mechanism that you ask for. I will elaborate a bit for your benefit and anyone reading this later. I do not know your business case for this, so please understand I am not trying to sway you one way or another, I am just passing on information for you to use as you see fit.

This is the ancient single-executable-that-contains-everything application style. This was a feasible and even a preferred option to distribute software in the age of floppy discs and and prior to Windows Installer's existence. Today, not so much. (I know I dated myself here)

An executable file that unpacks data files and even worse, executable code (dlls) at the moment of launch has all the hallmarks of malware. Assuming that this is not the intent here, you still will face a host of anti-malware software shutting your utility down when it tries to do that. You also face the hurdle of not being able to do so, as you may lack the write access required to do that at whatever location your utility is. If I understand you correctly though, you would like to load these other files from some embedded storage rather than actually manifest them as a file, not even a temporary one.
If you would code this utility in C++ for example, I would say there is a way to do that. A lot of security software would still stop an attempt at runtime code injection I would think (and hope).
In Powershell, I cannot think of a mechanism to do that to be honest.

I will go through some of the questions and concerns I think you have (I may be wrong):

- I do not want users to be able to modify my files.
If you have an MSI installer for your application, it typically gets installed in C:\Program Files\<Company>\<your application>
As it is under Program Files, which is a protected folder, a normal user will not be able to modify your files. They will need admin privileges.

- I am worried users may corrupt my application
DLL and EXE files should be digitally signed at any rate, which will prevent anyone from modifying them without raising alarm.

- I do not want users to see the data I store or provide to prevent misuse or theft of my intellectual property.
That raises the question how far you want to go. You can store data in an encrypted format and decrypt the file on the fly when using it.
Since you will be using it, it will be visible in the computer's memory at some point. You mention printing PDF files, so an obvious question is, what is the point in hiding that
if I can simply print it and then scan it back into a PDF? Anyone determined enough will be able to see your files and your code.

- I do not want users to be able to modify my data files.
This implies "modify them without me knowing".
The simplest way is to detect modification and refuse to run if someone messed with something. A basic integrity check, checking files for existence, check sums, sizes, dates etc. could do that.
If something has been messed with, error out. That requires some organization when distributing it though, as this information must be updated each time you change something.

- I need a portable app that is running from a network location or flash drive.
Most cases the best way is to stick the application in an aptly named folder with a starter executable.
MyPrintTool.exe has a folder MyPrintTool next to is. MyPrintTool.exe will start ./MyPrintTool/MyApp.exe, which is your actual application.
All your stuff is neatly contained in a folder. Apple has done this for a long time, where the folder is actually considered the application.
You starter app could do a number of basic sanity checks before launching the actual application to address some of the concerns above.
This type of application is easily 'installed' with XCopy or contained in a zip file.

That's all I can think of at the moment, hope some of it helps.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 1 year and 10 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.