将powershell值传递给命令行工具

时间:2017-05-18 13:32:24

标签: powershell

我正在尝试替换命令行工具中的powershell值,但无法弄明白。

$a = "C:\Test\TestApp.exe"
$args = "arg1 arg2 arg3"


& schtasks /create /SC ONLOGON /TN "Test Task" /tr "'$a' $args" /F

//expected - schtasks /create /SC ONLOGON /TN "Test Task" /tr "'C:\Test\TestApp.exe' arg1 arg2 arg3" /F

1 个答案:

答案 0 :(得分:2)

$args替换为$MyArgs

之类的其他变种

请参阅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.