*.ps1 脚本本身可以作为后台作业运行来执行 *.ps1 脚本吗?
我想运行一个脚本的 15 个实例,将 5 个脚本通过管道连接在一起,但到目前为止我还缺少精灵灰尘。我将问题归结为一个测试用例,其中主脚本调用从属脚本,而从属脚本又调用 sub_slave 脚本(不需要管道来重现故障。)
如果 master.ps1 调用 Slave.ps1,则每个后台作业都会挂起在slave.ps1 无限期地调用sub_slave.ps1 时。如果我注释掉slave.ps1中对sub_slave.ps1的调用,master.ps1就会运行完成。如果我直接启动slave.ps1,它就会很好地运行对sub_slave.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
#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
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
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 个进程仍在运行并且有更多数据。
我得到的唯一线索是,如果我崩溃转储挂起的进程,我会看到:
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 中删除所有参数,行为不会改变,所以我有点怀疑,但这是可能的。我对任何想法都持开放态度。
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.)
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
$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
#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
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"
}
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
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
-----------------
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您调用 sub_slave.ps1 是错误的。这:
应该是这样:
消息正在被评估并成为多个参数。
You are calling sub_slave.ps1 wrong. This:
Should be this:
The message was getting evaluated and became multiple arguments.
这里只是猜测,但将多个线程/作业写入同一个文件
out.txt
可能会导致问题。Just taking a guess here, but having multiple threads/jobs write to the same file
out.txt
could be causing issues.