# Monday, February 26, 2007

It looks like the issue I've been having with calling AzMan via PowerShell via MMC have nothing to do with PowerShell, and is probably an AzMan issue.  I tried exactly the same scenario, but accessed a different path with my drive provider that ultimately makes a COM call to ADAM, rather than AzMan, and it worked just fine.  That makes me think it's something about the AzMan implementation. 

However, I still need to call my PowerShell drive provider from MMC, and that problem has been resolved, (many) thanks to Bruce Payette

Turns out that there's a way to get PowerShell to run in the caller's thread, rather than creating its own MTA thread.  This means that my PowerShell code will get called from MMC's STA thread directly, and the AzMan interfaces get created just fine.  Woohoo!

Previously, I was creating the Runspace and calling it thusly...

RunspaceConfiguration config = RunspaceConfiguration.Create();
PSSnapInException warning;
//load my drive provider
PSSnapInInfo info = config.AddPSSnapIn(snapInName, out warning); 
Runspace rs = RunspaceFactory.CreateRunspace(config);
create Command object with right parameters
Collection<PSObject> results;
using (Pipeline pipeline = _runspace.CreatePipeline())
    results = pipeline.Invoke();

This wasn't working, I believe because MMC is calling this code on an STA thread, and PowerShell is creating its own MTA thread, which doesn't seem to be acceptible to AzMan (not sure why).

However, according to Bruce's suggestion, I changed it to look like this...

Runspace rs = RunspaceFactory.CreateRunspace();
PSSnapInException warning;
//load my drive provider
rs.RunspaceConfiguration.AddPSSnapIn(snapinName, out warning);
Runspace.DefaultRunspace = rs;
EngineIntrinsics _exec = rs.SessionStateProxy.GetVariable("ExecutionContext") as EngineIntrinsics;
ScriptBlock sb = _exec.InvokeCommand.
    NewScriptBlock("my script block");
Collection<PSObject> results = sb.Invoke();

This causes the PowerShell call to happen on the caller's (MMC's) thread, and everyone is happy. 

Thursday, March 22, 2007 9:28:08 PM (Pacific Standard Time, UTC-08:00)
I've seen that example from Payette in a few different places. What I'd like to have, however, is a way to run the Powershell console itself on an STA thread. This would be fantastic for WatiN (as <a href="http://www.hanselman.com/blog/WATIRForNETWatiNApproaches08ReleaseAndAutomatingIEFromPowerShell.aspx">Scott has previously noted</a>).

I wonder if it would be possible to write a new STA-threaded console using Payette's technique to wrap around a Powershell runspace... But then, would it be worth it when one could just as easily use Watir and pipe its output to a Powershell variable?
Thursday, March 22, 2007 9:28:57 PM (Pacific Standard Time, UTC-08:00)
D'oh! <a> isn't apparently supported HTML. :)
Comments are closed.