将参数传递给Start-Job scriptblock?

Dri*_*ism 4 powershell powershell-2.0

我想设置一个cmdlet来启动和停止mysql,我正在尝试使用Start-Job.我的Powershell配置文件中包含以下内容:

$mysqlpath = "C:\Program Files\MySQL\MySQL Server 5.5\bin"
Function Start-Mysql
{
    Start-Job -ScriptBlock { & "$mysqlpath\mysqld.exe" }
}
Run Code Online (Sandbox Code Playgroud)

然而,变量似乎没有在job命令中扩展?我一定错过了某种范围规则.有人可以建议吗?谢谢!

Loï*_*HEL 9

你必须使用-argumentlist参数参见get-help start-job:

 start-job  -ScriptBlock { & $args[0] } -ArgumentList @($mysqlpath )
Run Code Online (Sandbox Code Playgroud)

请注意,在V3中,您只需using:在varname ex之前使用前缀:

  Start-Job -ScriptBlock { & "$using:mysqlpath\mysqld.exe" }
Run Code Online (Sandbox Code Playgroud)