@@ -11,6 +11,7 @@ import (
1111 "context"
1212 "errors"
1313 "fmt"
14+ "io"
1415 "log"
1516 "os"
1617 "path/filepath"
@@ -265,9 +266,43 @@ func (p *Provisioner) Prepare(raws ...interface{}) error {
265266 return nil
266267}
267268
269+ // extractScript prepares a temporary PowerShell script by prepending environment setup and debug config.
270+ // It copies the contents of the provided script file into the temp script and returns its path.
271+ func extractScript (p * Provisioner , script string ) (string , error ) {
272+ temp , err := tmp .File ("powershell-provisioner-script" )
273+ if err != nil {
274+ return "" , err
275+ }
276+
277+ defer temp .Close ()
278+
279+ baseString := `if (Test-Path variable:global:ProgressPreference)` +
280+ `{set-variable -name variable:global:ProgressPreference -value 'SilentlyContinue'};`
281+
282+ if p .config .DebugMode != 0 {
283+ baseString += fmt .Sprintf (`Set-PsDebug -Trace %d;` , p .config .DebugMode )
284+ }
285+ baseString += p .createFlattenedEnvVars (p .config .ElevatedUser != "" )
286+ if _ , err := temp .WriteString (baseString ); err != nil {
287+ return "" , fmt .Errorf ("Error writing PowerShell script: %w" , err )
288+ }
289+
290+ f , err := os .Open (script )
291+ if err != nil {
292+ return "" , fmt .Errorf ("Error opening powershell script: %s" , err )
293+ }
294+ defer f .Close ()
295+ if _ , err := io .Copy (temp , f ); err != nil {
296+ return "" , fmt .Errorf ("Error copying script contents: %w" , err )
297+ }
298+
299+ return temp .Name (), nil
300+
301+ }
302+
268303// Takes the inline scripts, adds a wrapper around the inline scripts, concatenates them into a temporary file and
269304// returns a string containing the location of said file.
270- func extractScript (p * Provisioner ) (string , error ) {
305+ func extractInlineScript (p * Provisioner ) (string , error ) {
271306 temp , err := tmp .File ("powershell-provisioner" )
272307 if err != nil {
273308 return "" , err
@@ -280,7 +315,7 @@ func extractScript(p *Provisioner) (string, error) {
280315 // we concatenate all the inline commands
281316 for _ , command := range p .config .Inline {
282317 log .Printf ("Found command: %s" , command )
283- if _ , err := commandBuilder .WriteString (command ); err != nil {
318+ if _ , err := commandBuilder .WriteString (command + " \n \t " ); err != nil {
284319 return "" , fmt .Errorf ("failed to wrap script contents: %w" , err )
285320 }
286321 }
@@ -310,11 +345,12 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe
310345 p .communicator = comm
311346 p .generatedData = generatedData
312347
313- scripts := make ( []string , len ( p . config . Scripts ))
314- copy ( scripts , p . config . Scripts )
315-
348+ var scripts []string
349+ // maps temp script paths to original script paths
350+ tempToOriginalScriptMap := make ( map [ string ] string )
316351 if p .config .Inline != nil {
317- temp , err := extractScript (p )
352+ temp , err := extractInlineScript (p )
353+ tempToOriginalScriptMap [temp ] = temp
318354 if err != nil {
319355 ui .Error (fmt .Sprintf ("Unable to extract inline scripts into a file: %s" , err ))
320356 }
@@ -323,12 +359,27 @@ func (p *Provisioner) Provision(ctx context.Context, ui packersdk.Ui, comm packe
323359 defer os .Remove (temp )
324360 }
325361
362+ if len (p .config .Scripts ) > 0 {
363+
364+ for _ , script := range p .config .Scripts {
365+ temp , err := extractScript (p , script )
366+ tempToOriginalScriptMap [temp ] = script
367+ if err != nil {
368+ ui .Error (fmt .Sprintf ("Unable to extract script into a file: %s" , err ))
369+ }
370+ scripts = append (scripts , temp )
371+ // Defer removal until the function exits
372+ defer os .Remove (temp )
373+
374+ }
375+ }
376+
326377 // every provisioner run will only have one env var script file so lets add it first
327378 uploadedScripts := []string {p .config .RemoteEnvVarPath }
328379 for _ , path := range scripts {
329- ui .Say (fmt .Sprintf ("Provisioning with powershell script: %s" , path ))
380+ ui .Say (fmt .Sprintf ("Provisioning with powershell script: %s" , tempToOriginalScriptMap [ path ] ))
330381
331- log .Printf ("Opening %s for reading" , path )
382+ log .Printf ("Opening %s for reading" , tempToOriginalScriptMap [ path ] )
332383 fi , err := os .Stat (path )
333384 if err != nil {
334385 return fmt .Errorf ("Error stating powershell script: %s" , err )
0 commit comments