Powershell Studio - Include Language Files (.psd1) in exe

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 8 years and 4 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
renato.bacchi
Posts: 8
Last visit: Tue Oct 30, 2018 1:46 am

Powershell Studio - Include Language Files (.psd1) in exe

Post by renato.bacchi »

Product, version and build: Powershell Studio 2015 V. 4.2.96
32 or 64 bit version of product: 64bit
Operating system: Windows 8.1 Enterprise
32 or 64 bit OS: 64bit

I made a big script with some language specifict output. if I run the script in Powershell Studio, all the language specific variables show the correct values. If I package it as .exe those are completely missing. The language files are located like this:

Project
----Globals.ps1
----Mainform.ps1
----de-DE
--------Language.psd1
----en-EN
--------Language.psd1

How can I check if the .exe includes my psd1 files? If it does not include it, how can I include those folders in the .exe?

Edit: If you want to directly check my script, it is on GITHub. I can PM you the link
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by davidc »

It does not embed them and you will have to include any dependent language files alongside the executable. I recommend creating a MSI so that you can package all the dependent files with the executable.

You will also have to test the language files with the executable because it runs the script from memory and not a specific script file. I'm not sure if this will cause a problem.

David
David
SAPIEN Technologies, Inc.
User avatar
renato.bacchi
Posts: 8
Last visit: Tue Oct 30, 2018 1:46 am

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by renato.bacchi »

How do I multiple languages then?
If i put my .exe in the folder with one language.psd1 the values are correct.
But I want to use the integrated localization function. ATM I would have to make the user to put the exe in the correct folder, which is not really what i want.

It would be cool if it worked like this:

Exe
de-DE
----Language.psd1
en-EN
----Language.psd1

I don't have a problem with installing 2 more folders, but it has to work for all languages
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by davidc »

This may have to do with the fact that the executable runs the script from memory. It is possible PowerShell does not look for the language files in this case.
We will need to investigate.

David
David
SAPIEN Technologies, Inc.
User avatar
juneblender
Posts: 93
Last visit: Thu Mar 30, 2017 8:54 am

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by juneblender »

This is a great question, Renato. I'm going to blog the answer.

I found the clue in this error message:
ERROR: Import-LocalizedData : The FileName parameter was not specified. The FileName
ERROR: parameter is required when Import-LocalizedData is not called from a script
ERROR: file.
And, the solution in help for Import-LocalizedData:
"The FileName parameter is required when Import-LocalizedData is not used in a script."

So, when you're going to package the script in an .exe, add the FileName parameter of Import-LocalizedData, even when the name of the .psd1 matches the script name.

Also, when I built my exe, but ran it from a different directory, ...
e.g. PS C:\> .\Scripts\CoolScript\MyCoolScript.exe -...

PowerShell looked for the .psd1 with the specified filename in the directory from which I was running, e.g. C:\, instead of the script directory, C:\Scripts\CoolScript. This doesn't happen with a .ps1, but it does with an .exe.

So, I wanted to use the BaseDirectory parameter of Import-LocalizedData, too.

To get the base directory, I tried to use $PSScriptRoot and $MyInvocation.MyCommand.Path, but because they're host-specific, they don't work in an .exe. To fix this, I used the Get-ScriptDirectory snippet in PowerShell Studio, which uses a $HostInvocation variable.

Here is the final fix:
function Get-ScriptDirectory
{...}	

Import-LocalizedData -BindingVariable msgTable -FileName Test-UserMsgs.psd1 -BaseDirectory (Get-ScriptDirectory)
if (!$msgtable)
{
    throw "Can't import localized user messages."	
}
Let me know if this works for you.
User avatar
renato.bacchi
Posts: 8
Last visit: Tue Oct 30, 2018 1:46 am

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by renato.bacchi »

wow you guys are awesome, thx for the help!!
User avatar
renato.bacchi
Posts: 8
Last visit: Tue Oct 30, 2018 1:46 am

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by renato.bacchi »

can you tell me what exactly your get-scriptdirectory function does?
it does not work if i try it loke this

function Get-ScriptDirectory
{
Split-Path -Parent $HostInvocation
}
User avatar
renato.bacchi
Posts: 8
Last visit: Tue Oct 30, 2018 1:46 am

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by renato.bacchi »

my bad, it works! thx again
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Powershell Studio - Include Language Files (.psd1) in exe

Post by davidc »

Our Get-ScriptDirectory looks for a packager engine variable that gives the path of the executable. The normal methods will not work since the script is ran from memory.

David
David
SAPIEN Technologies, Inc.
This topic is 8 years and 4 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.