vscode-powershell: Cannot get path of running script in $MyInvocation.InvocationName

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.
  • I have read the troubleshooting guide.
  • I am sure this issue is with the extension itself and does not reproduce in a standalone PowerShell instance.
  • I have verified that I am using the latest version of Visual Studio Code and the PowerShell extension.
  • If this is a security issue, I have read the security issue reporting guidance.

Summary

In versions 2022.5.0 and 2022.5.1 To get the path of a running script. i tried $MyInvocation.InvocationName but I could not get the path by this command.

Similarly, to retrieve the path of a script running on $PSScriptRoot This also did not retrieve the path.

I downgraded the version to 2021.12.0 and got the path correctly

Thank you

Translated with www.DeepL.com/Translator

PowerShell Version

Name                           Value
----                           -----
PSVersion                      7.2.3
PSEdition                      Core
GitCommitId                    7.2.3
OS                             Microsoft Windows 10.0.19044
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visual Studio Code Version

1.67.0
57fd6d0195bb9b9d1b49f6da5db789060795de47
x64

Extension Version

don't work
2022.5.0 and 2022.5.1

work
ms-vscode.powershell@2021.12.0

Steps to Reproduce

$MyInvocation.InvocationName

Visuals

No response

Logs

No response

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 19 (4 by maintainers)

Most upvoted comments

When debugging a script $MyInvocation, $PSScriptRoot and $PSCommandPath are all empty. VSCode 1.74 and PowerShell extension 2022.11.

I routinely have module function implementations in .PS1 files and I dot-source them from .PSM1 files. While I’m developing the functions, I would open the .PS1 file in VS Code and then use F5 and the InvocationName to exercise the new functions under the debugger:

You may want to set a variable in your psm1 instead. InvocationName reading as . instead of the actual invocation name is a bug that may be fixed some day.

Currently, I have confirmed that $MyInvocation.InvocationName does not work but $PSScriptRoot and $PSCommandPath do

Visual Studio Code Version:1.76.0 PowerShell:7.3.3 Extension Version:2023.2.1

@JustinGrote Now I understand why this problem occurred. Thank you!

@CarlosAndreuLlaneras

In the meanwhile I am using “$ScriptPath = Split-Path -Parent $PSCommandPath” instead of “$ScriptPath = Split-Path $MyInvocation.InvocationName”. It works for me.

This is a good method. I will replace $PSCommandPath too. Thanks for letting me know!

@vadhbka thanks for the update! I agree with @CarlosAndreuLlaneras because this makes your script immune to differences in how it is invoked (dotsource vs direct run). This happens in PowerShell itself, not just vscode, we just changed how it gets invoked but this exposes a flaw in your script, not necessarily in vscode.

Test.ps1

$myinvocation.InvocationName

image

@andschwa this looks like a “regression” (more like behavior change) from changing how the debug invokes scripts (since now it dotsources rather than running directly), but I don’t know if there’s anything we can do other than call it out as a “breaking” change.