Repository navigation
Closed
Labels
Issue-Enhancementthe issue is more of a feature request than a bugWG-Enginecore PowerShell engine, interpreter, and runtime
Description
Come form #9362
The comment in code says that interactive input should works
-
with -File:
PowerShell/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
Lines 1133 to 1141 in ceed73d
// Process interactive input... if (args[i] == "-") { // the arg to -file is -, which is secret code for "read the commands from stdin with prompts" _explicitReadCommandsFromStdin = true; _noPrompt = false; } else -
with -Command:
PowerShell/src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs
Lines 1296 to 1304 in ceed73d
else if (args[i] == "-") { // the arg to -command is -, which is secret code for "read the commands from stdin with no prompts" _explicitReadCommandsFromStdin = true; _noPrompt = true; ++i; if (i != args.Length)
The comments differ in "no prompts" vs "with prompts" - it is not clear is it comments and behaviors right.
Also using dash is documented only for -Command (not for -File)
https://docs.microsoft.com/en-us/powershell/scripting/components/console/powershell.exe-command-line-help?view=powershell-6#-command
Steps to reproduce
pwsh -file C:\temp\q1.ps1 -Expected behavior
pwsh -file C:\1\q1.ps1 -
<wait interactive inout>
Actual behavior
pwsh -file C:\1\q1.ps1 -
-
Expected behavior
pwsh -command -
<wait interactive inout>
Actual behavior
'-' was specified as the argument to -Command but standard input has not been redirected for this process.
Usage: pwsh[.exe] [[-File] <filePath> [args]]
[-Command { - | <script-block> [-args <arg-array>]
| <string> [<CommandParameters>] } ]
[-ConfigurationName <string>] [-CustomPipeName <string>]
[-EncodedCommand <Base64EncodedCommand>]
[-ExecutionPolicy <ExecutionPolicy>] [-InputFormat {Text | XML}]
[-Interactive] [-NoExit] [-NoLogo] [-NonInteractive] [-NoProfile]
[-OutputFormat {Text | XML}] [-Version] [-WindowStyle <style>]
[-WorkingDirectory <directoryPath>]
pwsh[.exe] -h | -Help | -? | /?
PowerShell Online Help https://aka.ms/pscore6-docs
All parameters are case-insensitive.
-
Environment data
Name Value
---- -----
PSVersion 6.2.0-rc.1
PSEdition Core
GitCommitId 6.2.0-rc.1-204-g262d86c691519ff7d5adacbae6424e2c87accce6
OS Microsoft Windows 10.0.17763
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Reacted by Josh Corrick
Metadata
Metadata
Assignees
Labels
Issue-Enhancementthe issue is more of a feature request than a bugWG-Enginecore PowerShell engine, interpreter, and runtime
Activity
iSazonov commentedon Apr 29, 2019
Perhaps we could discuss this in #3223.
mklement0 commentedon Apr 29, 2019
The
-inpwsh -file C:\temp\q1.ps1 -is just a positional argument;-is only special as the first argument after-fileor-command; that is, if it is specified instead of a file name / code snippet.Do note that the primary use of these idioms is to provide input via stdin / the pipeline, not interactive use.
Given that, when called from the outside,
-should represent stdin, the following commands should all be equivalent on Unix:pwsh -noprofile -file -is broken (as ispwsh -noprofile -command -, as demonstrated in your OP).pwsh -noprofile -file /dev/stdinworks in principle with interactive input, but, once the input is submitted with ^D, it then displays pseudo-interactive output behavior, as discussed in #3223.(As an aside,
bashactually prompts line by line in this scenario, because, in the Unix world, non-redirected, i.e., interactive stdin input is processed line by line, whereas redirected stdin is typically processed as a whole. Again, however: interactive input is not the typical or useful use case - if you want to submit commands interactively, one by one, just start a new session withpwsh.)Returning to the typical use case:
The following two commands should be equivalent (with the exception of what the script reports in
$PSCommandPath,$PSScriptRoot, ...):pwsh -noprofile -file foo.ps1cat foo.ps1 | pwsh -noprofile -file -It is what enables the powerful
curl ... | shinstall-from-the-web idiom in POSIX-like shells - that is, feeding the content of a script to the shell rather than needing to provide a file['s path].If
-file -worked that way, there would really be no need for-command -at all.Given that we cannot remove support for it, it should be made to work the same, but [soft]-deprecated.
There's also no reason to disallow arguments to follow
-file -- it's an unnecessary restriction that violates the equivalence of the two commands above. It prevents passing the script content via stdin / the pipeline while also accepting arguments.(Given how
-commandcurrently works, there is a good reason to disallow following-command -with arguments (the way that-commandcurrently works - stitching together a script from all arguments following-command- is highly unfortunate, but cannot be changed), but if we fix-file -, that restriction can then easily be bypassed by using-file -instead).Note that it still makes sense to combine
-command- not with-- with stdin input: The-commandargument then specifies the command to run, as usual, and stdin input becomes its pipeline input.Here's a
bashexample:Regrettably, stdin input passed to
pwshcurrently does not become pipeline input - see #9497 - so that the following does not work analogously:As a doleful aside: if you tried to run this from PowerShell, the broken quoting for external programs (PowerShell itself, in this case), would make the command fail.
iSazonov commentedon Apr 29, 2019
Documentation is silent about it that confuses me. Make sense to open new issue in Docs repo?
The question remains about the input from the console. Although it could also be documented that it is not supported.
mklement0 commentedon Apr 29, 2019
The docs definitely need improvement: The CLI help doesn't mention
-File -at all, and doesn't mention the requirement of redirected stdin input for-Command -The current behavior is conceptually backwards and fundamentally broken, as shown in #3223:
It is designed around line-by-line processing, so clearly focused on interactive use.
It is
-Command -that is implemented with redirected stdin input in mind - yet line-by-line process is still applied (but at least the prompt string isn't printed the way it is with-File -), unlike when you pass a multi-line command string as an argument.-File -makes more sense for redirected stdin input - and it is the only form needed.pwsh -File -without redirected stdin input simply starts a regular session - the same as not specifying-File -at all.While technically somewhat defensible, it's certainly surprising.
There is a way in which
-File -could be useful with interactive input:If it allowed you to type an entire script ad hoc, and then submit it at once - it is what
pwsh -File /dev/stinalready does on Unix, albeit accidentally:This is what
rubysupports, for instance.The caveat is that this form of supplying multi-line stdin input is probably more familiar to Unix users than Windows users, and it would be a first in the context of PowerShell.
(I suppose submitting an empty extra line could be taken as the end-of-input signal, the same way as with array-valued mandatory parameter prompts, but that preclude use of blank lines for readability.)
[-]Iinteractive input should works with -File parameter[/-][+]Interactive input should works with -File parameter[/+]