Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

There is no Runspace available to run scripts in this thread.

1,298 views
Skip to first unread message

andysvar

unread,
Jan 3, 2009, 3:37:21 PM1/3/09
to

Hello PS gurus,

I have problems with a very simple code snippet. I just create a new
RunSpace, a new PipeLine with a simple "ls" command which I invoke. The
pipeline correctly returns a set of PSObjects representing files and
directories and each of them has a SciptProperty called Mode (returning
a string such as -a--- known from the command shell). However trying to
get this value I get the exception:

SetValueInvocationException

Exception setting "Mode": "There is no Runspace available to run
scripts in this thread. You can provide one in the DefaultRunspace
property of the System.Management.Automation.Runspaces.Runspace type.
The script block you attempted to invoke was: $catr = "";
... $catr"

I suppose that the script property is being evaluated in another thread
(possibly some "pipeline execution thread"). Setting DefaultRunspace
does not work (presumably because I can't set it on the correct thread).
Any idea, please???

My code snippet:


Code:
--------------------
static void Main(string[] args)
{
Runspace runspace = RunspaceFactory.CreateRunspace();

// Does not work (MshTLSDataSlot is set on this thread)
Runspace.DefaultRunspace = RunspaceFactory.CreateRunspace();

var pipeline = runspace.CreatePipeline("ls");

runspace.Open();

var ret = pipeline.Invoke();

// The exception pops out here.
Console.WriteLine(ret[0].Properties["Mode"].Value);

}
--------------------


--
andysvar

Keith Hill [MVP]

unread,
Jan 3, 2009, 5:27:40 PM1/3/09
to
I would suggest that you either A) query the Mode (extended) property within
the PowerShell script you invoke in the runspace or B) forego the extended
properties and use the FileInfo's Attributes property.

--
Keith

"andysvar" <gu...@unknown-email.com> wrote in message
news:e97e4fcee08784ce...@nntp-gateway.com...

Markus2

unread,
Jan 13, 2009, 10:56:03 AM1/13/09
to

Hi,

I get the same error and don't know how to fix it.
I don't use Vista (XP SP3 and PS V2 CTP), but the thread fits exactly
my needs.
The purpose is to

I tested the following:


...


Code:
--------------------
protected override void BeginProcessing()
{
this.m_Runspace = RunspaceFactory.CreateRunspace();
}


protected override void ProcessRecord()
{
//Start a new thread in a multithreaded apartment

Thread t = new Thread( new ThreadStart(DoWork ) );
t.SetApartmentState( ApartmentState.MTA );
t.Start();
}


public void DoWork()
{
//Open the runspace
this.m_Runspace.Open();


//Invoke the Scriptblock

this.m_myScriptBlock.Invoke( this.m_Array );


//Close the Runspace
this.m_Runspace.Close();
}

--------------------


...


But that doesn't work. The PowerShell quits with the Error.

Can somebody give me a hint?


> I would suggest that you either A) query the Mode (extended) property
> within
> the PowerShell script you invoke in the runspace or B) forego the
> extended
> properties and use the FileInfo's Attributes property.


I don't really understand this hint


--
Markus2

0 new messages