Passing arguments to Start-Job scriptblock?

瘦欲@ 提交于 2019-11-30 17:57:42

问题


I'd like to setup a cmdlet to start and stop mysql, and I'm trying to do so with Start-Job. the I've got the following in my Powershell profile:

$mysqlpath = "C:\Program Files\MySQL\MySQL Server 5.5\bin"
Function Start-Mysql
{
    Start-Job -ScriptBlock { & "$mysqlpath\mysqld.exe" }
}

The variable doesn't seem to be expanding in the job command however? I must be missing some sort of scoping rule. Could someone please advise? Thanks!


回答1:


you have to use the -argumentlist parameter see get-help start-job :

 start-job  -ScriptBlock { & $args[0] } -ArgumentList @($mysqlpath )

note that in V3 you just have to use the prefix using: before your varname ex:

  Start-Job -ScriptBlock { & "$using:mysqlpath\mysqld.exe" }


来源:https://stackoverflow.com/questions/14664764/passing-arguments-to-start-job-scriptblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!