2016-11-04 40 views
2

那么作为标题状态,我做了一个PowerShell脚本,其性能如此糟糕,以致于它过度扩展了服务器资源并使其崩溃。PowerShell服务器在修改大的.xml文件时崩溃

脚本读取整个.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" 
    } 

回答

2

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

然后在DOS窗口或批处理文件运行此:

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

这一起坚持三个文件并将其保存为OutputFile.TXT

+0

注意,复制命令可以在PowerShell脚本中运行也 – Jimbo