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
Enhance Invoke-Command
with parameters that provide ad-hoc overrides of $PSNativeCommandArgumentPassing
and $PSNativeCommandUseErrorActionPreference
#18961
Comments
Can't you just set those variables in the scriptblock and it's going to be scoped like |
@jborean93, the fact that the script-block approach, demonstrated in the initial post, is too obscure and cumbersome was the motivation for creating this issue. Yes, I'm happy to open a new issue to supersede this one once we flesh this out a bit more: Let's assume a hypothetical I'm leaving the proposed parameters - which then wouldn't require the word # With PowerShell's own syntax, via a script block (-ScriptBlock implied)
# Note: Only useful if combined with parameters that modify the default behavior.
Invoke-Native { choice.exe /d y /t 0 /m '3" of snow?' }
# Passing a *no-shell* command line (-CommandLine implied), primarily useful on *Windows*
# Similar to Start-Process with a single -ArgumentList value, but including the executable.
# See also: https://github.com/PowerShell/PowerShell/issues/14347
Invoke-Native 'choice.exe /d y /t 0 /m "3\" of snow?"'
# Passing a *shell* command line (-CommandLine implied)
# Calls via `cmd /c` (or, better, via a *temporary batch file*) on Windows, and via `sh -c` on Unix.
Invoke-Native -UseShell 'ver && choice.exe /d y /t 0 /m "3\" of snow?" & echo ignore me 2>NUL' Note that I don't think that offering a way to provide the command arguments as a string array is then required - on Unix, you'll get proper verbatim-argument-value-as-parsed-by-PowerShell argument-passing by default; on Windows, you'll be able to request it ad hoc with something like At least hypothetically - there may be implementation challenges - a dedicated cmdlet would allow native-executable-specific behavior of common parameters such as Related issues: |
Yea my main point is what you are requesting is specific to native commands rather than a powershell scriptblock that
|
@jborean93, my previous comment was trying to flesh out the fundamentals of just that. Before we move on to the specifics of how the native invocations should be modified via specific parameters (all the things you mention are good candidates), do you have feedback on that? |
Instead of disabling error code handling by using That would not help with your choice.exe example, but a better solution there might be two new cmdlets... |
Better to have a new method to implement new functionality. We don't all have control over the version of powershell that is used. Our build system updates eventually to latest. And we get stuck with these breaking changes. Like the thing 7.3 did with parameters. There was 1 instance of an issue there that I know of. The native exit code thing will be a problem. Might ( I would argue should ) hinder adoption of 7.4. |
I'm closing this proposal, and will open a new one for a dedicated new cmdlet soon. @Andrew74L, note that a given CLI may have multiple nonzero exit codes that signal success ( @RichardMeadowsTC, yes, I agree that a new cmdlet is the right answer, after the discussion with @jborean93 above. Note that the breaking change in 7.3.0 should arguably never have happened and if a breaking change of this magnitude is deliberately made, it needs to announced long in advance, prominently - which unfortunately didn't happen. As I stated in #18944 (comment), I do NOT expect In preview versions, experimental features are ON by default, and this was combined with defaulting This to me is a symptom of a larger discoverability problem with respect to experimental features: As an aside, there's also a problem with how the state of experimental features is managed: |
I like that. I am a bit ignorant of the native argument issue. So was playing around with
Invoke-NativeCommand 'choice.exe /d y /t 0 /m "3\" of snow?"'
I notice there is mismatched quote. Missing a double quote.
Richard Meadows
From: Michael ***@***.***>
Sent: Thursday, January 19, 2023 8:36 PM
To: ***@***.***>
Cc: Richard ***@***.***>; ***@***.***>
Subject: Re: [PowerShell/PowerShell] Enhance `Invoke-Command` with parameters that provide ad-hoc overrides of `$PSNativeCommandArgumentPassing` and `$PSNativeCommandUseErrorActionPreference` (Issue #18961)
@jborean93<https://github.com/jborean93>, please see
* #18991<#18991>
—
Reply to this email directly, view it on GitHub<#18961 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AI5HOOBH73OYP4CW5WNBFN3WTII3LANCNFSM6AAAAAAT6KFI24>.
You are receiving this because you were mentioned.Message ID: ***@***.***>
|
@RichardMeadowsTC, no there's no missing
|
If Robocopy was 'primed' with an exitcode of 1 - |
@Andrew74L, in general, introduction of new preference variables should be avoided as much as possible - especially if not Magic variables that aren't a direct part of the command being executed make it harder to reason about what code does, and, generally speaking, PowerShell's dynamic scoping (where descendant scopes see variables too) makes such variables especially problematic. Having a dedicated cmdlet (see #18991) - which is easier to discover than a preference variable and doesn't introduce state - makes for a straightforward solution:
|
The proposed use and behavior is for $ZEROEXITCODE to be set by the user immediately prior to running a native command, which then automatically resets to zero immediately after. |
That's precisely what I think is ill-advised, as argued in my previous comment. I think we know our respective positions now, so I think it's fine to leave it at that. If you feel strongly about about your proposed solution, I encourage you to open a new proposal focused on just that. |
Summary of the new feature / enhancement
Note: While
Invoke-Command
already has an many parameter sets and parameters, it is still the conceptually best cmdlet to implement the proposed functionality in.Preference variables
$PSNativeCommandArgumentPassing
and$PSNativeCommandUseErrorActionPreference
both control the fundamental behavior of calls to native (external) programs.Changing the effective preference value for individual calls is cumbersome; e.g.:
Per-call
$PSNativeCommandUseErrorActionPreference
overrides are important for those nonstandard native programs that use nonzero exit codes to communicate success (variations), notablyrobocopy.exe
- see Robocopy Broken #18944Per-call
PSNativeCommandArgumentPassing
overrides are helpful selectively opting in and out of the corrected argument-passing behavior (Standard
) of v7.3+Invoke-Command
could ease the pain with parameters that encapsulate a per-call change to these preference variables.Proposed technical implementation details (optional)
Introduce two new parameters (which may be combined), which should be available in both local and remote invocations:
The text was updated successfully, but these errors were encountered: