diff --git a/src/System.Management.Automation/engine/InvocationInfo.cs b/src/System.Management.Automation/engine/InvocationInfo.cs index d0ba2c15c41..402d7c8d4a6 100644 --- a/src/System.Management.Automation/engine/InvocationInfo.cs +++ b/src/System.Management.Automation/engine/InvocationInfo.cs @@ -271,6 +271,18 @@ public string Line } } + /// + /// The full text of the invocation statement, may span multiple lines. + /// + /// Statement that was entered to invoke this command. + public string Statement + { + get + { + return ScriptPosition.Text; + } + } + /// /// Formatted message indicating where the cmdlet appeared /// in the line. diff --git a/test/powershell/Language/Scripting/MyInvocation.Tests.ps1 b/test/powershell/Language/Scripting/MyInvocation.Tests.ps1 index d88ea288dbc..54906f5407b 100644 --- a/test/powershell/Language/Scripting/MyInvocation.Tests.ps1 +++ b/test/powershell/Language/Scripting/MyInvocation.Tests.ps1 @@ -23,6 +23,25 @@ Describe 'Testing of MyInvocation' -Tags "CI" { { & myfilter } | Should -Not -Throw } + Context 'MyInvocation works with multi-line invocations' { + It 'MyInvocation.Statement works in & Script block' { + $a = & { + $MyInvocation.Statement + } + $a.IndexOf('& { + $MyInvocation.Statement + }') |Should -BeGreaterThan -1 + } + It 'MyInvocation.Statement works in dot sourced Script block' { + $a = . { + $MyInvocation.Statement + } + $a.IndexOf('. { + $MyInvocation.Statement + }') |Should -BeGreaterThan -1 + } + } + Context 'MyInvocation works in Script block' { It 'MyInvocation works in dot sourced Script block' {