question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unix: A script without .ps1 extension passed to the powershell binary lacks invocation information ($PSCommandPath, $MyInvocation), such as when invoked via a shebang line.

See original GitHub issue

Steps to reproduce

Create file ./t - note the absence of extension .ps1 - with the content below and make it executable (chmod +x ./t).

#!/usr/bin/env powershell

'$PSCommandPath: ' + $PSCommandPath
'$MyInvocation.MyCommand.Path: ' + $MyInvocation.MyCommand.Path
'$MyInvocation: ' + ($MyInvocation | Out-String)

Both the following invocation methods, from bash, yield the behavior described below:

./t  # invocation via shebang line
powershell ./t   # implied -File

Expected behavior

$PSCommandPath and $MyInvocation.MyCommand.Path should reflect the script’s file path, and $MyInvocation should be populated appropriately.

Actual behavior

$PSCommandPath:
$MyInvocation.MyCommand.Path:
$MyInvocation:

MyCommand             : #!/usr/bin/env powershell

                        '$PSCommandPath: ' + $PSCommandPath
                        '$MyInvocation.MyCommand.Path: ' + $MyInvocation.MyCommand.Path
                        '$MyInvocation: ' + ($MyInvocation | Out-String)

BoundParameters       : {}
UnboundArguments      : {}
ScriptLineNumber      : 0
OffsetInLine          : 0
HistoryId             : 1
ScriptName            :
Line                  :
PositionMessage       :
PSScriptRoot          :
PSCommandPath         :
InvocationName        :
PipelineLength        : 2
PipelinePosition      : 1
ExpectingInput        : False
CommandOrigin         : Runspace
DisplayScriptPosition :

Environment data

PowerShell Core v6.0.0-beta.3 on macOS 10.12.5
PowerShell Core v6.0.0-beta.3 on Ubuntu 16.04.1 LTS

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:11
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
daxian-dbwcommented, Aug 3, 2020

Reopen the issue as there is an open PR for this.

3reactions
mklement0commented, Jul 22, 2020

@iSazonov, of course there is:

A script should provide invocation information irrespective of the specific invocation method.

The proposed workaround is only a workaround from within PowerShell and would also switch from running in a child process to running in-process; when called from outside PowerShell, you’d have to call the script with the .ps1 extension, whereas the primary advantage of a shebang-line-based script is to not require a filename extension.

@SteveL-MSFT’s statement is open to interpretation, but I read it to mean: “While the current behavior is somewhat intentional (in the sense that there is a code path for it), it should be changed - here’s the relevant source-code location for someone who wants to work on this.” @SteveL-MSFT, can you confirm?

Either way, the current behavior is clearly a bug that should be fixed, so please reopen this issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unix: A script without .ps1 extension passed ...
Unix : A script without .ps1 extension passed to the powershell binary lacks invocation information ($PSCommandPath, $MyInvocation), such as when invoked via ......
Read more >
How can I use a shebang in a PowerShell script?
1 Answer 1 · Ensure the pwsh or powershell command is in PATH · Use this interpreter directive: #!/usr/bin/env pwsh · Ensure the...
Read more >
Untitled
2017 · Unix: A script without .ps1 extension passed to the powershell binary ... ($PSCommandPath, $MyInvocation), such as when invoked via a shebang...
Read more >
Do executable shebang scripts always require LF line ...
newlines - Do executable shebang scripts always require LF line endings? - Unix & Linux Stack Exchange.
Read more >
Creating ESM-based shell scripts for Unix and Windows ...
In this blog post, we learn how to implement shell scripts via Node.js ESM modules. There are two common ways of doing so:....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found