Powershell在处理过程中停滞不前

时间:2016-04-21 14:03:17

标签: excel powershell

我有以下power-shell(4.0)脚本。它打开了一本excel书(2013)并从单元格中获取一个URL并打开它。然后它将网页下载到.htm文件。它遍历工作表中的每一行(~3000)。

我遇到的问题是第一个~500个文件在大约3分钟内完成。然后它似乎会大大减慢到每2-3分钟只创建一个文件的位置。我检查了我的可用ram和cpu使用情况,两者都很好(ram 2.93gb使用的是8gb,cpu是35%)。

我能做些什么来绕过这个并加快速度吗?

cls

$output_folder = "c:\temp" 
$OUTPUT_FILENAME="" 

# comment following line to add a timestamp to each file gets created 
if($OUTPUT_FILENAME.length -eq 0) {$OUTPUT_FILENAME=(get-date).tostring().replace(" ","").replace("/","").replace(":","")} 
$filepath = "C:\Temp\MeteringHistory\Meters for Maximo Upload"
$xl = New-Object -COM "Excel.Application"
$xl.Visible = $false
$wb = $xl.Workbooks.Open($filepath)
$ws = $wb.Worksheets.Item("WOhistory")
$maxRow = ($ws.UsedRange.rows).count
$minRow = 1

for ($minrow -le $maxrow ; $minrow++) 

{

$website = $ws.cells.item($minRow, 1).text
$fileName = $ws.cells.item($minRow, 5).text + " - " + $ws.cells.item($minRow, 4).text
$fileName = $fileName -replace '/', '_'
$wc=new-object system.net.webclient
$wc.UseDefaultCredentials = $true
$wc.downloadfile($website,"c:\temp\MeteringHistory\Files\$filename.htm")
$wc.Dispose()

}

2 个答案:

答案 0 :(得分:1)

我会首先将Excel导出为CSV,然后再抛弃Excel。你甚至可以从powershell本身做到这一点。然后使用invoke-webrequest代替您拥有的所有样板并完成它。它只能在几行代码中完成。这样你就可以将问题简化为基本的PowerShell。

为了加快这个过程,你可以通过后台工作并行调用webrequest。

答案 1 :(得分:0)

很难在没有错误的情况下知道错误。我会尝试一件事:当你可以重用第一个时,避免创建和处理数百个WebClient - 对象。尝试:

cls

$output_folder = "c:\temp" 
$OUTPUT_FILENAME="" 

# comment following line to add a timestamp to each file gets created 
if($OUTPUT_FILENAME.length -eq 0) {$OUTPUT_FILENAME=(get-date).tostring().replace(" ","").replace("/","").replace(":","")} 
$filepath = "C:\Temp\MeteringHistory\Meters for Maximo Upload"
$xl = New-Object -COM "Excel.Application"
$xl.Visible = $false
$wb = $xl.Workbooks.Open($filepath)
$ws = $wb.Worksheets.Item("WOhistory")
$maxRow = ($ws.UsedRange.rows).count
$minRow = 1

$wc=new-object system.net.webclient
$wc.UseDefaultCredentials = $true

for ($minrow -le $maxrow ; $minrow++) {

    $website = $ws.cells.item($minRow, 1).text
    $fileName = $ws.cells.item($minRow, 5).text + " - " + $ws.cells.item($minRow, 4).text
    $fileName = $fileName -replace '/', '_'
    $wc.DownloadFile($website,"c:\temp\MeteringHistory\Files\$filename.htm")

}

$wc.Dispose()