2017-05-18 26 views
0

我试图替换命令行工具中的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 

回答

2

用不同的变种一样$MyArgs

更换$args请参阅从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. 
此摘录