Is Powershell -sta (apartment state) preferred? -
i've been dabbling in powershell (2.0) past several months , love use modernize , standardize of processes @ work - dos based processes. because of nature of work, there around 100 executions of same script(s) going @ once.
first--is powershell "safe" use in manner? have seen -sta execution option - preferred method of working powershell when executing number of same script simultaneously, or used when absolutely necessary? in searching, haven't come answer "when should use apartment state?" believe if not of scripting intend not threaded.
thanks in advance can shed light on powershell apartment state!
in psv2, console host runs mta, , ise runs sta. in psv3 console defaults sta.
you can see apartment state this:
[system.threading.thread]::currentthread.getapartmentstate()
the time need use sta when using classes in .net deal com objects use e.g. system.windows.forms.clipboard
.
there 2 ways know of change sta:
powershell.exe -sta -file myscript.ps1
or
$ps = [powershell]::create() $rs = [runspacefactory]::createrunspace() $rs.apartmentstate = "sta" $rs.threadoptions = "reusethread" $rs.open() $ps.runspace = $rs $ps.addscript( { ([system.threading.thread]::currentthread.getapartmentstate()) } ).invoke()
so question why psv2 console mta instead of sta? when want know why ps team made decisions refer bruce payette's powershell in action book. unfortunately didn't why. said there com objects require sta , if script doesn't work try re-running sta.
Comments
Post a Comment