$PSScriptRoot variable is empty when running an EXE created using PowerShell Tools for VS 2017

Issue #59 resolved
Adam Birds created an issue

$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)

  1. Adam Driscoll

    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

  2. Emrys MacInally

    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"

  3. Adam Driscoll

    $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?

  4. Steve Beaumont

    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.

  5. Emrys MacInally

    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?

  6. Emrys MacInally

    just tried using [System.Reflection.Assembly]::GetExecutingAssembly() in my script but, as expected, codebase is empty

  7. Steve Beaumont

    Works on my test app/script checking for this issue. I'll try it on some of my actual scripts later.

    Thanks!

  8. Adam Driscoll

    Great! I'll make sure this issue gets resolved in the next version so you can just use $PSScriptRoot.

  9. Emrys MacInally

    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?

  10. Log in to comment