Skip to content

PowerShell v6.0.0_Alpha.13 ignoring the "#requires -version" statement #2846

Closed

Description

Steps to reproduce

Create a script file and include the " #requires -version 5" line before the code.
Open PowerShell v6.0.0_Alpha-13
run .scriptfile.ps1

Expected behavior

Script file will error-out with the message:
"... cannot be run because it contained a "#requires" statement for Windows PowerShell 5.0..."

Actual behavior

It continues to execute the script file.

Environment data

OS: Windows 10 Pro Insider Preview 14971

PS C:\Program Files\PowerShell\6.0.0.13> $PSVersionTable

Name                           Value
----                           -----
CLRVersion
PSVersion                      6.0.0-alpha
SerializationVersion           1.1.0.1
BuildVersion                   3.0.0.0
GitCommitId                    v6.0.0-alpha.13
WSManStackVersion              3.0
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSEdition                      Core
PSRemotingProtocolVersion      2.3

psreqversionissue

Activity

  1. kilasuit commented on Dec 7, 2016

    @kilasuit
    Collaborator

    #Requires -version has always meant that this is the minimum required version of powershell to run - so this is working as I would expect in powershell v6.0.0.13 & 5.1

    However I'm confused as the behavior in the Windows PowerShell window in your screenshot goes against this completely although I can't replicate the behaviour and by the looks of it I'm on the same Windows 10 build 14971
    Forget the above - I misread the Screenshot and read it as requires v5 not v6 as it actually states

  2. MaximoTrinidad commented on Dec 7, 2016

    @MaximoTrinidad
    Author

    My only purpose was to demonstrate a "possible" way to validate is the PowerShell script can run in PowerShell Open Source. Now that we'll be creating "cross-platform" scripts, there must be a way to Windows PowerShell scripts in a PowerShell Open Source environment.

    In the above image, in PowerShell v6.0.0.0-Alpha_13, I'm trying to execute an SMO Script that can only run in Windows PowerShell v5.x (so it has the "#requires -version 5.0". The results is, PowerShell 6 ignores the '#requires -version' statement and try to run the SMO script.

    In the other console, is Windows PowerShell but I just change the "#requires -version" from 5 to 6. This was just the prevent the script to run. In reality the SMO script will not run in PowerShell 6.

    I think it should be important that PowerShell Open Source could use the "#requires " statement.

    Any comments are welcome!!

  3. iSazonov commented on Dec 7, 2016

    @iSazonov
    Collaborator

    I think that PS 6.0 will only be released when it is fully compatible with previous versions. Now it is only alfa

  4. added
    WG-Engine
    on Dec 7, 2016
  5. SteveL-MSFT commented on Dec 8, 2016

    @SteveL-MSFT
    Member

    As noted, #requires -version means minimal version, not exact version.

    PSCore 6.0 engine is expected to be compatible, however, the error in the repro above is due to differences between FullCLR and CoreCLR. At this point, PowerShell Core 6.0 is only planned to support CoreCLR which is our cross platform story. .Net Standard 2.0 in the future should remove some of these compatibility issues between FullCLR and CoreCLR.

  6. MaximoTrinidad commented on Dec 8, 2016

    @MaximoTrinidad
    Author

    Ilya (@iSazonov) , I know is Alpha but, as of now, we can see that even the Alpha version has differences between the OS's. I don't see PowerShell 6.0 been compatible with previous (Windows) version moving forward cross-platform. A lot would need to change on Windows side and too much has been invested.
    But, that's my thought!

  7. iSazonov commented on Dec 8, 2016

    @iSazonov
    Collaborator

    Maximo Trinidad (@MaximoTrinidad) Feel free to report all incompatibilities you found. It will be a great contribution.

  8. MaximoTrinidad commented on Dec 8, 2016

    @MaximoTrinidad
    Author

    Sounds good! I will work on it and colllect what I find.

    Thanks,
    Maximo

  9. kilasuit commented on Dec 8, 2016

    @kilasuit
    Collaborator

    To try and clear this up

    In the above image, in PowerShell v6.0.0.0-Alpha_13, I'm trying to execute an SMO Script that can only run in Windows PowerShell v5.x (so it has the "#requires -version 5.0". The results is, PowerShell 6 ignores the '#requires -version' statement and try to run the SMO script.

    Firstly PSv6 doesn't "ignore" the requires statement at all due to the fact that it's not specifying that it will only run in the version specified but all versions beyond that one. So any and all scripts that have #Requires -version will work in versions of PowerShell beyond the version specified - therefore PowerShell v6.0.0.13 does respect this when you specify #Requires -Version 5.0 as v6.0.0.13 is a higher version to v5. If you had specified #Requires -Version 3.0 then that would means that the script will be able to run in PowerShell v3, v4, v5, v5.1 & v6 but it will not run in v1 or v2. To confirm this please check the about_requires help text.

    If you have a script that you don't think will run in PowerShell v6 then I would suggest to adapt the below and place it at the top of the script so that it runs first and place the rest of the scripts logic within the else statement

    if ($iscoreclr) 
    {Write-Warning 'This script will only run on the FullCLR version of PowerShell which is only currently available on Windows, Please rerun this script in the Full Version of PowerShell.'; break}
    else
    {'Full'}

    This added to a script will check for the iscoreclr variable and if its true (on all current versions of PSv6 regardless of the OS it runs on including Windows) will stop the script from processing and provide the user a sensible warning message.

    I do recall a discussion (however I cannot find it now) where it was put forward that it may be benefit in extending the requires statement to include a max version so that you could include something like this as your requires statement

    #Requires -Version 5.0
    #Requires -MaxVersion 5.9

    Which of course would stop it running on PSv6 completely.

    I hope that has helped :-)

  10. added
    Issue-Discussion
    on Dec 9, 2016
  11. rjmholt commented on Mar 23, 2018

    @rjmholt
    Collaborator

    I've opened an RFC on updating the behaviour of #requires. Please feel free to comment in the PR.

  12. SteveL-MSFT commented on Mar 23, 2018

    @SteveL-MSFT
    Member

    Closing this as the behavior is expected. Please refer to Rob Holt (@rjmholt)'s RFC to discuss new behavior.

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

    Issue-DiscussionResolution-AnsweredWG-Engine

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions