title: Parsing XML-Like log file
body: <p>I have a log file which records events as follows. I would like convert each event into a PSCustomobject. It kinda looks like XML but casting xml to the Get-Content for the file gives me an error:</p>
  
  <p>Cannot convert value "System.Object[]" to type "System.Xml.XmlDocument". Error: "This document already has a 'DocumentElement' node." </p>
  
  <pre><code>&lt;event date='Jan 06 01:46:16' severity='4' hostName='ABC' source='CSMFAgentPolicyManager' module='smfagent.dll' process='AeXNSAgent.exe' pid='1580' thread='1940' tickCount='306700046' &gt;
    &lt;![CDATA[Setting wakeup time to 3600000 ms (Invalid DateTime) for policy: DefaultWakeup]]&gt;
  &lt;/event&gt;
  </code></pre>
  
  <p>Here is the piece of code I have so far</p>
  
  <pre><code>   &lt;#
  .EXAMPLE    
  source    : MaintenanceWindowMgr
  process   : AeXNSAgent.exe
  thread    : 8500
  hostName  : ABC
  severity  : 4
  tickCount : 717008140
  date      : Jan 10 19:45:00
  module    : PatchMgmtAgents.dll
  pid       : 11984
  CData     : isAbidingByMaintenanceWindows() - yes
  #&gt;
  $logpath = Join-Path $env:ProgramData 'Symantec\Symantec Agent\logs\Agent.log'
  $hash=[ordered]@{};
  $log = get-content $logpath | % {
  
      ## handle Event start
      ## sample: &lt;event date='Jan 10 18:45:00' severity='4' hostName='ABC' source='MaintenanceWindowMgr' module='PatchMgmtAgents.dll' process='AeXNSAgent.exe' pid='11984' thread='8500' tickCount='713408140' &gt;
      if ($_ -match '^&lt;event') {
  
          if ($hash) {                
              ## Convert the hastable to PSCustomObject before clearing it
              New-Object PSObject -Property $hash
              $hash.Clear()
          }
  
          $line = $_ -replace '&lt;event ' -replace ' &gt;' -split "'\s" -replace "'"               
          $line | % { 
  
              $name,$value=$_ -split '='                
              $hash.$name=$value
          }        
      }
  
      ## handle CData
      ## Sample: &lt;![CDATA[Schedule Software Update Application Task ({A1939DC8-DA4A-4E46-9629-0500C2383ECA}) triggered at 2014-01-10 18:50:00 -5:00]]&gt;
      if ($_ -match '&lt;!') {
          $hash.'CData' = ($_ -replace '&lt;!\[CDATA\[' -replace '\]\]&gt;$').ToString().Trim()
      }
  }
    $log 
  </code></pre>
  
  <p>Unfortunately, the object is not in the form I would want it. </p>
  
  <pre><code>$log|gm
  
  
     TypeName: System.Management.Automation.PSCustomObject
  
  Name        MemberType Definition                    
  ----        ---------- ----------                    
  Equals      Method     bool Equals(System.Object obj)
  GetHashCode Method     int GetHashCode()             
  GetType     Method     type GetType()                
  ToString    Method     string ToString()   
  </code></pre>
  
  <p>When I try to collect all the objects from the output, I am losing the NoteProperties that are generated when I convert the hash to PSCustomObject</p>
  
  <pre><code>   TypeName: System.Management.Automation.PSCustomObject
  
  Name        MemberType   Definition                                                                                                                                     
  ----        ----------   ----------                                                                                                                                     
  Equals      Method       bool Equals(System.Object obj)                                                                                                                 
  GetHashCode Method       int GetHashCode()                                                                                                                              
  GetType     Method       type GetType()                                                                                                                                 
  ToString    Method       string ToString()                                                                                                                              
  Equals      Method       bool Equals(System.Object obj)                                                                                                                 
  GetHashCode Method       int GetHashCode()                                                                                                                              
  GetType     Method       type GetType()                                                                                                                                 
  ToString    Method       string ToString()                                                                                                                              
  CData       NoteProperty System.String CData=isAbidingByMaintenanceWindows() - yes                                                                                      
  date        NoteProperty System.String date=Jan 10 18:45:00                                                                                                             
  hostName    NoteProperty System.String hostName=ABC                                                                                                             
  module      NoteProperty System.String module=PatchMgmtAgents.dll                                                                                                       
  pid         NoteProperty System.String pid=11984                                                                                                                        
  process     NoteProperty System.String process=AeXNSAgent.exe                                                                                                           
  severity    NoteProperty System.String severity=4                                                                                                                       
  source      NoteProperty System.String source=MaintenanceWindowMgr                                                                                                      
  thread      NoteProperty System.String thread=8500                                                                                                                      
  tickCount   NoteProperty System.String tickCount=713408140 
  </code></pre>
  
  <p>What am I missing here? </p>