Vscode-powershell: Pass parameters to PowerShell.exe (specifically NonInteractive)

Created on 7 Aug 2018  路  5Comments  路  Source: PowerShell/vscode-powershell

PowerShell.exe supports various command-line flags. It would be beneficial to allow control over these arguments. Specifically, I want to run powershell.exe -NonInteractive. This is especially useful when powershell.debugging.createTemporaryIntegratedConsole is set to true so you get an isolated run with the exact environment you want.

I tried using the args setting in launch.json but that seems to pass parameters directly to my script, after powershell.exe has already been loaded (with parameters I have no control over).

Area-Integrated Console Issue-Enhancement Up-for-Grabs

Most helpful comment

Rather than check the command line parameters, I would check $Host.Name and in the case of ConsoleHost this property and this property might be indicative.

I've often wished there was a public property for this :/

Even if you're willing to use reflection, this is a bigger pain than usual to get to because it's not on InternalHost. So you gotta go through Host.ExternalHost.UI.NoPrompt. It'll also fall apart when in remoting scenarios, though I suppose you can guess that if the host actually changed then it's interactive. Edit: I guess you can't can you? Host probably still changes even in Invoke-Command?

Generally though, I'm not sure how we would best emulate non-interactive startup, because it's a startup option -- there's no big switch inside the runtime to turn it on and off. I suspect that perfecting attach-to-process debugging is going to be the best option here

The cheapest way imo would be to add a property to Host.PrivateData and check it on host UI calls like ConsoleHost does with NoPrompt.

I'd be interested to hear some specifics about use cases though. I'm not sure I understand the value.

Note that what I'm proposing wouldn't really emulate it in a way that would solve your problem @gmckeown. I think it's safe to say that there is currently no reliable or supported way of "detecting" non-interactive and best practices would recommend against attempting to account for it.

All 5 comments

Technically we already do pass -NonInteractive, but only because we use a custom PSHost.

We should definitely look into implementing a host that mirrors the default -NonInteractive experience though.

Related to the comment from @SeeminglyScience, I've got a bunch of PowerShell scripts that are typically executed by an automation platform with "-noninteractive". These scripts rely on some files containing secure-string credentials when running automatically, but I've just been investigating ways to allow them to prompt for credentials when run interactively from the command-line.

Rightly or wrongly, I'm checking for the presence of "-noninteractive" on the command-line, and this works well from a standalone PowerShell instance. Inside VSCode, however, the script thinks its being run on the automation platform and so doesn't prompt for creds.

I'll find a work-around for now, but "a host that mirrors the default -NonInteractive experience" sounds helpful to me!

Rather than check the command line parameters, I would check $Host.Name and in the case of ConsoleHost this property and this property might be indicative.

Generally though, I'm not sure how we would best emulate non-interactive startup, because it's a startup option -- there's no big switch inside the runtime to turn it on and off. I suspect that perfecting attach-to-process debugging is going to be the best option here

Rather than check the command line parameters, I would check $Host.Name and in the case of ConsoleHost this property and this property might be indicative.

I've often wished there was a public property for this :/

Even if you're willing to use reflection, this is a bigger pain than usual to get to because it's not on InternalHost. So you gotta go through Host.ExternalHost.UI.NoPrompt. It'll also fall apart when in remoting scenarios, though I suppose you can guess that if the host actually changed then it's interactive. Edit: I guess you can't can you? Host probably still changes even in Invoke-Command?

Generally though, I'm not sure how we would best emulate non-interactive startup, because it's a startup option -- there's no big switch inside the runtime to turn it on and off. I suspect that perfecting attach-to-process debugging is going to be the best option here

The cheapest way imo would be to add a property to Host.PrivateData and check it on host UI calls like ConsoleHost does with NoPrompt.

I'd be interested to hear some specifics about use cases though. I'm not sure I understand the value.

Note that what I'm proposing wouldn't really emulate it in a way that would solve your problem @gmckeown. I think it's safe to say that there is currently no reliable or supported way of "detecting" non-interactive and best practices would recommend against attempting to account for it.

@rjmholt @SeeminglyScience Thanks for the insight!

Was this page helpful?
0 / 5 - 0 ratings