6

Suppose I'm running a PowerShell script that takes several input parameters. The command looks like:

psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

Is there a way to store this exact command in a variable within the script so that I can log it?

Specifically, I'd like to know what to assign to the variable $currentCommand:

$currentCommand = <something>
Write-Host "currently running script " $currentCommand

Such that the Write-Host output would be the exact command line used to invoke the script. If the script command was the same as above, for example, then the output would be:

currently running script psScript.ps1 -arg1 "arg1value" -arg2 "arg2value"

4

2 回答 2

6

这可能适合您的需求:

Write-Host "currently running script " $myinvocation.Line

参考

于 2013-01-11T17:54:45.000 回答
3

$MyInvocation 变量将包含信息。这是一篇关于它的好博客文章

于 2013-01-11T17:34:37.380 回答