Set working directory for Shell and Powershell

22 views
Skip to first unread message

goffries

unread,
Jan 24, 2019, 5:32:07 PM1/24/19
to Packer
Is there a way to set the working directory that packer executes a script from? Ideally I'd like to to run the script out of the folder that the script is in.

Megan Marsh

unread,
Jan 24, 2019, 5:56:46 PM1/24/19
to packe...@googlegroups.com
You can probably use the `execute_command` option to have Packer change directories to the one you want to execute from.

On Thu, Jan 24, 2019 at 2:32 PM goffries <goff...@gmail.com> wrote:
Is there a way to set the working directory that packer executes a script from? Ideally I'd like to to run the script out of the folder that the script is in.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/462c1af2-f15c-490b-824a-c60d8d4512ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

goffries

unread,
Jan 24, 2019, 6:03:01 PM1/24/19
to Packer
Would you happen to have an example?

Like if i wanted to run this art of the windows-netfx3 folder?

        {
            "type": "powershell",
            "elevated_user": "{{user `admin_username` }}",
            "elevated_password": "{{.WinRMPassword}}",
            "inline": [
                "& {{user `scriptTargetPath`}}\\artifacts\\windows-netfx3\\windows-netfx3.ps1"
            ]
        }

Megan Marsh

unread,
Jan 24, 2019, 6:12:01 PM1/24/19
to packe...@googlegroups.com
Off the top of my head, it would look something like this, but you may have to tinker to get it working. I don't think your scriptTargetPath variable will be properly interpolated inside of the execute_command option, unfortunately, so you may have to hardcode the path.

       {
            "type": "powershell",
            "elevated_user": "{{user `admin_username` }}",
            "elevated_password": "{{.WinRMPassword}}",
            "execute_command": "powershell -executionpolicy bypass \"& {Set-Location -Path C:\\artifacts\\windows-netfx3;. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }\""
            "inline": [

goffries

unread,
Jan 24, 2019, 6:15:23 PM1/24/19
to Packer
Very interesting, thank you. One last question, do you know if "environment_vars":[] are accessible from the execute command? If so, maybe i could just stick scriptTargetPath there and call it with $env:scriptTargetPath?

Megan Marsh

unread,
Jan 24, 2019, 7:01:03 PM1/24/19
to packe...@googlegroups.com
execute_command is where we wet the environment vars -- in the example above, we dot-source them from a file using the ". {{.Vars}}" portion of the command -- {{ .Vars }} is the golang templating method for adding items into a string, and in this case .Vars gets replaced with a formatted version of the variables you supply in environment_vars.  I imagine you should be able to use the environment vars in the command, as long as you have already called the code that sources {{Vars}}

So you'd want something like 

"execute_command": "powershell -executionpolicy bypass \"& {. {{.Vars}}; Set-Location -Path C:\\artifacts\\windows-netfx3; &'{{.Path}}'; exit $LastExitCode }\""
Reply all
Reply to author
Forward
0 new messages