< Back

Invoke-TemplateEngine

Tue Jan 14, 2020 1:28 am

NAME Invoke-TemplateEngine



SYNOPSIS

Process a PowerShell-based document template and generates a document dynamically.





SYNTAX

Invoke-TemplateEngine [-Template] <string> [[-ProcessorFile] <string>] [<CommonParameters>]





DESCRIPTION

Invoke-TemplateEngine processes a document template that embeds PowerShell scripts and generates a plain text as

an outcome to the standard output stream.



It recognizes text portions enclosed with <% and %> as PowerShell code snippets and executes them. The objects

returned by the code into the standard output stream are written into a resultant document.



Text portions enclosed with <% and- %> are processed as the same above, but the following newlines will not appear

in the output.



Note that, unlike usual PowerShell output, such objects are converted into strings by the ToString() method and no

newlines are inserted between them. See Example section for details.





PARAMETERS

-Template <string>

A document template, given as a string or an array of strings through pipeline.



Required? true

Position? 0

Default value

Accept pipeline input? true (ByValue)

Accept wildcard characters? false



-ProcessorFile <string>

If specified, an internal script for processing will be saved into the file. (For debugging purpse)



Required? false

Position? 1

Default value

Accept pipeline input? false

Accept wildcard characters? false



<CommonParameters>

This cmdlet supports the common parameters: Verbose, Debug,

ErrorAction, ErrorVariable, WarningAction, WarningVariable,

OutBuffer, PipelineVariable, and OutVariable. For more information, see

about_CommonParameters (https:/go.microsoft.com/fwlink/?LinkID=113216).



INPUTS

System.String

A document template, given as a string or an array of strings through pipeline.





OUTPUTS



---------- EXAMPLE 1 ----------



PS>Get-Content template.txt

Dear <% $customer %>,

This is a mail for you.

PS>

PS>$customer = "Bill"

PS>Get-Content template.txt | Invoke-TemplateEngine

Dear Bill,

This is a mail for you.

PS>



---------- EXAMPLE 2 ----------



PS>Invoke-TemplateEngine '<% foreach ($i in 1..3) { $i } %>'

123



The object returned from the script are written into the output without any gaps. To control spaces or newlines

between them, see the next example.



---------- EXAMPLE 3 ----------



PS>Get-Content template.txt

<% foreach ($i in 1..3) { -%>

<% $i %>

<% } -%>

PS>

PS>Get-Content template.txt | Invoke-TemplateEngine

1

2

3



When '-%>' is used as a closing tag, a following newline of a code block will not appear in the output.



---------- EXAMPLE 4 ----------



PS>dir C:\\work



Directory: C:\\work



Mode LastWriteTime Length Name

---- ------------- ------ ----

-a---- 2017/12/29 17:36 252 a.txt

-a---- 2017/12/29 17:36 252 b.txt

-a---- 2017/12/29 17:36 252 c.txt



PS>##### Objects are stringified by the ToString() method.

PS>

PS>Invoke-TemplateEngine '<% dir %>'

a.textb.txtc.txt

PS>

PS>##### Specify a property name to manage output format

PS>

PS>Invoke-TemplateEngine '<% (dir).FullName -join "`r`n" %>'

C:\\work\\a.txt

C:\\work\\b.txt

C:\\work\\c.txt

PS>

PS>##### Format-Table and Out-String enable to generate host-like output.

PS>

PS>Invoke-TemplateEngine '<% dir | ft -auto | out-string %>'



Directory: C:\\work



Mode LastWriteTime Length Name

---- ------------- ------ ----

-a---- 2017/12/29 17:36 252 a.txt

-a---- 2017/12/29 17:36 252 b.txt

-a---- 2017/12/29 17:36 252 c.txt





RELATED LINKS