将参数传递给 Start-Job 脚本块? [英] Passing arguments to Start-Job scriptblock?
本文介绍了将参数传递给 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" }
这篇关于将参数传递给 Start-Job 脚本块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文