gpt4 book ai didi

powershell - 并行化 powershell 脚本执行

转载 作者:行者123 更新时间:2023-12-03 00:24:41 25 4
gpt4 key购买 nike

我有 8 个 powershell 脚本。他们很少有依赖关系。这意味着它们不能并行执行。他们应该一个接一个地执行。

一些 Powershell 脚本没有依赖关系,可以并行执行。

以下是详细解释的依赖项

    Powershell scripts 1, 2, and 3 depend on nothing else
Powershell script 4 depends on Powershell script 1
Powershell script 5 depends on Powershell scripts 1, 2, and 3
Powershell script 6 depends on Powershell scripts 3 and 4
Powershell script 7 depends on Powershell scripts 5 and 6
Powershell script 8 depends on Powershell script 5


我知道通过手动硬编码依赖是可能的。但是可能会添加 10 个以上的 powershell 脚本,并且可能会添加它们之间的依赖关系。

有没有人通过发现依赖来实现并行性?如果是这样,请分享我如何进行。

最佳答案

您需要查看 PowerShell 3.0 工作流。它提供了满足您的要求所需的功能。像这样的东西:

workflow Install-myApp {
param ([string[]]$computername)
foreach -parallel($computer in $computername) {
"Installing MyApp on $computer"
#Code for invoking installer here
#This can take as long as 30mins and may reboot a couple of times
}
}

workflow Install-MyApp2{
param ([string[]]$computername)
foreach -parallel($computer in $computername) {
"Installing MyApp2 on $computer"
#Code for invoking installer here
#This can take as long as 30mins!
}
}

WorkFlow New-SPFarm {
Sequence {
Parallel {
Install-MyApp2 -computername "Server2","Server3"
Install-MyApp -computername "Server1","Server4","Server5"
}
Sequence {
#This activity can happen only after the set of activities in the above parallel block are complete"
"Configuring First Server in the Farm [Server1]"

#The following foreach should take place only after the above activity is complete and that is why we have it in a sequence
foreach -parallel($computer in $computername) {
"Configuring SharePoint on $computer"
}
}
}
}

关于powershell - 并行化 powershell 脚本执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9124308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com