将参数传递给 Start-Job 脚本块?
问题描述:
我想设置一个 cmdlet 来启动和停止 mysql,我正在尝试使用 Start-Job 来实现.我的 Powershell 配置文件中有以下内容:
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!
答
您必须使用 -argumentlist
参数,请参阅 get-help start-job :
you have to use the -argumentlist
parameter see get-help start-job :
start-job -ScriptBlock { & $args[0] } -ArgumentList @($mysqlpath )
请注意,在 V3 中,您只需在 varname 之前使用前缀 using:
,例如:
note that in V3 you just have to use the prefix using:
before your varname ex:
Start-Job -ScriptBlock { & "$using:mysqlpath\mysqld.exe" }