Powershell compiled exe, native engine, incorrect CurrentDirectory

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 7 years and 5 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
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

Product, version and build: PrimalScript 2015 7.1.63
32 or 64 bit version of product: x64
Operating system: Win 7
32 or 64 bit OS: x64

I am using the Powershell Native Engine compiler from an ealier posting (viewtopic.php?f=11&t=10067) and am having an issue where the current directory, as found by Split-Path $MyInvocation.Mycommand.Path, is coming back as the diretory where the temporary ps1 is being extracted to and not where the exe exists. This is similar to this post: viewtopic.php?t=8136. The temp .ps1 is being extracted from the .exe to c:\temp and that is being treated as it's running directory. Any assistance would be appreciated.

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: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by Alexander Riedel »

Well, that is what PowerShell does, it tells you where the script is. Since you use an engine which uses a temporary file, there is no way to tell Powershell otherwise. If you use the original powershell.exe as the engine to execute your script, this is normal. It is the correct directory.
We have to extract to a temp folder because that is the only safe location to extract files to on modern OS versions.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

Just to verify then, there's no way that, like with the post viewtopic.php?t=8136 where this was 'fixed' for vbscript, to correct that with a powershell that is compiled in Primalscript?

If so I'll leave my script as a .ps1, but it has some login information that I wanted to obscure a little bit with a compiled exe if at all possible.

Thanks for the help!
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by Alexander Riedel »

The post you refer to described a bug in an engine that was not, repeat NOT, using a temporary file. It set the current directory to the temp folder even though it was not necessary.
We corrected that.
In your case there is a temporary file. Different scenario.

Why do you not want to use the SAPIEN script engine with no temporary file?
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

I don't believe I have done anything that would cause a temporary file to be created. I am using the Packager in Primal 2015 to create a packaged exe for my ps1. I've attached an image of the settings I've selected in case there's something I missed that's causing this.
Primal-Packager settings.png
Primal-Packager settings.png (23.72 KiB) Viewed 5956 times
User avatar
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

Any idea on how to correct the use of a temporary file, or what it thinks is its working directory with these settings?

Thanks!
Mike
User avatar
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

I'm being asked to turn this over today, so if it would be possible to get the information on what selection I got wrong that will allow a ps1 compiled into an exe to get it's current directory correctly it would help.

Thanks
User avatar
mupdike
Posts: 31
Last visit: Wed Oct 27, 2021 5:25 am

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by mupdike »

I renewed my subscription this weekend, any chance input on this issue can be provided please?

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

Re: Powershell compiled exe, native engine, incorrect CurrentDirectory

Post by Alexander Riedel »

It seems I do not get notifications on this thread. My apologies.

You use a MICROSOFT script engine. That will ALWAYS use a temporary file.
If you use a SAPIEN script engine, it will not.

This is what you asked for in your original post (viewtopic.php?f=11&t=10067): "I am using the script packager for the first time and wanted to know if there's a way for my powershell script to be setup to use the target systems powershell engine based on the default architecture? So the exe will only call the powershell.exe on the machine and pass my .ps1 to it for execution."

So that is what I gave you.

I also added this:
"Yes. Choose the "Microsoft Windows Powershell (Command line)" engine fitting your platform (x64 or Win32)
It will create a temporary file during execution though. Just so you are aware.
FYI, the SAPIEN PowerShell host engines also use the "native" powershell engine. They just do not use temporary files or powershell.exe"

The current directory is not incorrect, it just may not be what you want. You can either adjust the current folder in your script to where you want it to be
or use a different engine. I am not sure what else I could recommend, sorry.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 7 years and 5 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.