将日志项解析为自定义对象

Parsing A Log Item Into Custom Object

我正在尝试将日志行项目解析为自定义 powershell 对象以添加到我可以迭代的集合中,但是我无法弄清楚如何在日志项目中查找特定数据。以下是订单项的读取方式示例:

### 4/20/2020 2:03:14 PM - [/SitePages/Home.aspx](https://xxxxxxx/sites/xxxxxxxxx/SitePages/Home.aspx)

_Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at SharePointPnP.Modernization.Framework.Transform.ContentTransformator.LastColumnOrder(Int32 row, Int32 col)
   at SharePointPnP.Modernization.Framework.Transform.ContentTransformator.Transform(List`1 webParts)
   at SharePointPnP.Modernization.Framework.Transform.PageTransformator.Transform(PageTransformationInformation pageTransformationInformation)_ 

根据这些数据,我试图创建一个具有以下属性的 powershell 对象:

Date - 4/20/2020 2:03:14 PM

Page - https://xxxxxxx/sites/xxxxxxxxx/SitePages/Home.aspx

Error - _Index was out of range. Must be non-negative and less than the size of the collection.

上面的代码示例开始和结束之间有一个换行符(空白行)。如何解析示例字符串以满足预期结果?

# Sample multi-line log entry.
$str = @'
### 4/20/2020 2:03:14 PM - [/SitePages/Home.aspx](https://xxxxxxx/sites/xxxxxxxxx/SitePages/Home.aspx)

_Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index   at System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument argument, ExceptionResource resource)
   at SharePointPnP.Modernization.Framework.Transform.ContentTransformator.LastColumnOrder(Int32 row, Int32 col)
   at SharePointPnP.Modernization.Framework.Transform.ContentTransformator.Transform(List`1 webParts)
   at SharePointPnP.Modernization.Framework.Transform.PageTransformator.Transform(PageTransformationInformation pageTransformationInformation)_ 
'@

# Use the -match operator with capture groups to extract the tokens of interest...
if ($str -match '^### (?<date>.+?) - .+\((?<url>.+?)\)\r?\n\s*\r?\n(?<msg>.+)') { 
  # ... and construct a custom object based on the capture-group values.
  [pscustomobject] @{
    Date = $Matches.date
    Page = $Matches.url
    Error = $Matches.msg
  }
}
# else: regex didnt' match.

以上结果:

Date                 Page                                                Error
----                 ----                                                -----
4/20/2020 2:03:14 PM https://xxxxxxx/sites/xxxxxxxxx/SitePages/Home.aspx _Index was out of range. Must be non-negative and less than the size of the collection.