Powershell并行作业输出文件 [英] Powershell parallel jobs output file

查看:149
本文介绍了Powershell并行作业输出文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了很多研究,但是仍然不知道如何完成我想做的事情.

I have already done a lot of research, but still can't figure out how to accomplish what I want to do.

我想在100台Linux服务器上并行执行相同的任务.

I want to perform the same tasks parallel on 100 Linux servers.

这是我的脚本的简化示例:

Here is a simplified example of my script:

$computer=Get-Content "serverList.txt"
$jobArray=@()
$script={
    $cpuThresh=70
    $cpuUsage=<Get CPU usage of the host>
    Write-Host "CPU Usage: $cpuUsage %"
    if ($cpuUsage -ge $cpuThresh) {
        Write-Host "Unexpected CPU Usage" -ForegroundColor Red
    }
}
foreach ($system in $computer) {
    $jobArray += Start-Job -ScriptBlock $script -ArgumentList $system
    While ((Get-Job -State 'Running').Count -ge 10) {
        Start-Sleep -Milliseconds 10
    }
}
foreach ($job in $jobArray) {
    While ($job.State -eq 'Running') {
        Start-Sleep -Milliseconds 10
    }
    Receive-Job -Job $job
    Remove-Job -Job $job
}

我遇到的问题是我想将某些消息(例如,意外的CPU使用率)写入一个单独的文件,并且多个作业试图同时写入该文件.

The problem I have is that I want to write certain messages (e.g. Unexpected CPU Usage) to a separate file and multiple jobs are trying to write to this file at the same time.

我的想法是将所有消息保存到一个数组中,并将脚本末尾的内容(第二个foreach循环)写入文件.

My idea would be to save all messages into an array and write the content at the end of the script (second foreach loop) to a file.

但是Receive-Job不会返回任何变量/对象.

But Receive-Job doesn't return any variables/objects.

有没有一种方法可以返回变量/对象?还是有另一种方法可以实现我想做的事?

Is there a way to return a variable/object? Or is there another way to achieve what I want to do?

我将不胜感激.谢谢.

推荐答案

每个作业至少都有(通常只有一个)子作业.进程的输出实际上保存在子作业的单独输出缓冲区中,并且可以从那里进行访问.您可以将Write-Verbose用于一组输出,将Write-Warning用于另一组输出,然后分别从Verbose和Warning流中读回它:

Every job has at least (an normally only one) child jobs. The output of the process is actually held in separate output buffers of the child jobs, and can be accessed from there. You can use Write-Verbose for one set of output, and Write-Warning for another, and read it back from the Verbose and Warning streams separately:

$computer=Get-Content "serverList.txt"
$jobArray=@()
$script={
    $VerbosePreference = 'Continue'
    $Args[0]
    $cpuThresh=70
    $cpuUsage=<Get CPU usage of the host>
    Write-Verbose "CPU Usage: $cpuUsage %"
    if ($cpuUsage -ge $cpuThresh) {
        Write-Warning "Unexpected CPU Usage" 
    }
$Results  = @{}
$Warnings = @{}
$Outputs  = @{}
}
foreach ($system in $computer) {
    $jobArray += Start-Job -ScriptBlock $script -ArgumentList $system
    While ((Get-Job -State 'Running').Count -ge 10) {
        Start-Sleep -Milliseconds 10
    }
}
foreach ($job in $jobArray) {
    While ($job.State -eq 'Running') {
        Start-Sleep -Milliseconds 10
    }
     $Server = $Job.ChildJobs[0].Output[0]
     $Results[$Server] = $Job.ChildJobs[0].Verbose
     $Warnings[$Server] = $Job.ChildJobs[0].Warning
     $Outputs[$Server]   = $Job.ChildJobs[0].Output

    Remove-Job -Job $job
}

已更新所有本地作业.

这篇关于Powershell并行作业输出文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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