You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PowerShell v6.0.0_Alpha.13 ignoring the "#requires -version" statement #2846
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime
#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
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.
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.
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!
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 -Version5.0#Requires -MaxVersion 5.9
Which of course would stop it running on PSv6 completely.
Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-AnsweredThe question is answered.WG-Enginecore PowerShell engine, interpreter, and runtime
Activity
kilasuit commentedon Dec 7, 2016
#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 14971Forget the above - I misread the Screenshot and read it as requires v5 not v6 as it actually states
MaximoTrinidad commentedon Dec 7, 2016
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!!
iSazonov commentedon Dec 7, 2016
I think that PS 6.0 will only be released when it is fully compatible with previous versions. Now it is only
alfaSteveL-MSFT commentedon Dec 8, 2016
As noted,
#requires -versionmeans 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.
MaximoTrinidad commentedon Dec 8, 2016
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!
iSazonov commentedon Dec 8, 2016
Maximo Trinidad (@MaximoTrinidad) Feel free to report all incompatibilities you found. It will be a great contribution.
MaximoTrinidad commentedon Dec 8, 2016
Sounds good! I will work on it and colllect what I find.
Thanks,
Maximo
kilasuit commentedon Dec 8, 2016
To try and clear this up
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.0as v6.0.0.13 is a higher version to v5. If you had specified#Requires -Version 3.0then 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
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
Which of course would stop it running on PSv6 completely.
I hope that has helped :-)
rjmholt commentedon Mar 23, 2018
I've opened an RFC on updating the behaviour of
#requires. Please feel free to comment in the PR.SteveL-MSFT commentedon Mar 23, 2018
Closing this as the behavior is expected. Please refer to Rob Holt (@rjmholt)'s RFC to discuss new behavior.