Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

powershell - Passing arguments to Start-Job scriptblock?

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 FilesMySQLMySQL Server 5.5in"
Function Start-Mysql
{
    Start-Job -ScriptBlock { & "$mysqlpathmysqld.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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:mysqlpathmysqld.exe" }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...