PowerShell script with input parameters with packer(.json)












-1















I have this Ps1 script,



param   ([string] $userpassword, [string] $UserName)

Write-host "Start.." (Get-date)

Write-host "User Name is "$UserName Write-host "User password is
"$userpassword

if ($userpassword.length -lt 0) {
Write-host "Please enter password!"$UserName }

Write-host "End.." (Get-date)


packer(.json) as follows:



"provisioners":    [
{
"type": "powershell",
"environment_vars":
[
"userpassword=********",
"UserName=ABC"
],
"scripts":
[
"test.ps1"
]
} ]


.Ps1 can not read input para from packer(.json)...










share|improve this question





























    -1















    I have this Ps1 script,



    param   ([string] $userpassword, [string] $UserName)

    Write-host "Start.." (Get-date)

    Write-host "User Name is "$UserName Write-host "User password is
    "$userpassword

    if ($userpassword.length -lt 0) {
    Write-host "Please enter password!"$UserName }

    Write-host "End.." (Get-date)


    packer(.json) as follows:



    "provisioners":    [
    {
    "type": "powershell",
    "environment_vars":
    [
    "userpassword=********",
    "UserName=ABC"
    ],
    "scripts":
    [
    "test.ps1"
    ]
    } ]


    .Ps1 can not read input para from packer(.json)...










    share|improve this question



























      -1












      -1








      -1








      I have this Ps1 script,



      param   ([string] $userpassword, [string] $UserName)

      Write-host "Start.." (Get-date)

      Write-host "User Name is "$UserName Write-host "User password is
      "$userpassword

      if ($userpassword.length -lt 0) {
      Write-host "Please enter password!"$UserName }

      Write-host "End.." (Get-date)


      packer(.json) as follows:



      "provisioners":    [
      {
      "type": "powershell",
      "environment_vars":
      [
      "userpassword=********",
      "UserName=ABC"
      ],
      "scripts":
      [
      "test.ps1"
      ]
      } ]


      .Ps1 can not read input para from packer(.json)...










      share|improve this question
















      I have this Ps1 script,



      param   ([string] $userpassword, [string] $UserName)

      Write-host "Start.." (Get-date)

      Write-host "User Name is "$UserName Write-host "User password is
      "$userpassword

      if ($userpassword.length -lt 0) {
      Write-host "Please enter password!"$UserName }

      Write-host "End.." (Get-date)


      packer(.json) as follows:



      "provisioners":    [
      {
      "type": "powershell",
      "environment_vars":
      [
      "userpassword=********",
      "UserName=ABC"
      ],
      "scripts":
      [
      "test.ps1"
      ]
      } ]


      .Ps1 can not read input para from packer(.json)...







      powershell






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 10:51









      Paxz

      2,302826




      2,302826










      asked Nov 23 '18 at 10:43









      dhaneenjadhaneenja

      1




      1
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.



          The syntax in Powershell for reading an environment variable is $env:variablename.





          You can either set the param default values to be the env var:



          param(
          [string] $userpassword = $env:userpassword,
          [string] $UserName = $env:username
          )

          Write-host "Start.." (Get-date)
          [...]


          Or scrap the param block and just assign the variables directly:



          [string] $userpassword = $env:userpassword
          [string] $UserName = $env:username

          Write-host "Start.." (Get-date)
          [...]





          share|improve this answer

































            0














            Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.



            Packer does support this but only for inline scripts.
            It does not matter if the parameter is an environment variable or direct entry.
            the key thing is to ensure that the script runs from the building machine.



            It's a 2 step process.




            1. Copy the file up to the machine.

            2. Run the file inline passing params.


            Eg






            "provisioners": [
            {
            "type": "file",
            "source": "c:myscriptsMyPowerShellScript.ps1",
            "destination": "c:tempMyPowerShellScript.ps1",
            "direction": "upload"
            },
            {
            "type": "shell",
            "inline": [
            "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
            ]
            },








            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445151%2fpowershell-script-with-input-parameters-with-packer-json%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.



              The syntax in Powershell for reading an environment variable is $env:variablename.





              You can either set the param default values to be the env var:



              param(
              [string] $userpassword = $env:userpassword,
              [string] $UserName = $env:username
              )

              Write-host "Start.." (Get-date)
              [...]


              Or scrap the param block and just assign the variables directly:



              [string] $userpassword = $env:userpassword
              [string] $UserName = $env:username

              Write-host "Start.." (Get-date)
              [...]





              share|improve this answer






























                0














                Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.



                The syntax in Powershell for reading an environment variable is $env:variablename.





                You can either set the param default values to be the env var:



                param(
                [string] $userpassword = $env:userpassword,
                [string] $UserName = $env:username
                )

                Write-host "Start.." (Get-date)
                [...]


                Or scrap the param block and just assign the variables directly:



                [string] $userpassword = $env:userpassword
                [string] $UserName = $env:username

                Write-host "Start.." (Get-date)
                [...]





                share|improve this answer




























                  0












                  0








                  0







                  Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.



                  The syntax in Powershell for reading an environment variable is $env:variablename.





                  You can either set the param default values to be the env var:



                  param(
                  [string] $userpassword = $env:userpassword,
                  [string] $UserName = $env:username
                  )

                  Write-host "Start.." (Get-date)
                  [...]


                  Or scrap the param block and just assign the variables directly:



                  [string] $userpassword = $env:userpassword
                  [string] $UserName = $env:username

                  Write-host "Start.." (Get-date)
                  [...]





                  share|improve this answer















                  Unlike a Unix shell script, Powershell variables (and params) aren't environment variables.



                  The syntax in Powershell for reading an environment variable is $env:variablename.





                  You can either set the param default values to be the env var:



                  param(
                  [string] $userpassword = $env:userpassword,
                  [string] $UserName = $env:username
                  )

                  Write-host "Start.." (Get-date)
                  [...]


                  Or scrap the param block and just assign the variables directly:



                  [string] $userpassword = $env:userpassword
                  [string] $UserName = $env:username

                  Write-host "Start.." (Get-date)
                  [...]






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 23 '18 at 11:13

























                  answered Nov 23 '18 at 11:02









                  James C.James C.

                  8,39222030




                  8,39222030

























                      0














                      Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.



                      Packer does support this but only for inline scripts.
                      It does not matter if the parameter is an environment variable or direct entry.
                      the key thing is to ensure that the script runs from the building machine.



                      It's a 2 step process.




                      1. Copy the file up to the machine.

                      2. Run the file inline passing params.


                      Eg






                      "provisioners": [
                      {
                      "type": "file",
                      "source": "c:myscriptsMyPowerShellScript.ps1",
                      "destination": "c:tempMyPowerShellScript.ps1",
                      "direction": "upload"
                      },
                      {
                      "type": "shell",
                      "inline": [
                      "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                      ]
                      },








                      share|improve this answer




























                        0














                        Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.



                        Packer does support this but only for inline scripts.
                        It does not matter if the parameter is an environment variable or direct entry.
                        the key thing is to ensure that the script runs from the building machine.



                        It's a 2 step process.




                        1. Copy the file up to the machine.

                        2. Run the file inline passing params.


                        Eg






                        "provisioners": [
                        {
                        "type": "file",
                        "source": "c:myscriptsMyPowerShellScript.ps1",
                        "destination": "c:tempMyPowerShellScript.ps1",
                        "direction": "upload"
                        },
                        {
                        "type": "shell",
                        "inline": [
                        "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                        ]
                        },








                        share|improve this answer


























                          0












                          0








                          0







                          Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.



                          Packer does support this but only for inline scripts.
                          It does not matter if the parameter is an environment variable or direct entry.
                          the key thing is to ensure that the script runs from the building machine.



                          It's a 2 step process.




                          1. Copy the file up to the machine.

                          2. Run the file inline passing params.


                          Eg






                          "provisioners": [
                          {
                          "type": "file",
                          "source": "c:myscriptsMyPowerShellScript.ps1",
                          "destination": "c:tempMyPowerShellScript.ps1",
                          "direction": "upload"
                          },
                          {
                          "type": "shell",
                          "inline": [
                          "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                          ]
                          },








                          share|improve this answer













                          Whilst the question is not very clear I surmise from the title that you wish to run a PowerShell script under packer passing parameters to it.



                          Packer does support this but only for inline scripts.
                          It does not matter if the parameter is an environment variable or direct entry.
                          the key thing is to ensure that the script runs from the building machine.



                          It's a 2 step process.




                          1. Copy the file up to the machine.

                          2. Run the file inline passing params.


                          Eg






                          "provisioners": [
                          {
                          "type": "file",
                          "source": "c:myscriptsMyPowerShellScript.ps1",
                          "destination": "c:tempMyPowerShellScript.ps1",
                          "direction": "upload"
                          },
                          {
                          "type": "shell",
                          "inline": [
                          "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                          ]
                          },








                          "provisioners": [
                          {
                          "type": "file",
                          "source": "c:myscriptsMyPowerShellScript.ps1",
                          "destination": "c:tempMyPowerShellScript.ps1",
                          "direction": "upload"
                          },
                          {
                          "type": "shell",
                          "inline": [
                          "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                          ]
                          },





                          "provisioners": [
                          {
                          "type": "file",
                          "source": "c:myscriptsMyPowerShellScript.ps1",
                          "destination": "c:tempMyPowerShellScript.ps1",
                          "direction": "upload"
                          },
                          {
                          "type": "shell",
                          "inline": [
                          "c:tempMyPowerShellScript.ps1 Param1 {{user `Param2_from_an_env_var`}}"
                          ]
                          },






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jan 6 at 14:58









                          Marcus AdamsMarcus Adams

                          114




                          114






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53445151%2fpowershell-script-with-input-parameters-with-packer-json%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Berounka

                              Fiat S.p.A.

                              Type 'String' is not a subtype of type 'int' of 'index'