Running PowerShell script as post processor

1,369 views
Skip to first unread message

Dan Franciscus

unread,
May 4, 2018, 12:49:46 PM5/4/18
to Packer
Hi all,

Can someone give me an example of a proper way to run a PowerShell script for a post processor on Windows? This does not seem to be working...

"post-processors": [
{
"type": "shell-local",
"execute_command": "PowerShell -File A:/Test.ps1",
"inline": ["Test"]
}
],

Megan Marsh

unread,
May 4, 2018, 1:02:57 PM5/4/18
to packe...@googlegroups.com
Turns out google blocked me attaching the packer binary; I'll attach it to the PR I linked above on GitHub

On Fri, May 4, 2018 at 10:01 AM, Megan Marsh <megan....@gmail.com> wrote:
I'm sorry to say it's not currently possible.  The shell-local post-processor has some hardcoded assumptions that the shell in question is sh:  https://github.com/hashicorp/packer/blob/4dcf9d760b2fd549ddad85837e78e0ed64620a5e/post-processor/shell-local/communicator.go#L16

But all is not lost. I have a pretty hefty rewrite of the code used for the shell-local provisioner and post-processors scheduled for the 1.3.0 release (https://github.com/hashicorp/packer/pull/5956) which, among other things, should make this possible. I've attached a windows build of that PR if you'd like to test it out; I could use some community validation that it solves these kinds of less common cases.

Example of running a powershell script on windows:
Required customizations: env_var_format and execute_command

      {
          "type": "shell-local",
          "environment_vars": ["SHELLLOCALTEST=ShellTest4"],
          "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
          "env_var_format": "$env:%s=\"%s\"; ",
          "script": "./scripts/example_ps.ps1"
      }

Example of running a powershell script on windows as "inline":
Required customizations: env_var_format, tempfile_extension, and execute_command

      {
          "type": "shell-local",
          "tempfile_extension": ".ps1",
          "environment_vars": ["SHELLLOCALTEST=ShellTest5"],
          "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
          "env_var_format": "$env:%s=\"%s\"; ",
          "inline": ["write-output $env:SHELLLOCALTEST"]
      }

Thanks!
Megan


--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/a641b13a-dee6-44ae-9b6d-4c0623bd8039%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Megan Marsh

unread,
May 4, 2018, 1:05:27 PM5/4/18
to packe...@googlegroups.com
Okay, trying one more time, now without the packer binary which Gmail decided was too dangerous to send.

_____

I'm sorry to say it's not currently possible.  The shell-local post-processor has some hardcoded assumptions that the shell in question is sh:  
https://github.com/hashicorp/packer/blob/4dcf9d760b2fd549ddad85837e78e0ed64620a5e/post-processor/shell-local/communicator.go#L16

But all is not lost. I have a pretty hefty rewrite of the code used for the shell-local provisioner and post-processors scheduled for the 1.3.0 release (https://github.com/hashicorp/packer/pull/5956) which, among other things, should make this possible. I've attached a windows build of that PR to that PR and you'll find it at the above link if you'd like to test it out; I could use some community validation that it solves these kinds of less common cases.


Example of running a powershell script on windows:
Required customizations: env_var_format and execute_command

      {
          "type": "shell-local",
          "environment_vars": ["SHELLLOCALTEST=ShellTest4"],
          "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
          "env_var_format": "$env:%s=\"%s\"; ",
          "script": "./scripts/example_ps.ps1"
      }

Example of running a powershell script on windows as "inline":
Required customizations: env_var_format, tempfile_extension, and execute_command

      {
          "type": "shell-local",
          "tempfile_extension": ".ps1",
          "environment_vars": ["SHELLLOCALTEST=ShellTest5"],
          "execute_command": ["powershell.exe", "{{.Vars}} {{.Script}}"],
          "env_var_format": "$env:%s=\"%s\"; ",
          "inline": ["write-output $env:SHELLLOCALTEST"]
      }

Thanks!

Dan Franciscus

unread,
May 4, 2018, 3:49:41 PM5/4/18
to Packer
Thanks Megan! I am new to Packer, like as in I just started using it today so not sure I would be the best person to test at the moment. I will take a look at this though.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.

Megan Marsh

unread,
May 4, 2018, 4:18:06 PM5/4/18
to packe...@googlegroups.com
Sounds good to me :) . This is a great list to reach out to if you have any questions. I assume you've seen our getting-started tutorial in the docs, but just in case: https://www.packer.io/intro/getting-started/build-image.html

Dan Franciscus

unread,
May 8, 2018, 8:08:01 AM5/8/18
to Packer
Hi Megan,

So upon some further learning and testing, what I am trying to accomplish ultimately is capturing a reference machine for Microsoft Deployment Toolkit. I know this is a weird use case and not something Packer was really built for, but I figured I would ask for some help. To capture an image, the VM runs a VBS script on a CIFS share (The MDT server). Which then starts the capturing process (such as sysprep and creating a .wim file). I actually have this working fine, but its a hack because I am actually just using a provisioner which is  PowerShell script mapping the CIFS share and kicking off the VBS script. I was wondering if you knew of a better way to handle this. The PowerShell post-processor might not work because I really need a shell on the packer VM, not my local.

"provisioners": [
{
"scripts": [
"./scripts/MDT.ps1"
],
"type": "powershell"
}

The MDT script is as this:

net use s: '\\wds-1.itg.ias.edu\win10capture$' /user:NTDOMAIN\wds 'PW'
Set-Location -Path S:\Scripts\
cscript .\litetouch.vbs
Start-Sleep -Seconds 3600

Alvaro Miranda Aguilera

unread,
May 8, 2018, 12:14:55 PM5/8/18
to packe...@googlegroups.com
Hello

If you deploy a VM with packer, without any shell provisioner will be like this

- VM start
- Poweroff

If you add provisioners:

- VM start
- provision one
- provision two
- poweroff

the post-provisioner shell will run on the host, as the VM is powered off

so if you want to capture an estate of a VM, just make it the last shell provisioner step, will happen before power off


not sure if I am missing something here, if I am, can you explain better whats is missed using the normal provisioner?

Alvaro
Reply all
Reply to author
Forward
0 new messages