Help needed with templating for ConvertFrom-String

Hello,

I have txt file below and would like to create structured object as output in essence a dictionary or hashtable. I created template but obviously doing something wrong since it does not return structured data.

$template = @'
{Name:{Model_Name:Model Number} {Name_value:FVB 15.5-1200-20 EA03}}
{Shop:{Shop_name:Shop} {Shop_value:Order Number}}
Model Tech
Manufacturing Date 03/19/1997
Serial Number 17-12085
'@

$testtext = @'
Model Number FVB 15.5-1200-20 EA03
Shop Order Number
Model Tech
Manufacturing Date 03/19/1997
Serial Number 17-12085
'@


$testtext | convertfrom-string -TemplateContent $template -OutVariable results
#$results

You might need a little more template data and more work data

$template = @’
Model Number FVB {ModelNumber*:15.5-1200-20 EA03}
{ShopName:Shop} Order Number
Model Tech
Manufacturing Date {ManDate:03/19/1997}
Serial Number {Serial:17-12085}

Model Number FVB {ModelNumber*:15.5-1200-21 EA04}
{ShopName:Online} Order Number
Model Tech
Manufacturing Date {ManDate:03/19/1998}
Serial Number {Serial:17-12087}

Model Number FVB {ModelNumber*:15.5-1200-22 EA05}
{ShopName:Local} Order Number
Model Tech
Manufacturing Date {ManDate:03/19/1999}
Serial Number {Serial:17-12086}

'@

$testtext = @’
Model Number FVB 15.5-1200-20 EA03
Shop Order Number
Model Tech
Manufacturing Date 03/19/1997
Serial Number 17-12085

Model Number FVB 15.5-1200-21 EA04
Online Order Number
Model Tech
Manufacturing Date 03/19/1998
Serial Number 17-12087

Model Number FVB 15.5-1200-22 EA05
Local Order Number
Model Tech
Manufacturing Date 03/19/1999
Serial Number 17-12086

Model Number FVB 15.5-1200-20 EA06
Shop Order Number
Model Tech
Manufacturing Date 03/19/2000
Serial Number 17-12083

Model Number FVB 15.5-1200-21 EA07
Online Order Number
Model Tech
Manufacturing Date 03/19/2001
Serial Number 17-12088

Model Number FVB 15.5-1200-22 EA08
Local Order Number
Model Tech
Manufacturing Date 03/19/2002
Serial Number 17-12089

'@

$testtext | convertfrom-string -TemplateContent $template -OutVariable results
$results

results:
ModelNumber       ShopName ManDate    Serial


15.5-1200-20 EA03 Shop 03/19/1997 17-12085
15.5-1200-21 EA04 Online 03/19/1998 17-12087
15.5-1200-22 EA05 Local 03/19/1999 17-12086
15.5-1200-20 EA06 Shop 03/19/2000 17-12083
15.5-1200-21 EA07 Online 03/19/2001 17-12088
15.5-1200-22 EA08 Local 03/19/2002 17-12089
15.5-1200-20 EA03 Shop 03/19/1997 17-12085
15.5-1200-21 EA04 Online 03/19/1998 17-12087
15.5-1200-22 EA05 Local 03/19/1999 17-12086
15.5-1200-20 EA06 Shop 03/19/2000 17-12083
15.5-1200-21 EA07 Online 03/19/2001 17-12088
15.5-1200-22 EA08 Local 03/19/2002 17-12089

I’m thoroughly confused what I need to do.
All I have a text below. There is nothing else, that is. 5 lines total. It’s delimited by spaces and I need to manually create template to separate Name of field from field value. Example below. First field name is “Model Number” and value of that field is “FVB 15.5-1200-20 EA03”. Second field name is “Shop” and field value is “Order Number”. I need to create template that based on field name (which will not be changing) I will be able to create hashtable like @{“Model Number”=“FVB 15.5-1200-20 EA03”;
“Shop”=“Order Number” etc}

Model Number FVB 15.5-1200-20 EA03
Shop Order Number
Model Tech
Manufacturing Date 03/19/1997
Serial Number 17-12085

Hmmm … now I am confused. You have only one single file you need to extract the information from, not more? So do it by hand??? :wink: You don’t need a script for that.

If it’s more than just that you could use regular expressions to get what you’re looking for. … lite this:

$testtext = Get-Content -Path C:\sample\shop.txt

foreach($line in $testtext){
If(line -match '^Model\s+Number\s+(.*)’){
$ModelNumber = $Matches[1]
Remove-Variable Matches -ErrorAction SilentlyContinue
}
if(line -match '^Shop\s+(.*)’ ){
$Shop = $Matches[1]
Remove-Variable Matches -ErrorAction SilentlyContinue
}
}
[PSCustomObject]@{
ModelNumber = $ModelNumber
Shop = $Shop
}

… outputs this:
ModelNumber           Shop


FVB 15.5-1200-20 EA03 Order Number


Probably not the most elegant way to do but it does the job. :smiley:

I meants I have a lot of text files with the same pattern as $testtext but I load them one by one and hoped to avoid using RegEx and just define template since FieldNames are not changing. Each of the files has only 1 instance of entire object and hence there is no sequences. Will it still be possible to use templating in such cases or RegEx is the only way?

I don’t know if that really provides a benefit for you this way. Anyway you could try to use at least 2 or 3 or 4 of these different little text files as your template.
You might watch this video from Tobias. It might help you deciding what approach could be the best for you.
Sophisitcated Techniques of Plain Text Parsing

+1 one for having more than one example in the template. Here’s an ipconfig one with two examples. It wouldn’t work with one example.

$template = @'
   IPv4 Address. . . . . . . . . . . : {ip*:172.16.173.105}
   Subnet Mask . . . . . . . . . . . : {mask:255.255.255.128}
   Default Gateway . . . . . . . . . : {gateway:172.16.173.1}
   IPv4 Address. . . . . . . . . . . : {ip*:172.16.173.106}
   Subnet Mask . . . . . . . . . . . : {mask:255.255.255.192}
   Default Gateway . . . . . . . . . : {gateway:172.16.173.1}
'@

ipconfig | ConvertFrom-String -TemplateContent $template

ip             mask            gateway
--             ----            -------
172.16.173.105 255.255.255.128 172.16.173.1