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

Support for all customers who have purchased a SAPIEN PowerShell Studio product license. This forum does not offer a response time guarantee.
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.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
User avatar
DennisVH
Posts: 12
Joined: Sun Mar 03, 2013 12:46 pm

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

Post by DennisVH » Wed Jun 12, 2019 7:07 am

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: 6947
Joined: Tue May 29, 2007 4:43 pm

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

Post by Alexander Riedel » Wed Jun 12, 2019 7:46 am

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.