Asked by:
Convert list output into PowerShell Object

-
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
Question
All replies
-
-
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''
-
-
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.
-
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.