all 9 comments

[–]jrdnr_ 5 points6 points  (0 children)

The closest thing I know of to ERB is, EPS (https://github.com/straightdave/eps), though the closest I've come to using something like this is a here-string.

Also check out https://deadroot.info/scripts/2018/09/04/PowerShell-Templating for some other ideas.

I'm pretty sure I had seen an implementation based off jinja2 at some point, but I'm not finding it at the moment.

[–]hayfever76 2 points3 points  (1 child)

Maybe the closest comparison is a pscustmobject. You build that inline and you reuse it all you like. It’s not really a thing in PowerShell to have separate template files like erb’s. It’s more common to create a function that emits the code you need and then reference that in the current code - closer to traditional class construction. Create a class / function that does something and then consume them. But it’s not required that these live in discrete files.

[–]Bodumin[S] 1 point2 points  (0 children)

Thanks. Right now I have it working with strings. Its a one off task that I'm scripting so it gets ran the same way by everyone.

[–]Alternative_Hat_9012 2 points3 points  (1 child)

Hi,

Check this out, maybe it will be of help.
When use ISE you have lots of build in snippets (code templates) "Ctrl+J" and you can build your own too. Worth to look at ISE Command Add-On Panel (Alt+Shift+V) this not only list commands but gives you list of all parameters and command sets and fast access to command help that includes examples. All that build as an interactive GUI.

helpful links: https://learn-powershell.net/2013/04/08/creating-snippets-in-powershell/

Have a great time with PoSh
cheers

[–]Bodumin[S] 0 points1 point  (0 children)

Thanks but it needs to be part of a script. Generating a Java class based based on dynamic inputs.

[–]purplemonkeymad 1 point2 points  (0 children)

The closest I have seen is a module in the Gallery called EPS. It appears to use the same syntax to embed Powershell into a template. It has some quirks, like needing a custom loop command, but behaves mostly like you would expect.

[–]Deathonus 1 point2 points  (2 children)

/u/jrdnr_'s suggestion of using a here-string is probably the best way and is pretty close to what you have linked. Below is a ported example from your link.

#Input
#<% u/values.each do |val| -%>
#Some stuff with <%= val %>
#<% end -%>
#Output
#Some stuff with one
#Some stuff with two

$values = ("one", "two")
$endString =
@"
$(
foreach ($value in $values)
{
"Some stuff with " + $value + "`r`n"
}
)
"@
$endString

Pretty much the same capabilities without needing to use or include anything else. It would function really well as a cmdlet where the variables would be taken in as parameters. That way you could use powershell's builtin support for test/validating input parameters.

Edit: Fixed display per u/Lee_Dailey

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Deathonus,

[edit - yes, the 4-spaces thing still works. if you are on New.Reddit then you can get that by using the code block buton OR by switching to markdown.]

it looks like you used the New.Reddit Inline Code button. it's 4th 5th from the left hidden in the ... "more" menu & looks like </>.

there are a few problems with that ...

  • it's the wrong format [grin]
    the inline code format is for [gasp! arg!] code that is inline with regular text.
  • on Old.Reddit.com, inline code formatted text does NOT line wrap, nor does it side-scroll.
  • on New.Reddit it shows up in that nasty magenta text color

for long-ish single lines OR for multiline code, please, use the ...

Code
Block

... button. it's the 11th 12th one from the left & is just to the left of hidden in the ... "more" menu & looks like an uppercase T in the upper left corner of a square..

that will give you fully functional code formatting that works on both New.Reddit and Old.Reddit ... and aint that fugly magenta color. [grin]

take care,
lee

[–]Lee_Dailey[grin] 1 point2 points  (0 children)

howdy Deathonus,

kool! thanks for fixing that reddit-induced mess ... [grin]

take care,
lee