Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The -NonInteractive Parameter should be added by default when starting a powershell shell #12637

Open
MBradbury opened this issue Sep 27, 2023 · 3 comments

Comments

@MBradbury
Copy link

When a script called by the Powershell provisioner includes a command that involves a y/n prompt (or other kinds of interaction (e.g., Read-Host) the Powershell script will appear to hang as that script will wait indefinitely for the input. The -NonInteractive Parameter should be added by default when using the Powershell provisioner. This would result in an error instead of the apparent hanging when using commands requiring interactivity.

This can currently be worked around by specifying a custom execute_command, but it would be preferable to set the parameter by default.

Originally posted by @floh96 in #4916 (comment)

@nywilken
Copy link
Contributor

nywilken commented Oct 17, 2023

Hi @MBradbury thanks for opening up this request. You mentioned the workaround, which is is the first thing that came to mind. But I understand why folks would want that to be the default.

I've labelled the issue in case you or a member of the community is interested in adding the feature. We are prioritizing work on removing bundled plugins and improving HCL so we wont be able to work on it soon.

I'm not a Powershell user but I am curious if this is something that could be accomplished from within a user provided powershell script? Or if it has to be a flag on the powershell command?

@MBradbury
Copy link
Author

MBradbury commented Oct 17, 2023

I think it would be sufficient to update https://github.com/hashicorp/packer/blob/main/provisioner/powershell/provisioner.go#L122 and https://github.com/hashicorp/packer/blob/main/provisioner/powershell/provisioner.go#L124 to include the flag before -executionpolicy, but I don't have an environment to compile and test Go.

From a quick search, I have found this, so it doesn't look like it is possible to set -NonInteractive in the script itself.

The workaround I have been using would be to use something similar to:

provisioner "powershell" {
        elevated_user     = var.winrm_username
        elevated_password = var.winrm_password
        scripts           = var.scripts
        execute_command   = "powershell -NonInteractive -executionpolicy bypass \"& { if (Test-Path variable:global:ProgressPreference){$ProgressPreference='SilentlyContinue'};. {{.Vars}}; &'{{.Path}}'; exit $LastExitCode }\""
}

@mr-aboutin
Copy link
Contributor

Hi @nywilken

Tought about clearing that quick fix, adding the -NonInteractive to the command does cause an error in case the command needs input, I was wondering if there is any test where we actually run commands trough powershell or we simply compare the command string?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants