Skip to main content
improved formatting
Source Link
Ansgar Wiechers
  • 201.5k
  • 27
  • 286
  • 363

How to convert a plain text into a structured powershellPowerShell object

I have a generic text, not PSCustom, not csvCSV, not jsonJSON but has some structure

Field1:ABC Field2: DEF Field1:HKA Field3:YZ Field1:123 Field2:234 Field4:876 Field5:XUZ  Field1:ABC Field2: DEF Field1:HKA Field3:YZ Field1:123 Field2:234 Field4:876 Field5:XUZ 

Obviously, the text is a multiple records with fields and missing fields, each record is separated with crlfCR-LF.

Is there an efficient way to parse the string and extract only Field1Field1 and Field2Field2 out of the records and put the results in jsonJSON?

I tried put the string into a string variable called $s$s then

$s | select -ExpandProperty Field1,Field2 

But I got this error

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported. 

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported.

If I tried

$s | select -ExpandProperty Field1 

Then I got the error saying Field1Field1 not being a property.

I guess I kinda understand the error but not sure how to fix it. I think I have to somehow convert the text into a structured table first then extract it. But how do I do that?

Please help..

How to convert a plain text into a structured powershell object

I have a generic text, not PSCustom, not csv, not json but has some structure

Field1:ABC Field2: DEF Field1:HKA Field3:YZ Field1:123 Field2:234 Field4:876 Field5:XUZ 

Obviously, the text is a multiple records with fields and missing fields, each record is separated with crlf.

Is there an efficient way to parse the string and extract only Field1 and Field2 out of the records and put the results in json?

I tried put the string into a string variable called $s then

$s | select -ExpandProperty Field1,Field2 

But I got this error

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported. 

If I tried

$s | select -ExpandProperty Field1 

Then I got the error saying Field1 not being a property.

I guess I kinda understand the error but not sure how to fix it. I think I have to somehow convert the text into a structured table first then extract it. But how do I do that?

Please help..

How to convert a plain text into a structured PowerShell object

I have a generic text, not PSCustom, not CSV, not JSON but has some structure

 Field1:ABC Field2: DEF Field1:HKA Field3:YZ Field1:123 Field2:234 Field4:876 Field5:XUZ 

Obviously, the text is a multiple records with fields and missing fields, each record is separated with CR-LF.

Is there an efficient way to parse the string and extract only Field1 and Field2 out of the records and put the results in JSON?

I tried put the string into a string variable called $s then

$s | select -ExpandProperty Field1,Field2 

But I got this error

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported.

If I tried

$s | select -ExpandProperty Field1 

Then I got the error saying Field1 not being a property.

I guess I kinda understand the error but not sure how to fix it. I think I have to somehow convert the text into a structured table first then extract it. But how do I do that?

Source Link
user1205746
  • 3.4k
  • 13
  • 51
  • 81

How to convert a plain text into a structured powershell object

I have a generic text, not PSCustom, not csv, not json but has some structure

Field1:ABC Field2: DEF Field1:HKA Field3:YZ Field1:123 Field2:234 Field4:876 Field5:XUZ 

Obviously, the text is a multiple records with fields and missing fields, each record is separated with crlf.

Is there an efficient way to parse the string and extract only Field1 and Field2 out of the records and put the results in json?

I tried put the string into a string variable called $s then

$s | select -ExpandProperty Field1,Field2 

But I got this error

Select-Object : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter 'ExpandProperty'. Specified method is not supported. 

If I tried

$s | select -ExpandProperty Field1 

Then I got the error saying Field1 not being a property.

I guess I kinda understand the error but not sure how to fix it. I think I have to somehow convert the text into a structured table first then extract it. But how do I do that?

Please help..