packer-community / packer-windows-plugins

A suite of Packer plugins for provisioning Windows machines
113 stars 20 forks source link

Having trouble with winrm and powershell scripts #35

Closed levpaul closed 9 years ago

levpaul commented 9 years ago

If I have a script like:

Start-Process -FilePath "powershell.exe" -ArgumentList @("/c", "sleep", "5") -Wait
Exit 1

And run it under the powershell provisioner, I see two errors. Firstly, the wait is not respected and also the return code is 0, so it passes.

I get each of those behaviour when I invoke the command through the winrm binary from the package that you're using for the communicator, so I will raise an issue there too. Not sure if you guys know if there's something I can add here to make it behave as I want?

I should note that this works for blocking the script:

$p = Start-Process -FilePath "powershell.exe" -ArgumentList @("/c", "sleep", "5") -Wait -PassThru
while ($p.HasExited -ne "True") {
  Start-Sleep -Milliseconds 500
}

Still can't work around the exit status though!

mefellows commented 9 years ago

Thanks @levilovelock, a bug was just raised (#34) which addresses the exit codes - this has been somewhat known for a while - good to be properly captured.

WRT the wait, it's likely to do with the fact we are running this process via another powershell process (https://github.com/packer-community/packer-windows-plugins/blob/master/provisioner/powershell/provisioner.go#L118).

If you could please submit a gist containing an example packer setup we can then work on making that pass.

Appreciate the help.

mefellows commented 9 years ago

FYI - the exit code problem should now be sorted (you can grab the latest binary from here: https://github.com/packer-community/packer-windows-plugins/releases/tag/v1.0.0-rc).

I'm not sure about the sleep, i'll have to have a bit more of a play to fully grasp the situation. I think the solution might be to wrap up the async command as you have done, however.

levpaul commented 9 years ago

Ah, thanks for the update! I actually managed to fix the problem by upgrading my powershell from 3.0 -> 4.0 ... -Wait seems to work fine now. Thanks again for your fast responses :)

mefellows commented 9 years ago

No worries - out of interest, was that on your host machine or the guest? Might be a good tip to put in our docu that is in need of some love.

levpaul commented 9 years ago

It was on the guest (amazon windows 2008r2 base image)

mefellows commented 9 years ago

Thanks @levilovelock, noted. Closing this one for now.