packer-community / packer-windows-plugins

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

Elevated Powershell scripts output progress bar as XML object #60

Open gildas opened 9 years ago

gildas commented 9 years ago

When running a powershell script in elevated mode, the output received by Packer contains the Progress object (System.Management.Automation.PSCustomObject) as an XML blob.

Example of a script that install .Net 3.5 via the Install-WindowsFeature Cmdlet (that produces a typical progress indicator in your Powershell session usually). Here is the output as shown when running packer:

==> virtualbox-windows-iso: Provisioning with shell script: ./scripts/install-dotnet35.ps1
    virtualbox-windows-iso: #< CLIXML
    virtualbox-windows-iso: Script started at 2015-06-01 02:14:53
    virtualbox-windows-iso: Installing .Net 3.5
    virtualbox-windows-iso: <Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><Obj S="progress" RefId="0"><TN RefId="0"><T>System.Management.Automation.PSCustomObject</T><T>System.Object</T></TN><MS><I64 N="
SourceId">1</I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI><PC>-1</PC><T>Completed</T><SR>-1</SR><SD> </SD></PR></MS></Obj><Obj S="progress" RefId="1"><TNRef RefId="0" /><MS><I64 N="SourceId">1</
I64><PR N="Record"><AV>Preparing modules for first use.</AV><AI>0</AI><Nil /><PI>-1</PI>...

I cut the XML as it is quite huge.

IMO, that XML should not be visible (at least) or process properly by showing some progress text + % value (that'd be the best).

mefellows commented 9 years ago

It's a good point, I'll mark this as an enhancement - I'm hoping its something trivial.

dragon788 commented 8 years ago

This also seems to cause failures if you upgrade PowerShell to 5 from a prior version on the next WinRM command that gets executed (file or script).

@mefellows seems this could be something in the underlying WinRb/WinRM gem. https://github.com/WinRb/winrm-fs/issues/18

mefellows commented 8 years ago

See mefellows/vagrant-dsc/issues/21. This looks to be the same issue.

agarstang commented 7 years ago

Hey, I've been playing around with this the last few days (as well as with PowerShell exit codes).

Passing -NoProfile to the powershell.exe works however this means that you have to quote the command passing to PowerShell. This means you can't pass {{ .Vars }} as the quotes that are passed in will not be escaped properly.

I'm happy to sacrifice having the environment variables to get cleaner output so my command looks like this.

"execute_command": "powershell.exe -NoProfile \"if (Test-Path variable:global:ProgressPreference){`$ProgressPreference='SilentlyContinue'}; & '{{.Path}}';exit `$LastExitCode\"; exit $LastExitCode",

I have kept the JSON so that the escape characters are a bit more obvious.