换句话说,如何获取脚本本身的命令行?
所以,我知道$PSBoundParameters
,但它不一样。我只想按原样获取包含传入参数的字符串。
我该怎么做?
答案 0 :(得分:2)
$MyInvocation.Line
$MyInvocation Contains an information about the current command, such as the name, parameters, parameter values, and information about how the command was started, called, or "invoked," such as the name of the script that called the current command. $MyInvocation is populated only for scripts, function, and script blocks.
答案 1 :(得分:1)
请参阅get-help about_Automatic_Variables
。
$Args
Contains an array of the undeclared parameters and/or parameter
values that are passed to a function, script, or script block.
When you create a function, you can declare the parameters by using the
param keyword or by adding a comma-separated list of parameters in
parentheses after the function name.
In an event action, the $Args variable contains objects that represent
the event arguments of the event that is being processed. This variable
is populated only within the Action block of an event registration
command. The value of this variable can also be found in the SourceArgs
property of the PSEventArgs object (System.Management.Automation.PSEventArgs)
that Get-Event returns.
示例:的
test.ps1
param (
)
Write-Output "Args:"
$args
输出:
PS L:\test> .\test.ps1 foo bar, 'this is extra'
Args:
foo
bar
this is extra