Add Statement property to $MyInvocation (#19027)

This commit is contained in:
Mathias R. Jessen
2023-03-06 15:01:42 -08:00
committed by GitHub
parent 6dfba03aa3
commit 1925cf67af
2 changed files with 31 additions and 0 deletions
@@ -271,6 +271,18 @@ namespace System.Management.Automation
}
}
/// <summary>
/// The full text of the invocation statement, may span multiple lines.
/// </summary>
/// <value>Statement that was entered to invoke this command.</value>
public string Statement
{
get
{
return ScriptPosition.Text;
}
}
/// <summary>
/// Formatted message indicating where the cmdlet appeared
/// in the line.
@@ -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' {