I'm trying to read the text files that are generated from another program and parse them to perform other actions.
This topic is close but the file I'm reading has more "structuring" for lack of a better word. Here is a sample of what the file I am reading will look like for reference:
data:extend(
{
{
type = "buildformula",
name = "item1",
active = false,
buildtime = 3,
ingredients =
{
{"ingredient1", 1},
{"ingredient2", 4},
{"ingredient3", 5}
},
result = "finished item 1"
},
{
type = "buildformula",
name = "item2",
active = true,
buildtime = 12,
ingredients =
{
{"ingredient1", 2},
{"ingredient2", 3}
},
result = "finished item 2"
},
}
)
One other suggestion in that post referenced ConvertFrom-String command which seems to have the power I need but i'm still getting no results.
Lastly here is the most recent code I have :
$TemplateAdr = @'
{
type = "buildformula",
name = "item1",
active = false,
buildtime = 3,
ingredients =
{
{"ingredient1", 1},
{"ingredient2", 4},
{"ingredient3", 5}
},
result = "finished item 1"
},
{
type = "buildformula",
name = "item2",
active = true,
buildtime = 12,
ingredients =
{
{"ingredient1", 2},
{"ingredient2", 3}
},
result = "finished item 2"
},
'@
$output=Get-Content -Path "C:\temp\itembuilds.txt" | ConvertFrom-String -TemplateContent $TemplateAdr
I'm new to PowerShell but I feel like this is possible.
Your $TemplateAdr
template is not marked up as such.
Try this one:
$TemplateAdr = @'
data:extend(
\{
\{
type = "{Type*:buildformula}",
name = "{Name:item1}",
active = {[bool]Active:false},
buildtime = {[int]BuildTime:3},
{Ingredients:ingredients =
\{
\{"{IngredientName*:ingredient1}", {[int]IngredientCount:1}\},
\{"{IngredientName*:ingredient2}", {[int]IngredientCount:4}\},
\{"{IngredientName*:ingredient3}", {[int]IngredientCount:5}\}
\},}
result = "{[string]Result:finished item 1}"
\},
\{
type = "{Type*:buildformula}",
name = "{Name:item2}",
active = {[bool]Active:true},
buildtime = {[int]BuildTime:12},
{Ingredients:ingredients =
\{
\{"{IngredientName*:ingredient1}", {[int]IngredientCount:2}\},
\{"{IngredientName*:ingredient2}", {[int]IngredientCount:3}\}
\},}
result = "{[string]Result:finished item 2}"
\},
\}
)
'@
Get-Content -Path "C:\temp\itembuilds.txt" |
ConvertFrom-String -TemplateContent $TemplateAdr |
Select-Object -Property Type,Name,Active,BuildTime,@{Name='Ingredients';Expression={$_.Ingredients.Items}}
Curly braces are escaped using \
. I've explicitly specified PowerShell types for some of the properties (You might not want that). I've used Select-Object
to map ingredients
list to a new property.
Feel free to change property names inside field specifications ({PropertyName:whatever}
).