从 Visual Basic/C#/.NET 应用程序运行 Exchange Management Shell cmdlet

发布于 2024-08-29 14:16:54 字数 469 浏览 8 评论 0原文

目标:使用 Visual Basic、C# 或 .NET 提供与 Exchange 命令行管理程序交互的 Web 服务,向其发送命令以运行 cmdlet,并以 XML 形式返回结果。 (请注意,我们可以使用任何语言来编写服务,但由于它是 Windows Box 并且我们有 Visual Studio 2008,因此似乎最简单的解决方案就是使用它来创建 VB/.NET Web 服务。事实上,它这样做非常容易,只需点击即可。)

问题:如何从 Web 服务运行 Exchange Management Shell cmdlet,例如 Get-DistributionGroupMember“Live Presidents”

似乎我们应该能够创建一个运行的 PowerShell 脚本cmdlet,并且能够从命令行调用它,因此只需从程序内调用它。这听起来正确吗?如果是这样我该怎么办?谢谢。答案可能与语言无关,但 Visual Basic 可能是最好的,因为我就是在其中加载测试 Web 服务的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ヤ经典坏疍 2024-09-05 14:16:54

实际代码改编自MSDN http:// /msdn.microsoft.com/en-us/library/exchange/bb332449(v=exchg.80).aspx 可能很棘手,因为您必须获得正确的权限并在具有所有权限的计算机上运行它交换插件:

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;
    }

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;
    }
爱你是孤单的心事 2024-09-05 14:16:54

好吧,虽然没有得到答案,但大概明白了。我在运行 64 位 PowerShell 时遇到问题,但最终升级到 Exchange 2010 并使用 C#,然后就不再有问题了。

简而言之,您在 Visual Studio 中创建一个新的 PowerShell 应用程序,然后添加对 System.Management.Automation dll 的引用。这允许您为 Powershell 设置命名空间并对其进行调用。 http://msdn.microsoft.com /en-us/library/system.management.automation(VS.85).aspx 使用可用的 Pipeline 类创建 Pipeline http://msdn.microsoft.com/en-us/library/system.management.automation.runspaces .pipeline(VS.85).aspx 以便通过管道传回您的命令。然后输入命令,如果需要的话添加参数。运行该应用程序,它将返回您在 PowerShell 中调用的 cmdlet 的结果,您可以从那里开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文