在powershell中,如何获取用于调用函数的原始命令?

在powershell中,如何获取用于调用函数的原始命令?,powershell,Powershell,$MyInvocation中是否有这样的东西?或者用什么方法提取它?我想要函数调用中用于参数的原始文本。C:\script.ps1: function Say-MyName { $MyInvocation | fl * } Say-MyName 选择你的武器: MyCommand : Say-MyName BoundParameters : {} UnboundArguments : {} ScriptLineNumber :

$MyInvocation
中是否有这样的东西?或者用什么方法提取它?我想要函数调用中用于参数的原始文本。

C:\script.ps1

function Say-MyName {
    $MyInvocation | fl *
}

Say-MyName
选择你的武器:

MyCommand             : Say-MyName
BoundParameters       : {}
UnboundArguments      : {}
ScriptLineNumber      : 5
OffsetInLine          : 1
HistoryId             : 6
ScriptName            : C:\script.ps1
Line                  : Say-MyName
PositionMessage       : At C:\script.ps1:5 char:1
                        + Say-MyName
                        + ~~~~~~~~~~
PSScriptRoot          : C:\
PSCommandPath         : C:\script.ps1
InvocationName        : Say-MyName
PipelineLength        : 1
PipelinePosition      : 1
ExpectingInput        : False
CommandOrigin         : Internal
DisplayScriptPosition : 

使用
$myinvocation.line
$myinvocation.position
有一些注意事项。如果将对命令的调用拆分为多行(使用backtick),则
.position
.line
不会显示完整的命令。使用splatting、
$args
/
$myinvocation.unboundarguments
(并且不为函数定义任何参数)或$input(如果函数获得管道输入)更安全,这取决于您希望对命令行执行的操作。如果您需要保留空格,那么使用
.line
.position
是您唯一的选择。

您是否正在寻找
$MyInvocation.line
$MyInvocation | fl*
将帮助您决定需要什么:)未绑定参数的另一个问题是它仍然解析逗号,在未绑定的情况下,移除它们。