Powershell: Can pscore support powershell-style spawning?

Created on 28 May 2019  路  3Comments  路  Source: PowerShell/PowerShell

Steps to reproduce

$ pwsh.exe -Command Start-Process -Wait -NoNewWindow pwsh.exe -ArgumentList 'Get-Help'
The argument 'Get-Help' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Expected behavior

The commands output being (truncated)


TOPIC
    PowerShell Help System

SHORT DESCRIPTION
    Displays help about PowerShell cmdlets and concepts.

LONG DESCRIPTION
    PowerShell Help describes PowerShell cmdlets,

Actual behavior

The commands output being

The argument 'Get-Help' is not recognized as the name of a script file. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

Environment data


Name                           Value
----                           -----
PSVersion                      6.2.1
PSEdition                      Core
GitCommitId                    6.2.1
OS                             Microsoft Windows 10.0.17134
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Issue-Question Resolution-Answered

Most helpful comment

pwsh now defaults to -File rather than to -Command, which is why you must include -Command in your -ArgumentList in order for Get-Help to be interpreted as a a _command_ rather than a _script file_:

In order to do that, additionally enclose the entire original -Command argument in "...":

pwsh -Command "Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-Command', 'Get-Help'"

All 3 comments

pwsh now defaults to -File rather than to -Command, which is why you must include -Command in your -ArgumentList in order for Get-Help to be interpreted as a a _command_ rather than a _script file_:

In order to do that, additionally enclose the entire original -Command argument in "...":

pwsh -Command "Start-Process -Wait -NoNewWindow pwsh -ArgumentList '-Command', 'Get-Help'"

This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.

So @mklement0, what you're saying is, there is basically no way to fully translate, i. e. the following pre-pscore expression without much adding quotes (note the powershell.exe instance)?

pwsh.exe -NoProfile -Command "Start-Process" -Wait -NoNewWindow powershell.exe -ArgumentList 'Set-ClipboardText','-Value','$(([string](pwsh" "-Command" "Get-ClipboardText)).Replace(''" "'',''\\" \\"''))'

Have this to translate a list of full paths to files in Intellij to a format needed by some app. The problem with adding more double quotes here is, that Intellij applies some semantics already a bit different to what one expected would a different shell be used instead of powershell.

EDIT: This is what I got running in a bare pwsh session so far, have to figure it out how to adapt it for use with IntelliJ:

pwsh.exe -NoProfile -Command "Start-Process -Wait -NoNewWindow pwsh.exe -ArgumentList '-Command', 'Set-ClipboardText', '''$(([string] (Get-ClipboardText)) -replace "`n|`r")'''"

Clarification:
What I needed was a powershell expression that turns a linefeed-separated list of files into one separated by the string " " (double quote space double quote).
The one I came up with finally (no powershell.exe anymore) to use in Intellij is:

pwsh.exe -NoProfile -Command "Start-Process -Wait -NoNewWindow pwsh.exe -ArgumentList '-Command', 'Set-ClipboardText', '$(([string] (Get-ClipboardText)) -replace ''`n|`r'' -replace ''" "'',''\\" \\"'')'"
  • left the task to fit it back to bare pwsh sessions to the potential reader.
Was this page helpful?
0 / 5 - 0 ratings