Compiled EXE InvocationInfo.MyCommand.Name has part of full path when hashtag (#) in folder path

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 4 years and 9 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
DennisVH
Posts: 12
Last visit: Wed Nov 16, 2022 6:24 am

Compiled EXE InvocationInfo.MyCommand.Name has part of full path when hashtag (#) in folder path

Post by DennisVH »

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

When the folder path has a # sign in it, where you run the compiled exe from, the InvocationInfo.MyCommand.Name doesn't contain just the exe file name (which is expected), but also contains the path to the parent directory that contains the # sign.

I used following script to recreate the issue:

Code: Select all

If (Test-Path -LiteralPath 'variable:HostInvocation') { $InvocationInfo = $HostInvocation }
Else { $InvocationInfo = $MyInvocation }
$InvocationInfo.MyCommand | fl
f.e. running C:\Temp\InvocationInfo\#1\x64\Invocation.exe results in
Name : #1/x64/invocationinfo.exe
where running C:\Temp\InvocationInfo\#1\Invocation.ps1 results in
Name : invocationinfo.ps1

If i run the same exe from C:\Temp\InvocationInfo\1\x64, (no hashtag), this results in
Name : invocationinfo.exe
and running C:\Temp\InvocationInfo\1\Invocation.ps1 results in
Name : invocationinfo.ps1
which is expected.
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 20
Been upvoted: 37 times

Re: Compiled EXE InvocationInfo.MyCommand.Name has part of full path when hashtag (#) in folder path

Post by Alexander Riedel »

The reason for this is that the .NET framework mangles the file name if a # character is in it.
Ran it quickly in the debugger and I see that Application.ExecutablePath delivers something like:
C:\Temp\InvocationInfo\#1/x64/PoshAppHostV5.exe

For some reason it decides mid name to switch from \ to / as a path separator.
It's not that hard to fix that in the packager engines, but I suspect that this will affect other parts of the .NET framework,
so I would just stay away from # in folder names. I have not yet researched if that is known .NET thing or not.
Alexander Riedel
SAPIEN Technologies, Inc.
This topic is 4 years and 9 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.