locked
Powershell script running with ISE but not Powershell RRS feed

  • Question

  • Hi,

    I have my first script and found it is working on many computers with ISE with no problem but not directly with PS.

    There are no error message. Nothing

    I just have:

    PS C:\temp\_!NTrousses1.0_Frv1> .\NTrousses1.0_Frv1.ps1
    Windows PowerShell
    Copyright (C) 2009 Microsoft Corporation. Tous droits réservés.

    Is it possible the following lines wound be the problems:

    if ([threading.thread]::CurrentThread.GetApartmentState() -eq "MTA") {
     & $env:SystemRoot\system32\WindowsPowerShell\v1.0\powershell.exe -sta $MyInvocation.ScriptName
     Exit
    }

    If so, are they any workaround?

    Thanks,

    François

    Friday, November 28, 2014 3:33 PM

Answers

  • Hi,

    Why was it working correctly with ISE but not Powershell?

    When the script was wrote it was not working correctly with the script editor without the first line.

    Thanks,

    François

    Did you try what I suggested?

    I don't know why it was written that way.  You will have to ask the author.  If it wsa written fo rPowershell V2 or earlier then it forces you into STA mode.  PowerShell V3 and later default to STA mode.  The ISE defaults to STA.

    If you are not using V2 then the lines are unnecessary.


    ¯\_(ツ)_/¯

    • Marked as answer by FRacine Sunday, November 30, 2014 3:13 AM
    Friday, November 28, 2014 8:44 PM

All replies

  • Hi,

    Could you share the whole script? That will help us figure out where you might have an issue.

    Friday, November 28, 2014 3:59 PM
  • Hi,

    This is a big script... I am loading a form.

    Thanks,

    Friday, November 28, 2014 7:20 PM
  • THe default threading model for PowerShell so the first lines are odd.  It must be an old script.

    Comment out those lines and runit again.

    If it is forms based then yo may need to run as MTA.  Some controls require it and some require STA.

    Comment out the first lines and run it iek this:

    powershell -sta -file .\NTrousses1.0_Frv1.ps1


    ¯\_(ツ)_/¯

    • Proposed as answer by egoktay Saturday, November 29, 2014 3:02 PM
    Friday, November 28, 2014 7:36 PM
  • Hi,

    Why was it working correctly with ISE but not Powershell?

    When the script was wrote it was not working correctly with the script editor without the first line.

    Thanks,

    François

    Friday, November 28, 2014 8:20 PM
  • Hi,

    Why was it working correctly with ISE but not Powershell?

    When the script was wrote it was not working correctly with the script editor without the first line.

    Thanks,

    François

    Did you try what I suggested?

    I don't know why it was written that way.  You will have to ask the author.  If it wsa written fo rPowershell V2 or earlier then it forces you into STA mode.  PowerShell V3 and later default to STA mode.  The ISE defaults to STA.

    If you are not using V2 then the lines are unnecessary.


    ¯\_(ツ)_/¯

    • Marked as answer by FRacine Sunday, November 30, 2014 3:13 AM
    Friday, November 28, 2014 8:44 PM
  • hi,

    thanks

    Saturday, November 29, 2014 3:03 PM