Question

Goal: Provide a web service using Visual Basic or C# or .NET that interacts with the Exchange Management Shell, sending it commands to run cmdlets, and return the results as XML. (Note that we could use any lanaguage to write the service, but since it is a Windows Box and we have Visual Studio 2008, it seemed like easiest solution would be just use it to create a VB/.NET web service. Indeed, it was quite easy to do so, just point and click.)

Problem: How to run an Exchange Management Shell cmdlet from the web service, e.g, Get-DistributionGroupMember "Live Presidents"

Seems that we should be able to create a PowerShell script that runs the cmdlet, and be able to call that from the command line, and thus just call it from within the program. Does this sound correct? If so how would I go about this? Thanks. Answer can be language agnostic, but Visual Basic would probably be best since that is what I loaded the test web service up in.

Was it helpful?

Solution 2

Well, didn't get an answer, but sort of figured it out. I had a problem getting a 64-bit PowerShell to run, but eventually upgraded to Exchange 2010 and used C# and then there was no longer a problem.

The short answer is that you create a new PowerShell application in Visual Studio, then you add a reference to the System.Management.Automation dll. This allows you to set up a namespace for Powershell and make calls to it. http://msdn.microsoft.com/en-us/library/system.management.automation(VS.85).aspx You create a Pipeline using the available Pipeline class http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces.pipeline(VS.85).aspx in order to pipe your commands back. Then you put your commands in, add parameters if needed. Run the app and it will return the results from the cmdlets you called in the PowerShell and you can go from there.

OTHER TIPS

The actual code adapted from from MSDN http://msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx can be tricky because you have to get the permissions right and run it on a macine with all of the Exchange plug-ins:

using Microsoft.Win32;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation.Runspaces;
using System.Reflection;

    public static Runspace GetExchangeRunspace()
    {
        return GetExchangeRunspace("");
    }
    public static Runspace GetExchangeRunspace(string snapIn)
    {
        string consoleFilePath = (ScriptEngine.GetExchangeAssemblyPath() 
            + "bin\\exshell.psc1").Replace("Exchange Server", "EXCHAN~1");
        Response.Write("<br/>" + consoleFilePath);
        PSConsoleLoadException warnings = null;
        RunspaceConfiguration runspaceConfiguration 
            = RunspaceConfiguration.Create(consoleFilePath, out warnings);
        if ((snapIn + "").Trim().Length > 0)
        {
            PSSnapInException warning = null;
            Response.Write("<br/>Start AddPSSnapIn..." + snapIn);
            Response.Write("<br/>" 
                + runspaceConfiguration.AddPSSnapIn(snapIn, out warning));
            Response.Write("<br/>" + warning);
        }
        return RunspaceFactory.CreateRunspace(runspaceConfiguration);
    }

    private static string GetExchangeAssemblyPath()
    {
        string path = "";
        try
        {
            RegistryKey key = Registry.LocalMachine.OpenSubKey(
                "SOFTWARE\\Microsoft\\ExchangeServer\\v14\\Setup"); // or your version
            if (key != null)
            {
                path = Path.GetFullPath(string.Concat(key.GetValue("MsiInstallPath")));
                Response.Write(path);
            }
        }
        catch (Exception ex) { }
        return path;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top