Powershell – 从脚本启动作业以调用带参数的另一个脚本

huangapple go评论59阅读模式
英文:

Powershell - Start-Job from a script to call another script with parameters

问题

以下是您要翻译的部分:

I'm trying to run a script in background from a main script, the command is :

Start-Job -Name "Job Name" -File "Child script path" `
  -ArgumentList Param1, ..., Param6.

There are 6 parameters, the first one is an array, the second a string, 3,4 and 5 are credentials and the last one a string. If I call the second script like ( & "script path" ), it works well. If I call the second script with Start-ThreadJob by adding the option -StreamingHost $Host after the option -Name, I got in the console a parameter's request for a command inside the second script, as it was not receiving the parameters given by the script calling Start-ThreadJob.

Any idea what I'm doing wrongly ?

DraKKarD

I tried :

Start-Job -Name "Job Name" -File "Child script path" -ArgumentList Param1, ..., Param6

=> Doesn't work

Start-ThreadJob -Name "Job Name" -StreamingHost $Host -File "Child script path" -ArgumentList Param1, ..., Param6

=> Doesn't work

& "Child script path" Param1 ... Param6

=> works

The expecting result is some log files created and filed and some actions that will write in log files.

英文:

I'm trying to run a script in background from a main script,
the command is :

Start-Job -Name "Job Name" -File "Child script path" `
  -ArgumentList Param1, ..., Param6.

There are 6 parameters, the first one is an array, the second a string, 3,4 and 5 are credentials and the last one a string.
If I call the second script like ( & "script path" ), it works well.
If I call the second script with Start-ThreadJob by adding the option -StreamingHost $Host after the option -Name, I got in the console a parameter's request for a command inside the second script, as it was not receiving the parameters given by the script calling Start-ThreadJob.

Any idea what I'm doing wrongly ?

DraKKarD

I tried :

Start-Job -Name "Job Name" -File "Child script path" -ArgumentList Param1, ..., Param6

=> Doesn't work

Start-ThreadJob -Name "Job Name" -StreamingHost $Host -File "Child script path" -ArgumentList Param1, ..., Param6

=> Doesn't work

& "Child script path" Param1 ... Param6

=> works

The expecting result is some log files created and filed and some actions that will write in log files.

答案1

得分: 1

似乎问题与使用 Start-ThreadJob 时传递参数给子脚本有关。使用 Start-Job 时,参数是显式传递的,使用 -ArgumentList 参数,但使用 Start-ThreadJob 时,参数是通过 -StreamingHost 参数隐式传递的。

$arguments = @(
    $Param1,
    $Param2,
    $Param3,
    $Param4,
    $Param5,
    $Param6
)

Start-ThreadJob -Name "任务名称" -File "子脚本路径" -ArgumentList $arguments

尝试上面的部分,这应该正确地将参数传递给子脚本。如果仍然遇到问题,您需要在子脚本中添加一些调试日志,以查看参数是否被正确接收。

英文:

It seems like the issue is related to passing parameters to the child script when using Start-ThreadJob. When using Start-Job, the parameters are passed explicitly using the -ArgumentList parameter, but with Start-ThreadJob, the parameters are implicitly passed using the -StreamingHost parameter.

$arguments = @(
    $Param1,
    $Param2,
    $Param3,
    $Param4,
    $Param5,
    $Param6
)

Start-ThreadJob -Name "Job Name" -File "Child script path" -ArgumentList $arguments

Try upper part, This should pass the parameters to the child script correctly. If you are still having issues, you need to add some debug logging to your child script to see if the parameters are being received correctly.

huangapple
  • 本文由 发表于 2023年4月4日 13:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75925872.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定