<p>{1{cdi>的解决方案很可能是为了把XML{1}转换成一个非常简单的XML文件,而不是使用cdl格式的XML文件。在</p>
<pre class="lang-psh prettyprint-override"><code>$InputData = Get-Content 'C:\Path\To\File.log'
$Records = @{}
$InputData | ?{$_ -match "^(?<Date>\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) (?<ID>\[.+?\]) (?<IP>\S+)\<AgentInfo (?<AgentInfo>.+?)\/\>" -or $_ -match "^(?<Date>\d{2}\/\d{2} \d{2}:\d{2}:\d{2}) (?<ID>\[.+?\]) (?<Msg>.+)"}|
%{$Record = [pscustomobject]@{
[string]'Date'=$Matches['Date']
[string]'ID'=$Matches['ID']
[string]'IP'=$Matches['IP']
[string]'Msg'=$Matches['Msg']
'AgentInfo'=New-Object PSObject -Prop ($Matches['AgentInfo'] -replace '(?<=") ',"`r`n" | ConvertFrom-StringData)
}
$record
If($Matches['ID'] -notin $Records.Keys){
$Records.Add($Matches['ID'], $Record)
}Else{
$Record|Get-Member -MemberType Properties | Where{![string]::IsNullOrEmpty($Record.($_.Name))} | ForEach{$Records."$($Matches['ID'])"|Add-Member "$($_.Name)" $Record.$($_.Name) -Force}
}
}
$records.Values|select Date,ID,IP,Msg,AgentInfo|convertto-xml -Depth 2 -NoTypeInformation -as Stream|%{$_ -replace 'Property Name="(.+?)(?=">)"(.*)Property(?=>)','$1$2$1' -replace 'Property Name="(.+?)"(?= />)','$1' -replace '<Property Name="(.+?)">','<$1>' -replace '</Property>','</AgentInfo>'} | Set-Content C:\Path\To\OutFile.xml
</code></pre>
<p>将输出:</p>
^{pr2}$
<p>这与您想要的输出非常接近。在</p>