Skip to content

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

@mklement0

Description

@mklement0

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

Metadata

Metadata

Assignees

Labels

Issue-BugIssue has been identified as a bug in the productOS-LinuxOS-macOSResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions