MVC Controller - Exchange Cmdlets RRS feed

  • Question

  • I have a ASP.NET MVC 3 Controller which spawns a runspace and uses impersonation to run the commands.

    The code below is what I'm working with.

    StringBuilder stringBuilder = new StringBuilder();
    PSSnapInException snapInException = null;            
    RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
    PSSnapInInfo info = runspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException); 
    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    using (new Impersonator("<username>", "<domain>", "<password>"))
    	PowerShell ps = PowerShell.Create();
            ps.Runspace = runspace;
            ps.AddCommand("Get-MailboxPermission").AddParameter("Identity", "<valid identity>");
            foreach (PSObject result in ps.Invoke())

    Note: The impersonation class uses the standard technique of calling a P/Invoke of LogonUser to get the security token etc.

    For troubleshooting I placed the above in a WinForms application and when it's executed it crashes the runtime out and I haven't been able to debug it. DebugDiag and the VS2010 debuggers have not found anything.

    If I change the command from from the Exchange Cmdlet to a built in one such as Get-Process the above works without issue, even if I fetch process information from another machine on the network.

    Any tips on how I can debug the above code or what the issue actually is that's causing it to exit?

    Thursday, February 2, 2012 3:46 AM