Powershell:并行运行多个作业并查看后台作业的流结果 [英] Powershell: Run multiple jobs in parralel and view streaming results from background jobs

查看:45
本文介绍了Powershell:并行运行多个作业并查看后台作业的流结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

概述

希望调用一个接受参数的 Powershell 脚本,在后台运行每个作业,并向我显示详细的输出.

Looking to call a Powershell script that takes in an argument, runs each job in the background, and shows me the verbose output.

我遇到的问题

脚本似乎可以运行,但我想通过流式传输后台作业运行的结果来确定这一点.

The script appears to run, but I want to verify this for sure by streaming the results of the background jobs as they are running.

代码

###StartServerUpdates.ps1 Script###

#get list of servers to update from text file and store in array
$servers=get-content c:serverstoupdate.txt

#run all jobs, using multi-threading, in background
ForEach($server in $servers){
  Start-Job -FilePath c:cefcu_itpsscriptsPSPatch.ps1 -ArgumentList $server
}

#Wait for all jobs
Get-Job | Wait-Job

#Get all job results
Get-Job | Receive-Job

我目前看到的:

Id              Name            State      HasMoreData     Location             Command                  
--              ----            -----      -----------     --------             -------                  
23              Job23           Running    True            localhost            #patch server ...        
25              Job25           Running    True            localhost            #patch server ...        

我想看到的:

Searching for approved updates ...

Update Found:  Security Update for Windows Server 2003 (KB2807986)
Update Found:  Windows Malicious Software Removal Tool - March 2013 (KB890830)

Download complete.  Installing updates ...

The system must be rebooted to complete installation.
cscript exited on "myServer" with error code 3.
Reboot required...
Waiting for server to reboot (35)

Searching for approved updates ...

There are no updates to install.
cscript exited on "myServer" with error code 2.
Servername "myServer" is fully patched after 2 loops

我希望能够看到输出或将其存储在某处,以便我可以参考以确保脚本运行并查看哪些服务器重新启动等.

I want to be able to see the output or store that somewhere so I can refer back to be sure the script ran and see which servers rebooted, etc.

结论:

过去,我运行脚本,它一次更新一个服务器并给我我想要的输出,但是当我开始做更多的服务器时 - 这项任务花费了太长时间,这就是我尝试的原因使用带有Start-Job"的后台作业.

In the past, I ran the script and it went through updating the servers one at a time and gave me the output I wanted, but when I started doing more servers - this task took too long, which is why I am trying to use background jobs with "Start-Job".

有人能帮我解决这个问题吗?

Can anyone help me figure this out, please?

推荐答案

您可以查看模块 SplitPipeline.它专为此类任务而设计.工作演示代码是:

You may take a look at the module SplitPipeline. It it specifically designed for such tasks. The working demo code is:

# import the module (not necessary in PS V3)
Import-Module SplitPipeline

# some servers (from 1 to 10 for the test)
$servers = 1..10

# process servers by parallel pipelines and output results immediately
$servers | Split-Pipeline {process{"processing server $_"; sleep 1}} -Load 1, 1

为您的任务替换 "processing server $_";sleep 1(模拟一个缓慢的工作)并调用您的脚本并使用变量 $_ 作为输入,即当前服务器.

For your task replace "processing server $_"; sleep 1 (simulates a slow job) with a call to your script and use the variable $_ as input, the current server.

如果每个作业不是处理器密集型的,则增加参数 Count(默认为处理器计数)以提高性能.

If each job is not processor intensive then increase the parameter Count (the default is processor count) in order to improve performance.

这篇关于Powershell:并行运行多个作业并查看后台作业的流结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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