Getting PowerShell version issue

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

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 6 years and 3 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.
Locked
User avatar
SkiFreak
Posts: 13
Last visit: Thu Jun 27, 2019 7:50 am

Re: Getting PowerShell version issue

Post by SkiFreak »

jvierra wrote: Fri Dec 01, 2017 6:46 pm #requires -version 3

This will pop a message that says that the version is not available.

All of this is one of the many reasons you should not be running or have V2 installed anywhere.
It would be fine if this happens, but it doesn't.
On a standard Win7 workstation, If I do not build the application to use the v2 script engine I get a Windows system message that the executable has stopped working and another dialog asking if I want to send more information to Microsoft. I am trying to avoid the possibility of a user getting this message, as it is an "application has crashed" message.
Basically, I want to trap any such error and present a more user friendly dialog, saying that this application cannot be run on that computer.

With the executable built using the v2 script engine it runs just fine, other than reporting the wrong PS version. I get no message that the minimum PS version is v3, despite the first line of the code being #requires -version 3
As I said, if that happened it would be acceptable, but it doesn't do this.

I do understand what you are saying about PSS setting the PS version as v2 (as per your previous post), but the thing that really confuses me is that on the workstation where PSS is installed (a Win7 VM with PowerShell v5 installed), if I run the test version check executable on that machine it reports the correct version of PowerShell. If I run the same executable on a virtually identical VM it reports v2.0 as the PowerShell version.
I cannot see any logic to this, and it's why I am so confused.

In an ideal world I would not have mixed PS versions in the network, but this is the real world and I do.
Only the workstations that are configured to do remote user management will have the PS version upgraded to v5.x
What I need to deal with, gracefully, is a user copying this application to another workstation and trying to run it.

So... as I mentioned earlier, I need a reliable way to check the PS version and to be able to deal with the application being run on a workstation with the wrong PS version.
Is there any way to do that?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting PowerShell version issue

Post by jvierra »

As I noted above. You should not be using V2 anymore. Also, if V2 is damaged or has installation issues then there isn't much you can do in script. Be sure you have all Net 2.0 patches installed and that the Win7 system is working correctly.

You could use a batch file to launch the exe and test the PS version in the batch file.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Getting PowerShell version issue

Post by jvierra »

Let me try to clarify the issue you are facing. YOU want to build with a PS 5 Windows host and run it on a system with only PS V2 installed. YOu also want the PS 5 exe to run and ell you that the code won't run on a V2 system.

Ho can you expect a V5 host EXE to run on a system that does not have V5 installed. Of course you get a system error. You are trying to run a program that requires components that are not installed. The program cannot even load.

To better understand why this is you will need to look up and study how a Windows process is created and run. Your error is coming before the program is even run because Windows cannot even load the program. You cannot trap or detect errors in a program that crashes during the load phase because the issue happens before any code is executed.

You could build an installer for the EXE that checks the version and installs a separate version for V2 and V5.

(This is often called the "chicken and egg" problem)
User avatar
SkiFreak
Posts: 13
Last visit: Thu Jun 27, 2019 7:50 am

Re: Getting PowerShell version issue

Post by SkiFreak »

Firstly, I should acknowledge my appreciation for the help given. It's appreciated. Thanks!

I now realise that the exported PS1 script and an executable created by PSS can perform with significant differences.
That's okay, if you know this, but it is frustrating if you don't.

I think I have a better grasp on this now, even though I cannot do exactly what I want to do, I have found ways to work around my problems.
Thanks again...
This topic is 6 years and 3 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.
Locked