PowerShell服务器在修改大型.xml文件时崩溃

时间:2016-11-04 14:09:44

标签: performance powershell

正如标题所述,我制作了一个PowerShell脚本,其性能非常糟糕,以至于它过度扩展了服务器资源并使其崩溃。

该脚本读取整个.xml文件,并在文件的开头和末尾附加文本。此外,它还会更改文件的名称,该文件名称位于我的 filename.txt 中。

.xml文件大约500 MB,有超过470万行。有没有办法,我不必阅读整个文件,但没有松散的信息?

function start-jobhere([scriptblock]$block){
start-job -argumentlist (get-location),$block { set-location $args[0];  invoke-expression $args[1] }
}

$handler_button1_Click= { 
    Try{ 
    $job3 = start-jobhere {
    #Text that should be at filebeginning
    @('<?xml version="1.0" encoding="UTF-8"?>
    <ids:ControlInfo>
    <ids:ObjectFormat>CSV</ids:ObjectFormat>
    <ids:SeparatorForCSV>;</ids:SeparatorForCSV>
    </ids:ControlInfo>

    <ids:BatchDeltaUntil></ids:BatchDeltaUntil>
    </ids:BatchInfo>
    </ids:Header>
    <ids:Body>'
    ) + (get-content ZUB_Lokalisation.xml) | set-content ZUB_Lokalisation.xml

    #Text that should be at file end
    Add-Content ZUB_Lokalisation.xml -Value "</ids:Body>`n</ids:SimpleOperation>"

    #Information that goes into the header of the file but has to be extracted from the filename inside a .txt
    $filename = Select-String filename.txt -Pattern "Lokalisation"
    $nameoffile = [System.IO.Path]::GetFileName($filename)
    $split = $nameoffile.split('_')
    $finalid = $split[5]
    $content = Get-Content ZUB_Lokalisation.xml
    $content[8] = '     <ids:BatchInfo ids:BatchID="{0}">' -f $finalid
    $content | Set-Content ZUB_Lokalisation.xml

    #Rename the file
    Rename-Item ZUB_Lokalisation.xml -NewName $filename}
    }catch [System.Exception]{zeigen
    [System.Windows.Forms.MessageBox]::Show("ZUB_LOK_ERROR", "ERROR")}
    }

    Get-Job | Wait-Job | Where State -eq "Running"
    }

1 个答案:

答案 0 :(得分:2)

创建包含所需开始和结束片段的文件。

然后在dos窗口或批处理文件中运行它:

COPY StartFile.TXT + YourXMLFile.TXT + EndFile.TXT OutputFile.TXT

将三个文件粘在一起并将其保存为OutputFile.TXT