1

I wrote an extremely lengthy script that creates a form. The main purpose of it it to connect to vCenter servers, total up hosts/vms within different datacenters and also identify ESXi host versions. Running the script in ISE the form loads fine and when I click the Button to start the many various functions to update the fields, it runs. I have Foreach() fields throughout and added in a little StatusLabel to the bottom of the form to show me where in the script it is currently scanning.

Once all was done and working, I saved the PS1 file and moved it to a shared server to be run. When I run the Powershell console (as Admin) and point to the file, the Form loads ok. Once I click the button to begin generating the info, It just changes to (Not Responding) and grays out. I know it's working and if left there long enough it will return the info.

Why does the console window not function like the ISE? I want to be able to see the information and Status Label update/change as it goes through the script (just like I do in the ISE).

Appreciate the feedback. The script is several thousand lines long and would need to be scrubbed before I upload. Was hoping to just get some general info before doing that.

LTrig
  • 89
  • 1
  • 2
  • 9
  • My first guess would be a profile issue. Make sure that the account running it on the server has the same rights as the account that you run it in the ISE with. Also make sure that any required Snapins or Modules are being loaded for the account running it on the server. – EBGreen May 21 '15 at 15:47
  • To confirm your findings I would also close all open ISE sessions you have in the working environment and run it in ISE again. There could be session information that is persisting between runs that you might not be aware of such as a variable. – Matt May 21 '15 at 16:07
  • 1
    @LTrig Some of Windows.Forms functionality needs PowerShell to run in a Single Threaded Apartment (STA). By default Powershell_ISE runs in STA, but PowerShell command prompt will run in MTA. (this is true up to version 3, I think) To check your session for apartment state run `[System.Threading.Thread]::CurrentThread.GetApartmentState()` – Jan Chrbolka May 22 '15 at 05:51
  • 1
    @LTrig OK, the above is true in PowerShell v.2 and it was fixed in v.3. See [Is Powershell -sta (apartment state) preferred?](http://stackoverflow.com/questions/16072041/is-powershell-sta-apartment-state-preferred) – Jan Chrbolka May 22 '15 at 05:56
  • Thanks all for the info. Will do some checking and report back. Also working on upgrading from v2 to v4 on some of our servers. – LTrig May 22 '15 at 17:41

2 Answers2

3

I updated to v4 and verified that my command window was running as STA using the command from Jan Chrbolka. Script runs properly now.

LTrig
  • 89
  • 1
  • 2
  • 9
0

I was also facing the same issue.(Btw, I cannot upgrade Powershell to V4 right now since it is my company owned one.)

The Browse button that I incorporated in my form was not working when I am lauching it from Powershell command line. It went to Not Responding state when I click the browse button.

I took the advise from Jan ChrbolkaMay about the STA and MTA.

I checked for the threads related to that and changed the script execution format to the below one which fixed my issue.(adding the word -STA)

powershell -STA -File D:\PS\GUI.ps1

For checking the current state. Use the below query in Commandshell as well as ISE.

Cleb
  • 25,102
  • 20
  • 116
  • 151