This blog has moved to http://ThePowerShellGuy.com
while playing with the MY object in a former post,
I noticed that the MSH will exit if called from MSH.
I posted this on the newsgroup, and got the following aswer :Sorry - this is a known problem. Execution threads in the monad engine are
created MTA and you can't create an object that requires STA from an MTA
thread. We have workaround code in the new-object cmdlet that let's you
create an STA COM object when you create the object explicitly. Unfortunately
this doesn't work for objects that are created implicitly by a .NET object.
We instantiate the .NET object and some time later it instantiates the COM
object. We can't use the same workaround because we don't get the exception
until the first time the object is used instead of when it's created. Anyway
- we're still working on this and will hopefully have a solution soon.
This posting is provided "AS IS" with no warranties, and confers no rights.
I have found the following workaround for it :
PS, it's more a POC (proof-of-Concept), it will only write the line "Hello Clip" (hard-coded),
You need to make it a function that takes arguments, or better yet a CMDlet. to make it realy usefull ;-)
$provider = new-object Microsoft.VisualBasic.VBCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters
$params.GenerateInMemory = $True
$refs = "System.dll","Microsoft.VisualBasic.dll","System.Data.DLL","System.management.dll","System.DirectoryServices.dll"
# VB.NET EXAMPLE
$txtCode = @'
Public Class mow
Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
newThread.ApartmentState = ApartmentState.STA
Shared Sub ThreadMethod()
dim Comp as new Microsoft.VisualBasic.Devices.computer
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$mAssembly = $results.CompiledAssembly
$i = $mAssembly.CreateInstance("mow")
$r = $i.main()