none
Convert list output into PowerShell Object

    Question

  • I have data like below in a text file which I would like to convert into PS Object

    Date             : 10-11-2018 16:00:16
    Direction        : Inbound
    SenderAddress    : adriana@abc.com
    RecipientAddress : werner.pedersen@abc.com
    Subject          : Black Hat & DEF CON® 2018 - a debrief with Quinn Shamblin
    Action           : 
    Domain           : abc.com
    EventType        : SpamContentFiltered
    Organization     : abc.onmicrosoft.com
    MessageTraceId   : fb-ad49-4706-e29a-08d647259b57
    MessageId        : <B1.BC.28078.D8007EB5@abc>

    Date             : 10-11-2018 16:00:43
    Direction        : Inbound
    SenderAddress    : bob@abc.com
    RecipientAddress : werner.pedersen@abc.com
    Subject          : Black Hat & DEF CON® 2018 - a debrief with Quinn Shamblin
    Action           : 
    Domain           : abc.com
    EventType        : SpamContentFiltered
    Organization     : abc.onmicrosoft.com
    MessageTraceId   : fb-ad49-4706-e29a-08d647259b57
    MessageId        : <B1.BC.28078.D8007EB5@abc>

    So far I have below but its only adding the first property "Date" in the object

    $a = gc C:\temp.txt
    $Result = @()
    $a | % {
    
    $line = $_
    #Below If statement is for removing empty line
    If ($line -ne "") {
    $Split = $line -split ": "
    $Header = $Split[0]
    $Value = $Split[1]
    $Object = New-Object psobject
    $Object | Add-Member -NotePropertyName $Header ($Value)
    $Result += $Object
      }
    }
    
    $Result
    


    Thanks, Samer

    Thursday, December 6, 2018 1:56 PM

All replies

  • help ConvertFrom-StringData -online


    \_(ツ)_/

    • Proposed as answer by BOfH_666 Thursday, December 6, 2018 9:25 PM
    Thursday, December 6, 2018 2:20 PM
    Moderator
  • I think that's the perfect occation for the cmdlet ConvertFrom-String. It uses example driven text parsing, but I think it would be a little hard to explain it here in the forum. You might watch this video from Dr. Tobias Weltner. He can explain it much better than I could.  ;-)

    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    Thursday, December 6, 2018 2:21 PM
  • String Data can do it very easily.  Just read the file in chunks and replace the ':' with '='.


    \_(ツ)_/

    • Proposed as answer by BOfH_666 Thursday, December 6, 2018 9:25 PM
    Thursday, December 6, 2018 7:46 PM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 10, 2018 6:14 AM
    Moderator
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Tuesday, December 11, 2018 6:33 AM
    Moderator