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

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

问题描述

概述

希望调用一个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_it\psscripts\PSPatch.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.

结论:

过去,我运行脚本并一次更新一个服务器,并向我提供了想要的输出,但是当我开始做更多的服务器时-该任务花费了太长时间,这就是为什么我要尝试在开始作业"中使用后台作业.

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:在Parralel中运行多个作业,并查看后台作业的流式传输结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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