From ea7f9b8c56f7a1158a11d339d9383d681202b801 Mon Sep 17 00:00:00 2001 From: IISResetMe Date: Tue, 24 Jan 2023 22:22:07 +0100 Subject: [PATCH 1/4] Add Statement property to InvocationInfo --- .../engine/InvocationInfo.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/System.Management.Automation/engine/InvocationInfo.cs b/src/System.Management.Automation/engine/InvocationInfo.cs index d0ba2c15c41..a135eac9539 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 ?? string.Empty; + } + } + /// /// Formatted message indicating where the cmdlet appeared /// in the line. From 6e0ad73e251ce1c689a03bd3c4453185d6363ee7 Mon Sep 17 00:00:00 2001 From: IISResetMe Date: Tue, 24 Jan 2023 22:47:55 +0100 Subject: [PATCH 2/4] Add $MyInvocation.Statement tests --- .../Language/Scripting/MyInvocation.Tests.ps1 | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) 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' { From da65f3394be80615fea7e205462fa2332ae3b0ee Mon Sep 17 00:00:00 2001 From: "Mathias R. Jessen" Date: Wed, 25 Jan 2023 13:12:14 +0100 Subject: [PATCH 3/4] Update src/System.Management.Automation/engine/InvocationInfo.cs Co-authored-by: Ilya --- src/System.Management.Automation/engine/InvocationInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InvocationInfo.cs b/src/System.Management.Automation/engine/InvocationInfo.cs index a135eac9539..768611922e5 100644 --- a/src/System.Management.Automation/engine/InvocationInfo.cs +++ b/src/System.Management.Automation/engine/InvocationInfo.cs @@ -274,7 +274,7 @@ public string Line /// /// The full text of the invocation statement, may span multiple lines. /// - /// Statement that was entered to invoke this command + /// Statement that was entered to invoke this command. public string Statement { get From 954f80fb4cb70ed1e0550e4d3cab213f18c47c16 Mon Sep 17 00:00:00 2001 From: IISResetMe Date: Wed, 25 Jan 2023 23:28:43 +0100 Subject: [PATCH 4/4] Remove unnecessary null-fallback --- src/System.Management.Automation/engine/InvocationInfo.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/InvocationInfo.cs b/src/System.Management.Automation/engine/InvocationInfo.cs index 768611922e5..402d7c8d4a6 100644 --- a/src/System.Management.Automation/engine/InvocationInfo.cs +++ b/src/System.Management.Automation/engine/InvocationInfo.cs @@ -279,7 +279,7 @@ public string Statement { get { - return ScriptPosition.Text ?? string.Empty; + return ScriptPosition.Text; } }