【问题标题】:What is the Powershell equivalent for bash $*?bash $* 的 Powershell 等价物是什么?
【发布时间】:2016-11-23 09:22:11
【问题描述】:

换句话说,我怎样才能获得脚本本身的命令行?

所以,我知道$PSBoundParameters,但不一样。我只想按原样获取包含传入参数的字符串。

我该怎么做?

【问题讨论】:

  • "我只想按原样获取包含传入参数的字符串。" - $* 不会那样做。
  • 当您可以利用 PowerShell 的内置参数解析时,为什么要这样做?
  • @Bill_Stewart - 我需要使用具有相同命令行参数和一些附加参数的相同脚本调用 powershell.exe。
  • 为什么?更全面地解释您的用例会很有帮助。
  • 我的工作使用来自github.com/RamblingCookieMonster/PowerShell/blob/master/…Invoke-Parallel 函数产生了几个并行任务。但随后它必须与已经运行的批处理并行生成另一批类似的任务。出于某种原因,我无法使用Invoke-Parallel 做到这一点。也许不支持嵌套的运行空间 - 我不太了解这件事。无论如何,我通过使用相同的命令行(几乎)生成一个完整的工作来解决问题。

标签: powershell


【解决方案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

【讨论】:

  • 我的脚本有很多声明的参数。
【解决方案2】:
$MyInvocation.Line

阅读about_Automatic_Variables

$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.

【讨论】:

    猜你喜欢
    • 2011-01-25
    • 2012-06-16
    • 2016-07-25
    • 2011-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多