繁体   English   中英

Powershell多线程

[英]Powershell multithreading

我有一个Powershell脚本,可以将Office文档转换为PDF。 我想对其进行多线程处理,但无法根据我所看到的其他示例弄清楚该如何做。 主脚本(OfficeToPDF.ps1)扫描文件列表,并为每种文件类型/办公应用程序调用单独的脚本(例如,.doc文件称为WordToPDF.ps1进行转换)。 主脚本一次将1个文件名传递给子脚本(出于两个原因,我这样做了)。

这是主要脚本的示例:

    $documents_path = "C:\Documents\Test_Docs"
    $pdf_out_path = "C:\Documents\Converted_PDFs"
    $failed_path = "C:\Documents\Failed_to_Convert"

    # Sets the root directory of this script
    $PSScriptRoot = Split-Path -parent $MyInvocation.MyCommand.Definition

    $date = Get-Date -Format "MM_dd_yyyy"
    $Logfile = "$PSScriptRoot\logs\OfficeToTiff_$Date.log"

    $word2PDF = "$PSScriptRoot\WordToPDF.ps1"
    $arguments = "'$documents_path'", "'$pdf_out_path'", "'$Logfile'"

    # Function to write to log file
    Function LogWrite
    {
       Param ([string]$logstring)
       $time = Get-Date -Format "hh:mm:ss:fff"

       Add-content $Logfile -value "$date $time $logstring"
    }


################################################################################
# Word to PDF                                                                  #
################################################################################

    LogWrite "*** BEGIN CONVERSION FROM DOC, DOCX, RTF, TXT, HTM, HTML TO PDF ***"

    Get-ChildItem -Path $documents_path\* -Include *.docx, *.doc, *.rtf, *.txt, *.htm? -recurse | ForEach-Object {

            $original_document = "$($_.FullName)"

            # Verifies that a document exists before calling the convert script
            If ($original_document -ne $null)
            {

                Invoke-Expression "$word2PDF $arguments"

                #checks to see if document was successfully converted and deleted.  If not, doc is moved to another directory
                If(Test-Path -path $original_document)
                {
                Move-Item $original_document $failed_path
                }
            }
         }

    $original_document = $null

    [gc]::collect()
    [gc]::WaitForPendingFinalizers()

这是主脚本调用的脚本(WordToPDF.ps1):

Param($documents, $pdf_out_path, $Logfile)

# Function to write to the log file
Function LogWrite
{
   Param ([string]$logstring)
   $time = Get-Date -Format "hh:mm:ss:fff"

   Add-content $Logfile -value "$date $time $logstring"
}

$word_app = New-Object -ComObject Word.Application

$document = $word_app.Documents.Open($_.FullName)
$original_document = "$($_.FullName)"

# Creates the output file name with path
$pdf_document = "$($pdf_out_path)\$($_.BaseName).pdf"

LogWrite "Converting: $original_document to $pdf_document"
$document.SaveAs([ref] $pdf_document, [ref] 17)
$document.Close()

# Deletes the original document after it has been converted
Remove-Item $original_document
LogWrite "Deleting: $original_document"

$word_app.Quit()

任何建议,将不胜感激。 谢谢。

我只是要发表评论并将您链接到这个问题: PowerShell可以在Parallel中运行命令 然后,我记下了该问题的日期和答案,在PowerShell v3.0中,有些新功能可能对您更好。

问题超出了使用PowerShell作业的范围 可以正常工作,但需要跟上工作状态,因此可以添加一些额外的代码来管理。

PowerShell v3通过基于Windows Workflow Foundation的workflow为您打开了更多的大门。 可以在Script Guy的博客上找到有关此新命令如何工作的基础知识的好文章。 您基本上可以调整代码以通过工作流运行转换,它将并行执行此操作:

workflow foreachfile {
  foreach -parallel ($f in $files) {
    #Put your code here that does the work
  }
}

从我可以找到的线程限制中,一次有5个线程。 我不确定这有多准确,但是这里的博客文章指出了限制 但是,由于Word和Excel的Application com对象可能占用大量CPU,因此一次执行5个线程可能会很好地工作。

我有一个多线程Powershell环境,用于显示在所有AD设备上进行危害扫描的指标-使用Gearman进行625次线程扫描。 http://gearman.org

它是开源的,允许跨平台使用。 它与服务器工作程序流配合并通过Python运行。 真正由您极力推荐-滥用Powershell线程的人。 这不是一个答案,而是我从未听说过的东西,但每天都在爱和用。 向前传递。 开源的胜利:)

我以前也使用过psjobs,并且它们在达到一定数量级之前都很棒。 也许是因为我缺乏.net专业知识,但是ps具有一些古怪的微妙记忆细微差别,在很大程度上可以产生一些讨厌的效果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM