0

I am trying to parse log line items into a custom powershell object to add to a collection I can iterate, however I am having trouble figuring out how to find the specific data within the log item. Here is an example of how the line item reads:

### 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)_ 

Out of this data, I am trying to create a powershell object with the following properties:

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.

There is a line break (whitespace line) between where the code sample above starts, and where it ends. How would one parse the example string to meet the expected result?

1 Answer 1

1
# 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.

The above yields:

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.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.