*.ps1 脚本能否作为后台作业运行,自己执行 *.ps1 脚本? [英] Can *.ps1 scripts run as background jobs themselves execute *.ps1 scripts?

查看:99
本文介绍了*.ps1 脚本能否作为后台作业运行,自己执行 *.ps1 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行一个脚本的 15 个实例,将 5 个脚本组合在一起,但到目前为止我还没有看到小精灵的灰尘.我将问题归结为一个测试用例,主脚本调用从属脚本,而从属脚本又调用 sub_slave 脚本(不需要管道来重现故障.)

I want to run 15 instances of a script that pipelines 5 scripts together, and so far I'm missing the pixie dust. I've boiled the problem down to a test case with a master script that calls a slave script that in turn calls a sub_slave script (no pipelines are necessary to reproduce the failure.)

如果 master.ps1 调用 slave.ps1,每个后台作业在 slave.ps1 调用 sub_slave.ps1 时无限期挂起.如果我将 slave.ps1 中对 sub_slave.ps1 的调用注释掉,master.ps1 将运行完成.如果我直接启动 slave.ps1,它会运行对 sub_slave.ps1 的调用就好了.这个问题似乎难以解决,但如果我尝试双链.我在文档中没有看到任何内容说您不能将这些脚本菊花链到任意深度,但也许我阅读得不够深入?

If master.ps1 calls slave.ps1 each background job hangs indefinitely at the point slave.ps1 calls to sub_slave.ps1. If I comment out the call in slave.ps1 to sub_slave.ps1, master.ps1 runs to completion. And if I start slave.ps1 directly, it runs the call to sub_slave.ps1 just fine. The problem seems intractable, though if I try the double-chain. I've not seen anything in the docs saying you cannot daisy chain these scripts past some arbitrary depth, but maybe I'm not reading deeply enough?

您会看到我通过将内容添加到一个简单的文本文件来跟踪脚本的进度.

You'll see I'm tracking progress through the script by add-content to a simple text file.

d:\jobs\master.ps1

d:\jobs\master.ps1

$indexes = @(0,1,2)

$initString = "cd $pwd"
$initCmd = [scriptblock]::create($initString)

set-content -path out.txt -value "Master started"
foreach ($index in $indexes)
{
    add-content -path out.txt -value "job  starting"
    start-job -filepath slave.ps1 -InitializationScript $initCmd -ArgumentList @("master index $index originated")
}

add-content -path out.txt -value "master completed"

d:\jobs\slave.ps1

d:\jobs\slave.ps1

#requires -version 2.0

param (
    [parameter(Mandatory=$false)]
    [string]$message = "slave_originated"
)

begin
{
    Set-StrictMode -version Latest
    add-content -path out.txt -value "slave beginning in $pwd"
}

process
{
    add-content -path out.txt -value "slave processing $message"
    invoke-expression "D:\jobs\sub_slave.ps1 -message $message" 
}

end
{
    add-content -path out.txt -value "slave ending"
}

d:\jobs\sub_slave.ps1#requires -version 2.0

d:\jobs\sub_slave.ps1 #requires -version 2.0

param (
    [parameter(Mandatory=$false)]
    [string]$message = "sub_slave originated"
)

begin
{
    Set-StrictMode -version Latest

    add-content -path out.txt -value "sub_slave beginning"
}

process
{
    add-content -path out.txt -value "sub_slave processing $message"
}

end
{
    add-content -path out.txt -value "sub_slave ending"
}

当代码按编写的方式运行时,没有任何注释,我在 out.txt 中得到了这个:d:\jobs\out.txt

When the code is run as written, without anything commented out, I get this in out.txt: d:\jobs\out.txt

Master started
job  starting
job  starting
job  starting
master completed
slave beginning in D:\jobs
slave processing master index 0 originated
slave beginning in D:\jobs
slave processing master index 1 originated
slave beginning in D:\jobs
slave processing master index 2 originated

请注意,每个告别消息之后的下一个命令是调用 sub_slave.ps1,并且根本没有来自 sub_slave.ps1 的消息出现.系统将挂起 3 个 powershell.exe 进程永远愉快地运行.Get-job 报告 3 个进程仍在运行和 HasMoreData.

Note that the very next command after each farewell message is the call to sub_slave.ps1 and that no message from sub_slave.ps1 appears at all. The system will hang with 3 powershell.exe processes running happily forever. Get-job reports the 3 processes are still Running and HasMoreData.

我得到的唯一线索是,如果我崩溃转储挂起的进程,我会看到:

The only clue I've got is that if I crash dump the hung processes, I see:

Number of exceptions of this type:        2
Exception MethodTable: 79330e98
Exception object: 012610fc
Exception type: System.Threading.ThreadAbortException
Message: <none>
InnerException: <none>
StackTrace (generated):
<none>
StackTraceString: <none>
HResult: 80131530
-----------------

Number of exceptions of this type:        2
Exception MethodTable: 20868118
Exception object: 01870344
Exception type: System.Management.Automation.ParameterBindingException
Message: System error.
InnerException: <none>
StackTrace (generated):
<none>
StackTraceString: <none>
HResult: 80131501
-----------------

也许我遇到了某种参数问题?如果我从 sub_slave.ps1 中删除所有参数,则行为不会改变,所以我有点怀疑,但这是可能的.我愿意接受任何想法.

Perhaps I'm having some sort of parameter issue? If I remove all parameters from sub_slave.ps1 the behaviour is unchanged, so I kind of doubt that, but it's possible. I'm open to any idea.

推荐答案

您调用 sub_slave.ps1 是错误的.这:

You are calling sub_slave.ps1 wrong. This:

invoke-expression "D:\jobs\sub_slave.ps1 -message $message" 

应该是这样的:

D:\jobs\sub_slave.ps1 -message $message

消息正在被评估并变成多个参数.

The message was getting evaluated and became multiple arguments.

这篇关于*.ps1 脚本能否作为后台作业运行,自己执行 *.ps1 脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆