none
Generate a file from a template using a powershell script RRS feed

  • Question

  • Hello, I have a powershell script which accepts a file name as a parameter. This file contains variables which I will use to generate a file from a template containing the variable names. I would like the script to create the file already containing the values of the variable file

    generate.ps1
    ################
    param(
      [string] $file
    )

    $var = (Get-Content .\$file -Raw | ConvertFrom-StringData)
    $template = (Get-Content .\template -Raw)

    $template = @"
    $template
    "@

    $template | Out-File -FilePath .\file
    #################

    I was able to do this manually, I am not sure why it does not work in the script.

    Please advise. Thank you


    • Edited by Luuuffy Friday, June 21, 2019 1:44 PM Update
    Friday, June 21, 2019 1:36 PM

All replies

  • You never use $var.

    \_(ツ)_/

    Friday, June 21, 2019 2:06 PM
  • You never use $var.

    \_(ツ)_/

    Hello, sorry for the late reply. My bad, $var is used in my template file. I will show an example

    template
    #################
    var1 = $($var.var1)
    var2 = $($var.var2)
    var3 = $($var.var3)
    #################

    $file # the variable file
    #################
    var1 = "var1"
    var2 = "var2"
    var3 = "var3"
    #################

    Thank you.


    Monday, June 24, 2019 5:07 AM
  • That is not PowerShell script.  What is it? 

    In your original post you never use "$var".


    \_(ツ)_/


    • Edited by jrv Monday, June 24, 2019 12:11 PM
    Monday, June 24, 2019 12:09 PM
  • That is not PowerShell script.  What is it? 

    In your original post you never use "$var".


    \_(ツ)_/


    I wanted my generate.ps1 to get the template from a file named "template". I was able to do it manually in powershell when I tried to execute the code below, it works.

    $var = (Get-Content .\$file -Raw | ConvertFrom-StringData)
    $template = @"
    var1 = $($var.var1)
    var2 = $($var.var2)
    var3 = $($var.var3)
    "@

    But when I try to do the one below (this is part of my .ps1 script)... It does not work.

    $var = (Get-Content .\$file -Raw | ConvertFrom-StringData)
    $template = 
    (Get-Content .\template -Raw)
    $template = @"
    $template
    "@

    Please advise. Thank you

    Monday, June 24, 2019 1:15 PM
  • It is impossible to understand what you are asking.  What is in your files?  What do you expect the output to be?

    To insert variable values into a string you will need to use the "-f" operator.


    \_(ツ)_/

    Monday, June 24, 2019 1:21 PM
  • Example:

    $template = @'
     var1 = {0}
     var2 = {1}
     var3 = {2}
    '@
    $template -f $var.var1, $var.var2, $var.var3
    This will also work when the template string is loaded from a file.


    \_(ツ)_/

    Monday, June 24, 2019 1:24 PM
  • It is impossible to understand what you are asking.  What is in your files?  What do you expect the output to be?

    To insert variable values into a string you will need to use the "-f" operator.


    \_(ツ)_/

    The situation is like this, I have multiple variable files, but I wanted to have a single variable file which contains all the variables of all those variable files so I can easily update the variables. I want to create a script which would create those variable files from the single variable file based on a template for each of those variable files.

    e.g
    # variable files
    component1
    component2
    component3

    # single variable file (where all the variables of all the components should be in)
    componentvariables

    # template for variable files (where I would "populate" the template from the values in the componentvariables file
    component1template
    component2template
    component3template

    # part of the script to create the file
    $template | Out-File -FilePath .\componentX

    My problem is that the contents of "componentX" is still the same as the componentXtemplate file and is not populated by the script. I have followed your instruction just now, and it is still the same. The contents in the componentX file is still
    var1 = {0}
    var2 = {1}
    var3 = {2}
    I have just learnt about the -f option, and I will also study further about other possibilities.
    • Edited by Luuuffy Monday, June 24, 2019 2:24 PM Update to make it clearer
    Monday, June 24, 2019 2:08 PM
  • Example:

    $template = @'
     var1 = {0}
     var2 = {1}
     var3 = {2}
    '@
    $template -f $var.var1, $var.var2, $var.var3
    This will also work when the template string is loaded from a file.


    \_(ツ)_/

    I have tested this, and it works. Are there any other options that would allow it to be the variable names instead from the hashtable gotten at "$var = (Get-Content .\$file -Raw | ConvertFrom-StringData)" instead of just {0,1,2,x...} to make it $($var.var1), $($var.var2) to make it easier to understand? 

    Thank you so much.
    Monday, June 24, 2019 2:10 PM
  • Without any idea of the purpose for all of this it is not possible to give you any answer. 

    I recommend that you populate variables from more standard places like a CSV or a database.  This would make things more reasonable and understandable. 


    \_(ツ)_/

    Monday, June 24, 2019 2:15 PM
  • Without any idea of the purpose for all of this it is not possible to give you any answer. 

    I recommend that you populate variables from more standard places like a CSV or a database.  This would make things more reasonable and understandable. 


    \_(ツ)_/

    The purpose of this is to make the updates of all those variable files easier. It is a little bit time consuming and more prone to human error if I update multiple variable files one by one. So instead, I planned to create a single variable file which contains all the variables in those different variable files, create a script that will create those variable files from a template which gets the values from the single variable file. I hope it is clearer by now.

    Thank you.
    Monday, June 24, 2019 2:34 PM
  • What are "variable files?  Are these PowerShell variables?  You have to have some design requirements that are complete and not just a loose idea with little definition.

    Are these variables for some other system?  What is it that needs to be done and why?


    \_(ツ)_/


    • Edited by jrv Monday, June 24, 2019 2:51 PM
    Monday, June 24, 2019 2:50 PM
  • What are "variable files?  Are these PowerShell variables?  You have to have some design requirements that are complete and not just a loose idea with little definition.

    Are these variables for some other system?  What is it that needs to be done and why?


    \_(ツ)_/


    I am sorry for not being so clear. These variable files are .tfvars for deployment using terraform. These variable files are separated by component.

    root/
    deployment/
      - component1
        - var file
      - component2
        - var file
      - component3
        - var file

    I want to have a variable file in the root directory that will create the var files in each component using a script with a template.

    root/
    varfile
    generate.ps1
    deployment/
      - component1
        - template for the varfile for this component
      - component2
        - template for the varfile for this component
      - component3
        - template for the varfile for this component.

    generate.ps1 will "source" the varfile, and (create the var file using the template or put the needed variables in the template for each component.)
    The templates contain the needed variables to be used for the component which will be "populated" by the script. It is like putting the values in the template. I am sorry I do not know much of the terms yet as I am new to this. Thank you

    Tuesday, June 25, 2019 5:07 AM
  • You have all of the information needed to do this.  You just have to write the script.  WE cannot guess at what you are trying to do.


    \_(ツ)_/

    Tuesday, June 25, 2019 5:10 AM
  • You have all of the information needed to do this.  You just have to write the script.  WE cannot guess at what you are trying to do.


    \_(ツ)_/

    Hello, I was able to do this using a bash script instead. Before, I wanted to do this using a powershell script, but I resorted to bash... It would be nice if someone could create a powershell script equivalent.

    gen.sh
    ########
    #!/bin/bash
    filename=$1
    source $filename

    temp=$(cat file.template)

    eval "echo \"${temp}\"" > file.ts
    ########

    file.template
    ########
    export const env = {
      test: $isTest,
      vars: {
        var1: '${var1}',
        var2: '${var2}',
        var3: '${var3}
      }
    };
    ########


    $filename
    ########
    export isTest = false
    export var1 = "var1"
    export var2 = "var2"
    export var3 = "var3"
    ########

    Thursday, June 27, 2019 8:36 AM