Simple Powershell Template Text Generator

Recently, I came across the need to generate multiple kickstart files for some systems. The usual approach involves using Jinja2 and Python. However, since I won’t be using logical expressions within the template and so a simple replacement of text will suffice for this, so I decided to create my own in powershell.

How to use the script

The script will process two input files as below and output a single text file or multiple text files.

Template File – The file that contains the static text and text to be replaced (written in double curly brackets). Here is the sample template for configuring trunk interface of a network switch.

Interface Eth1/1/{{Interface}}
description Connection_to_{{ServerName}}
switchport mode trunk
switchport trunk allowed vlan {{VlanID}}
exit

Variable File – The csv file which contains the variables names (name must be the same as in Template File) and their values. Note that Server2 and Server3’s VlandID contains commas, and hence you have to use double quotes in the csv file. But if you’re using Microsoft Excel, Excel will take care of it automatically.

Interface,ServerName,VlanID
1,Server1,10
2,Server2,”10,20″
3,Server3,”10,20,30″

To generate multiple output text files, the file names can be defined in -OutputFileColumn, and can further prefixed and suffixed accordingly.

Here is the example command.

.\Generate_TemplateText_v1.0.ps1 -CsvFilePath .\Eth_Variables.csv -TemplatePath .\Eth_Config.txt -SingleFile -DestinationFileName .\Eth_Config_Output.txt

The explanation of the parameters:

-CsvFilePath : The path of the csv file which contains variables as headers and values as contents

-TemplatePath : The path of the template file which contains the text along with the strings to be replaced which is written in double brackets {{ }}

-SingleFile : The flag used to generate the output contents as a single file

-DestinationFileName : The output filename to which the generated text will be saved

OutputFileColumn : Name of the column which contains values to be used as the file names for multiple output files.

  • If the column name is ‘FileName’ which contains File1, File2, File3 as rows under ‘FileName’.
  • Without Prefix defined, the generated file names will be File1, File2, and File3
  • If the Prefix is Config_ and Suffix is .txt, the generated file name will be Config_File1.txt, Config_File2.txt, Config_File3.txt

-Prefix : The text which will be prefixed to the filename which is defined in DestinationFileName or in the csv column.

-Suffix : The text which will be appeneded to the filename which is defined in DestinationFileName or in the csv column.

-ChangeTwoSingleQuotestoDoubleQuotes : The flag which will convert two consecutive single quotes to one double quote. It’s because you can’t use double quotes in the csv file, which will be ignored as part of the csv import.

-OutText : The flag which will output the generated text to the screen.

-Encoding : The encoding of the output file, which is set to ASCII by default.

You can download the script from here or see at the bottom of this post.

Here is the screenshots for quick reference.

Fig-1: Generate a single file which contains multi-generated text blocks

Fig-2: Generate multiple files which contain each text block

Fig-3: How to use double quotes in the csv file

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *