Skip to content

PowerShell needs a way to hide commands which won't work #1998

Closed

Description

@Jaykul

For example: Set-ExecutionPolicy. This command should simply not exist on Linux. Rather than throwing RuntimeErrorException terminating errors, I would prefer to get a command-not found, because that's something I can test for ahead of time.

Years of developing for moving targets on the web has taught us that the right way to avoid features that don't exist is to test for the feature, and not for the underlying OS, Architecture, or software version, because you never know when someone's going to finally figure out how to implement something.

If non-functioning commands are not hidden, there is no easy, fast way for scripts to test for whether commands will work -- you have to run them and catch the exception. However, many scripters will instead write incorrect conditional code based on the PSEdition (or operating system sniffing, but currently that's not even in $PSVersionTable #1997) which will prevent them from using the feature in the case where it's added later on.

In general, when a (core) module exposes a command (functioning or not), it's more difficult for another module to provide an implementation -- I know we can't anyway in this particular example, but that's the problem with picking examples ;-)

Suggestion: This should be exposed in the manifest.

I would like an easy way to identify commands that should be exported based on PSEdition, PSVersion, OS, or Architecture from the module manifest.

I don't want to have to wrap binary modules in script modules to put conditional Export-ModuleManifest statements in ... especially since putting the right values in the manifest for exported cmdlets is vital to discoverability.

One thought is to allow specific module manifests for platforms, like MyModule.Core-Linux.psd1 or something -- this would provide solutions to other design problems as well (like loading the right binary assemblies).

Activity

  1. lzybkr commented on Aug 21, 2016

    @lzybkr
    Contributor

    The plan is to allow the variables $IsLinux and $IsWindows so you can use something like:

    CmdletsToExport = "Get-Process",$(if ($IsWindows) { "Set-ExecutionPolicy" })
  2. kilasuit commented on Aug 22, 2016

    @kilasuit
    Collaborator

    Although I agree this needs to be in the Module Manifest I think that we need to have this information in the Cmdlet (or Function) as an additional property that we can define perhaps like so

    [CmdletBinding(SupportedOsVersions='Windows','Linux','MacOS')]

    (not sure would need MacOS but best to put it in there)

    I believe that would be a breaking change but would allow for CI systems to dynamically build the *ToExport lists like Jason Shirk (@lzybkr) has suggested for in the Manifest

  3. Jaykul commented on Aug 23, 2016

    @Jaykul
    ContributorAuthor

    It would be convenient to be able to do it with an attribute, but unless putting it in the CmdletBinding attribute automatically hides it, it should not go there at all.

    I'm ok with the $IsWindows and $IsLinux but can we also access $IsCore or $IsARM ?

  4. melabadi commented on Aug 23, 2016

    @melabadi

    Something could be done using the "Get-CimInstance" cmd, if it works on Linux, of course.
    GitHub noob contributor here.

  5. masaeedu commented on Aug 25, 2016

    @masaeedu

    Jason Shirk (@lzybkr) Would this help us with intellisense when e.g. developing scripts for Linux from an instance of VSCode running on Windows?

  6. added
    WG-Engine
    on Sep 29, 2016
  7. added this to the milestone on Sep 29, 2016
  8. modified the milestones: , on Dec 19, 2016
  9. modified the milestones: , 6.0.0-beta1 on Mar 18, 2017
  10. modified the milestones: , on May 15, 2017
  11. 7 remaining items

  12. removed this from the milestone on Aug 31, 2017
  13. SteveL-MSFT commented on Aug 31, 2017

    @SteveL-MSFT
    Member

    We need to spend more time thinking through the solution (perhaps a RFC), moving to 6.1.0

  14. mi-hol commented on Feb 17, 2018

    @mi-hol

    Steve Lee (@SteveL-MSFT) did the thinking bear fruit that could be shared here?

  15. SteveL-MSFT commented on Feb 21, 2018

    @SteveL-MSFT
    Member

    mi-hol no investigation for this has happened yet

  16. bcdady commented on Dec 13, 2018

    @bcdady

    I am running PowerShell Core 6.1.1 on MacOS, and encountered these symptoms again. I found this issue via #7573.
    I run Get-ExecutionPolicy in $PROFILE Script, which works fine, but then I instinctively want to go run Set-ExecutionPolicy, which doesn't work.

    I could add more specific issues to running Set-ExecutionPolicy in PS Core on MacOS, but for this Issue, I think this is sufficient.

  17. added
    Review - Committee
    on Jan 9, 2019
  18. SteveL-MSFT commented on Jan 9, 2019

    @SteveL-MSFT
    Member

    Module manifest already supports conditional exporting https://docs.microsoft.com/en-us/powershell/gallery/concepts/module-psedition-support

    As for the ExecutionPolicy cmdlets themselves, I don't recall exactly why we left them since we explicitly removed others. Let's have Committee review.

  19. SteveL-MSFT commented on Jan 9, 2019

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this, since we want to retain Get-ExecutionPolicy for compatibility reasons (and should return Bypass which is more accurate on non-Windows). It would make sense to keep Set-ExecutionPolicy and only allow setting Unrestricted or Bypass, other values should continue to return the Operation not supported on this platform error. pwsh -executionpolicy should also follow this.

  20. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Jan 9, 2019
  21. iSazonov commented on Sep 29, 2019

    @iSazonov
    Collaborator

    It seems we hide/remove all that don't work on Unix-s. Can we close the issue?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Committee-ReviewedResolution-FixedWG-Engine

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions