I have a pretty extensive Powershell script that I'm working on that generates a GUI interface and gives access to a lot of different functions. Stand alone, the Powershell script runs fine when executed as a Powershell script. When I use the PS2EXE tool and convert it to an executable and then try to run the exe, it comes up with an error message saying illegal character in file path and then continues to show dialogue saying cannot find Drive. A drive with a name.... And then it shows me the first couple lines of my commented section at the beginning of the Powershell script. Even if I remove this commented section it still does the same thing, just shows the first several lines of my script. Not exactly sure what's going on. My Powershell script relies on getting the current directory as I run the script from a thumb drive. So I've tried a couple different ways of getting the current directory, as I understand that sometimes converting to an executable has issues with this.
# Get the current script or exe's directory
$currentDir = [System.IO.Path]::GetDirectoryName([System.Reflection.Assembly]::GetExecutingAssembly().Location)
or
# Get the current script or exe's directory
$currentDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
this variable is referenced several times later on when populating some text box with contents of directories and building a tools menu that auto-populates with another directory structure.
Line 20: $currentDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
Line 208: $scriptPath = Join-Path -Path $currentDir -ChildPath "scripts"
Line 223: $softwarePath = Join-Path -Path $currentDir -ChildPath "software"
Line 229: $webShortcutsPath = Join-Path -Path $currentDir -ChildPath "WebShortcuts"
Line 466: $formtoolsPath = Join-Path -Path $currentDir -ChildPath "formtools"
My guess is that it has something to do with the fact that when I convert it to an exe it does not like how I'm getting the current script or exe directory. I have other Powershell scripts that run just fine when I convert them to an exe but they're not getting any kind of a current running directory.
I guess just let me know what you'd like to see, I can post my entire code here, but it is pretty extensive. Currently almost 700 lines.
I've tried a couple different ways of getting the current directory to see which one would work best.
# Get the current script or exe's directory
$currentDir = [System.IO.Path]::GetDirectoryName([System.Reflection.Assembly]::GetExecutingAssembly().Location)
or
[# Get the current script or exe's directory
$currentDir = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition][1]
I don't have an explanation for your specific symptom, but I expect the problem to go away if you switch to the following method for determining your script's / executable's own directory:
$ownDir =
if ($PSScriptRoot) { # running as .ps1 script
$PSScriptRoot
} else { # running as .exe
Split-Path -Parent (Convert-Path -LiteralPath ([System.Environment]::GetCommandLineArgs()[0]))
}
Neither of your attempts to determine this directory works from a ps2exe
-compiled executable.
The automatic $PSScriptRoot
variable variable as well as $PSCommandPath
are only defined inside *.ps1
scripts and *.psm1
modules.
ps2exe
(Invoke-ps2exe
) embeds the contents of the input script inside the executable that it creates, which is why those variables do not work there.
However, you can determine the full path of the executable via [System.Environment]::GetCommandLineArgs())
[0]
, which contains the executable path as invoked (when calling from cmd.exe
) or in full (when calling from PowerShell).
So as to ensure that its full path is always used, the result is passed to Convert-Path
.