Issues running in STA

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 10 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
akrofly
Posts: 30
Last visit: Fri Mar 24, 2023 3:33 am

Issues running in STA

Post by akrofly »

Hello,

Using Powershell Studio to consolidate certain SQL functions I ran into an issue with the folder browse button function which worked perfectly until it hung when run on servers with a PowerShell v2 environment.
Pressing the folder browse button simply caused the entire application to hang.(The file browse button worked fine though)

The issue of the "browse folder button" is solved by staring the PowerShell in STA and then running the PS1 script.

When compiled into an EXE, I made sure to choose v2 and STA, the folder browse button worked perfectly but the application failed on the following:
Add-PSSnapin SqlServerCmdletSnapin100
and
Add-PSSnapin SqlServerProviderSnapin100

All seems to work fine if the GUI is started using the script instead of the EXE by starting "powershell -sta" first.

Any fix for this behavior?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues running in STA

Post by jvierra »

You can try Import-Module SQLPS. It will work in both MTA and STA sessions.
User avatar
akrofly
Posts: 30
Last visit: Fri Mar 24, 2023 3:33 am

Re: Issues running in STA

Post by akrofly »

Thanks for that tip, but that will not work in this case.
I forgot to mention, the script checks the SQL version, if the SQL cluster is running a 2012 or newer, SQLPS is imported, but if the SQL is an older version then the pssnapins are added.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues running in STA

Post by jvierra »

The imported module should work with all current versions of SQLServer.
User avatar
akrofly
Posts: 30
Last visit: Fri Mar 24, 2023 3:33 am

Re: Issues running in STA

Post by akrofly »

SQLPS as an imported module is only supported in SQL 2012 or newer.
In 2008R2 versions it cannot be imported as a module, it is only a host, hence the need to add the pssnapins for certain operations.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues running in STA

Post by jvierra »

No - I have been using it on SQS2008 for years. You have to upgrade the SSMS. It works just fine.

SQLPS works in PS 2 but the SQLASCMDLETS module will not work without PS4.

You will have many issues with the snap-in and PS2. I recommend upgrading PowerShell to the latest version and then all things will work.

If you load the snap-in then you will have to use MTA.

One way around the issue is to create your own folder browser and use stock PowerShell CmdLets to retrieve folders.
User avatar
akrofly
Posts: 30
Last visit: Fri Mar 24, 2023 3:33 am

Re: Issues running in STA

Post by akrofly »

Upgrading the PowerShell on 100+ servers is not an option at this point in time since they are being migrated and phased out.
For those servers, the SQLPS cannot be imported as a module and the need for the snapins arises.
So I conclude from our discussion that either an upgrade or to continue using it as a PS1 run in "Powershell -STA".

The issue with the browse folder has been solved in STA mode.
The snapins work in STA mode as well.
The problem with the snapins only surfaces when compiled to an EXE.

Thank you for your attempts.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues running in STA

Post by jvierra »

be sure you are setting STA in the build settings an choosing the correct host.
User avatar
akrofly
Posts: 30
Last visit: Fri Mar 24, 2023 3:33 am

Re: Issues running in STA

Post by akrofly »

I thought I did, attached a snapshot of the settings.
Attachments
STA.jpg
STA.jpg (39.58 KiB) Viewed 3262 times
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Issues running in STA

Post by jvierra »

Wrong settings. For forms you must use the Windows Forms host as well as STA.
This topic is 6 years and 10 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