Skip to content

Add a Windows mode for native commands that allows some commands to use legacy argument passing - #15408

Merged
Rob Holt (rjmholt) merged 25 commits into
PowerShell:masterfrom
JamesWTruher:NativeArgPassing002
Jul 20, 2021
Merged

Add a Windows mode for native commands that allows some commands to use legacy argument passing#15408
Rob Holt (rjmholt) merged 25 commits into
PowerShell:masterfrom
JamesWTruher:NativeArgPassing002

Conversation

@JamesWTruher

@JamesWTruher James Truher (JamesWTruher) commented May 14, 2021

Copy link
Copy Markdown
Collaborator

PR Summary

This PR targets a consistent default behavior between PowerShell 5 and PowerShell 7 on Windows for some executables and file types. The behavior is now update to use the legacy behavior for the following files:

  • cmd.exe
  • cscript.exe
  • wscript.exe
  • ending with .bat
  • ending with .cmd
  • ending with .vbs

This list is table driven, and can be easily altered. I have started with this list as I am familiar with real world issues with them.

The PSNativeArgumentPassing preference value has been expanded to include the new value Windows. When the preference variable is set to Windows the above table will be in effect, meaning that invocations of those files will automatically use the Legacy style argument passing. If the PSNativeArgumentPassing is set to either Legacy or Standard, then the additional checks will not take place.

The default behavior is now also platform specific. On Windows platforms, the default setting will be Windows and non-Windows platforms will be Standard.

PR Context

PR Checklist

@ghost ghost assigned Ilya (iSazonov) May 14, 2021
@JamesWTruher
James Truher (JamesWTruher) force-pushed the NativeArgPassing002 branch 2 times, most recently from 2fe31da to 8ed04ee Compare May 17, 2021 23:01
@JamesWTruher James Truher (JamesWTruher) changed the title WIP: Native argument passing adjustments for Windows systems Native argument passing adjustments for Windows systems May 20, 2021
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels May 20, 2021
@SteveL-MSFT

Copy link
Copy Markdown
Member

I believe this PR addresses part of #15143

@JamesWTruher

Copy link
Copy Markdown
Collaborator Author

Steve Lee (@SteveL-MSFT) - I think I've addressed your issues, please let me know

@SteveL-MSFT

Copy link
Copy Markdown
Member

James Truher (@JamesWTruher) one comment was not addressed

Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to allocate. We can do if (filePath.EndsWith(exception, StringComparison.OrdinalIgnoreVase)) in line 1302.

Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
@rkeithhill

Keith Hill (rkeithhill) commented Jun 3, 2021

Copy link
Copy Markdown
Collaborator

I'm not sure this is working completely for cmd.exe - ie excluding cmd.exe. preview.6 breaks the Invoke-BatchFile command we have implemented in PSCX:

PS> invoke-batchfile 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat' amd64
'\"C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\vcvarsall.bat\"' is not recognized as an internal or external command,
operable program or batch file.

This is basically Lee Holmes old function that we have implemented in PSCX:

function Invoke-BatchFile
{
    param([string]$Path, [string]$Parameters)

    $tempFile = [IO.Path]::GetTempFileName()

    ## Store the output of cmd.exe.  We also ask cmd.exe to output
    ## the environment table after the batch file completes
    cmd.exe /c " `"$Path`" $Parameters && set " > $tempFile
    ...
}

Reverting to Legacy makes this function work again but at this rate I suspect I'll be running Legacy on all my Windows installs. :-(

@ghost

Copy link
Copy Markdown

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

@JamesWTruher

Copy link
Copy Markdown
Collaborator Author

Keith Hill (@rkeithhill) wrt the pscx issue that you mentioned. Have you tried running this branch against your scenario? This PR is supposed to automatically put the arg passing into legacy mode for cmd.exe invocations, so it should be fine (but I would really like confirmation on that). I've added validation for that very scenario, but I might have missed something.

@rkeithhill

Copy link
Copy Markdown
Collaborator

I built this branch and yes, that command works now while I still have the mode set to Windows:

image

@JamesWTruher

Copy link
Copy Markdown
Collaborator Author

Keith Hill (@rkeithhill) - excellent - the default setting for windows will be "Windows", which is the hybrid model where certain commands will run as Legacy while others will use "Standard". This should hopefully be better and allow the wacky parsing that happens for batch files, but enable more modern apps to use the new style.

@SteveL-MSFT Steve Lee (SteveL-MSFT) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please take a look at Rob's suggestion

Comment thread src/System.Management.Automation/engine/CommandBase.cs Outdated
@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Needed The PR is being reviewed labels Jun 15, 2021
@SteveL-MSFT

Copy link
Copy Markdown
Member

Reassigning to Rob Holt (@rjmholt) as Dongbo Wang (@daxian-dbw) is taking time off right now

@rjmholt
Rob Holt (rjmholt) self-requested a review July 20, 2021 16:14
Comment thread src/System.Management.Automation/engine/NativeCommandParameterBinder.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
Comment thread src/System.Management.Automation/engine/NativeCommandProcessor.cs Outdated
@rjmholt

Copy link
Copy Markdown
Collaborator

Opened JamesWTruher#15 to add remaining suggested changes

@rjmholt

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 6 pipeline(s).

@rjmholt
Rob Holt (rjmholt) enabled auto-merge (squash) July 20, 2021 22:29
@rjmholt Rob Holt (rjmholt) changed the title Native argument passing adjustments for Windows systems Add a Windows mode for native commands that allows some commands to use legacy argument passing Jul 20, 2021
@rjmholt
Rob Holt (rjmholt) enabled auto-merge (squash) July 20, 2021 22:30
@rjmholt
Rob Holt (rjmholt) merged commit ac762c1 into PowerShell:master Jul 20, 2021
@adityapatwardhan Aditya Patwardhan (adityapatwardhan) added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Jul 21, 2021
@ghost

Copy link
Copy Markdown

🎉v7.2.0-preview.8 has been released which incorporates this pull request.:tada:

Handy links:

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

Labels

CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Committee-Reviewed PS-Committee has reviewed this and made a decision

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants