重复使用2个启动线程函数
假设get-foo
和get-foo2
和deploy-jobs
是3个功能,是一个非常大的模块的一部分。我想使用get-foo
和get-foo2
在deploy-jobs
's start-threadjob
(下面)无需每次重新加载整个模块。
是否可以为此做一个工作示例吗?
function Deploy-Jobs {
foreach ($Device in $Devices) {
Start-ThreadJob -Name $Device -ThrottleLimit 50 -InitializationScript $initScript -ScriptBlock {
param($Device)
Get-Foo | Get-Foo2 -List
} -ArgumentList $Device | out-null
}
}
Assuming Get-Foo
and Get-Foo2
and Deploy-Jobs
are 3 functions that are part of a very large module. I would like to use Get-Foo
and Get-Foo2
in Deploy-Jobs
's Start-ThreadJob
(below) without reloading the entire module each time.
Is an working example available for how to do this?
function Deploy-Jobs {
foreach ($Device in $Devices) {
Start-ThreadJob -Name $Device -ThrottleLimit 50 -InitializationScript $initScript -ScriptBlock {
param($Device)
Get-Foo | Get-Foo2 -List
} -ArgumentList $Device | out-null
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于
Invoke-Command
(psRemoting),start-job
,start-thare-threadJob,您可以使用将函数定义传递给其他范围的方法相同
和foreeach -object -parallel
。由于您想在作业的脚本块中调用2个不同的功能,我认为-Initializationscript
是一个选项,即使是,它也可能使代码成为代码甚至比应该复杂得多。您可以将其用作如何存储2个函数定义的示例在上述范围中,每项工作都将使用。
The method you can use to pass the function's definition to a different scope is the same for
Invoke-Command
(when PSRemoting),Start-Job
,Start-ThreadJob
andForeEach-Object -Parallel
. Since you want to invoke 2 different functions in your job's script block, I don't think-InitializationScript
is an option, and even if it is, it might make the code even more complicated than it should be.You can use this as an example of how you can store 2 function definitions in an array (
$def
), which is then passed to the scope of each TreadJob, this array is then used to define each function in said scope to be later used by each Job.