Check PS version on startup

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
ISAssets
Posts: 10
Last visit: Wed Mar 27, 2024 6:25 pm

Check PS version on startup

Post by ISAssets »

I've created a multi form project which requires PowerShell 5 as it includes a class in Globals.ps1.

How can I check the PS version before the main form loads and show a MessageBox error if version is less than 5?

#Requires -Version 5 will produce a console error if I export the code and run in ISE, but I need an error in the GUI. This method causes the built EXE to immediately exit without any output if triggered.

I tried the following function at the top of Startup.pss.
if ($PSVersionTable.PSVersion.Major -lt 5)
{
	[void][System.Windows.Forms.MessageBox]::Show("This aplication requires PowerShell 5 or greater.", "Error", 'OK', 'error')
	break
}
Unfortunately this still fails to trigger as PS4 errors with The 'class' keyword is not supported in this version of the language..

Is there a way to get a GUI error for PowerShell version before the rest of the script loads?

Thanks for your help :)

Product: PowerShell Studio 2015
Version: 4.2.89
Operating System: Windows 10 64-bit
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check PS version on startup

Post by jvierra »

#requires -version 5

Place this in Globals.ps1 a the first statement.
User avatar
ISAssets
Posts: 10
Last visit: Wed Mar 27, 2024 6:25 pm

Re: Check PS version on startup

Post by ISAssets »

Hey jvierra.

I tried this and the error fails to trigger. When launching the EXE on a PC without PS4, the process starts and soon stops with no output.

I exported the code to clipboard from PSS then ran in ISE on the PC with PS4. The error still fails to trigger. I get the following output in the console.
At line:1638 char:2
+     class User {
+     ~~~~~
The 'class' keyword is not supported in this version of the language.
At line:1682 char:12
+         [boolean]UserGetADObject($UserID)
+                  ~~~~~~~~~~~~~~~
Unexpected token 'UserGetADObject' in expression or statement.
At line:1789 char:24
+         UserClearCurrentUser()
+                              ~
An expression was expected after '('.
At line:1795 char:12
+         [boolean]UserUnlockAccount()
+                  ~~~~~~~~~~~~~~~~~
Unexpected token 'UserUnlockAccount' in expression or statement.
At line:1795 char:30
+         [boolean]UserUnlockAccount()
+                                    ~
An expression was expected after '('.
At line:1810 char:12
+         [boolean]UserSetEnabledStatus($UserStatus)
+                  ~~~~~~~~~~~~~~~~~~~~
Unexpected token 'UserSetEnabledStatus' in expression or statement.
At line:1826 char:12
+         [boolean]UserSetPassword($NewPassword)
+                  ~~~~~~~~~~~~~~~
Unexpected token 'UserSetPassword' in expression or statement.
At line:1844 char:12
+         [boolean]UserForcePasswordChange()
+                  ~~~~~~~~~~~~~~~~~~~~~~~
Unexpected token 'UserForcePasswordChange' in expression or statement.
At line:1844 char:36
+         [boolean]UserForcePasswordChange()
+                                          ~
An expression was expected after '('.
At line:1860 char:12
+         [boolean]UserSetOtherPhone($NewOtherPhone)
+                  ~~~~~~~~~~~~~~~~~
Unexpected token 'UserSetOtherPhone' in expression or statement.
Not all parse errors were reported.  Correct the reported errors and try again.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ReservedKeywordNotAllowed
Appears the class type kills it before anything gets done. Is there another workaround besides running a separate start up script?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check PS version on startup

Post by jvierra »

Where did you place the line of code?

Did you build the EXE with PS 5 only?
User avatar
ISAssets
Posts: 10
Last visit: Wed Mar 27, 2024 6:25 pm

Re: Check PS version on startup

Post by ISAssets »

I placed the #Requires -Version 5 on line 1 of Global.ps1.

I built a new EXE. The application process exits in about 1 second without any visual output.

I exported the script to clipboard then pasted into PSISE on my Windows 7 box. When I attempt to run I get the error below.
At line:1634 char:2
+     class User {
+     ~~~~~
The 'class' keyword is not supported in this version of the language.
I tried adding #Requires -Version 5 to the top of the exported script but the class keyword error still appears.
Did you build the EXE with PS 5 only?
Home > Platform I had V5 - 64bit selected.
Deploy > Packager > Settings > Platform I have V5 Host (Windows Forms) x64 selected.

Tried Deploy > Packager > Settings > Platform I have V3 Host (Windows Forms) x64 but no dice :(

I'm currently working around the issue of a launcher EXE that checks for PS5 and if not found triggers error and stops. If found launches the app. Still this seems a bit messy.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check PS version on startup

Post by jvierra »

If you have a script with #requires -version 5 on line one and it does not reject the script then you have other issues.

The "class" keyword is only available in version 5.

I cannot reproduce your issues.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check PS version on startup

Post by jvierra »

A few more test seem to show that an exe ignores the "requires" statement so use the version table:

if($PSVersionTable.PSVersion.Major -lt 5){exit}
User avatar
ISAssets
Posts: 10
Last visit: Wed Mar 27, 2024 6:25 pm

Re: Check PS version on startup

Post by ISAssets »

Thank you for your support.

Please note it does process the #Requires -Version 5 line. When I run the script from ISE I can see the error in the console.

However when using in a Winform EXE, it doesn't output anything onscreen to advise the user why the "application" is failing to launch.

That is what I'm seeking assistance with. My apologies for not communicating this clearly so far.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Check PS version on startup

Post by jvierra »

Then your issue really belongs in product support and not here. I cannot speculate on why an exe would ignore this but teh developers at Sapien would know.
DevinL
Posts: 1098
Last visit: Tue Jun 06, 2017 9:15 am

Re: Check PS version on startup

Post by DevinL »

I've taken note of this and will come back with a reply when I have a chance to speak with the developers.
DevinL
SAPIEN Technologies, Inc.
This topic is 7 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.