$PSScriptRoot variable is empty when running an EXE created using PowerShell Tools for VS 2017
$PSScriptRoot variable is empty when running an EXE created using PowerShell Tools for VS 2017
The version of Posh Tools is 4.1.4
PS C:\Windows\system32> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.1
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.1
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
This was apparently fixed in 4.0 as part of #3, but it does not appear to be.
This critical and is stopping our app from going live.
I have uninstalled and reinstalled PowerShell Tools for VS, but still an issue. The program works fine when run/deubugged from within VS.
Comments (16)
-
-
reporter Just dropped you a line, with a bit more of an explanation.
-
- changed status to resolved
Fixes
#59→ <<cset a299b4c46b06>>
-
I have the same issue also in ps module 1.12.0. Is there a workaround?
All are empty: Write-Output "$PSScriptRoot" Write-Output "$pwd" Write-output "$(Get-Item -Path .).name"
-
$PSScriptRoot is never actually defined in a packaged process. During the bundling, the attempts to include the resources that are being referenced with $PSScriptRoot.
What I could do is have $PSScriptRoot set to the assembly's local file path.
What are you using $PSScriptRoot for?
-
- changed status to open
Reactivating as the fix did not resolve the issue.
-
Just hit this same issue, wish I'd checked the support site before trying ages of other methods thinking it was just me.
Generally using to reference other project files such as log files or WPF XML's in the same directory as the main PS script.
-
I use $psscriptroot in multiple files to find resources like config files or orther script based modules that can't by bundled or to write logfiles relative to my exe. E.g. $psscriptroot\log or $psscriptroot\my.config etc. Is there a other way to get the path the exe is in?
-
just tried using [System.Reflection.Assembly]::GetExecutingAssembly() in my script but, as expected, codebase is empty
-
What if you do this for now?
Split-Path (Get-Process -ID $Pid).Path
-
Works on my test app/script checking for this issue. I'll try it on some of my actual scripts later.
Thanks!
-
Great! I'll make sure this issue gets resolved in the next version so you can just use $PSScriptRoot.
-
i've been testing a couple of my scripts and modules. your suggested workaround is fine.
-
This has been resolved and will be in the next version.
-
- changed status to resolved
-
just tried it with the newest version and it still doesn't work.
ModuleType Version Name ExportedCommands ---------- ------- ---- ---------------- Manifest 4.1.8 PowerShellProTools {ConvertTo-CSharp, ConvertTo-PowerShell, Get-PoshProToolsLicense, Install-PoshProToolsLicense...}
script I'm testing with:
$var = $PSScriptRoot Write-Output "var = $var"
output if when running as script:
PS C:\GIT> c:\GIT\Untitled-1.ps1 var = C:\GIT
convert to exe with merge script:
Merge-Script -Script .\Untitled-1.ps1 -Package -OutputPath .\
output of exe: "var= "
I'm getting something wrong?
- Log in to comment
Sorry for the issues. Can you please email me at adam@poshtools.com so we can sync up and get this resolved for you quickly?
Thanks