C#.Net如何使用Powershell + Exchange Snapin并使其保持开放状态

时间:2013-11-27 15:52:05

标签: c# powershell

我的新代码如下,但它表示get-mailbox无法识别,因为snapin从未添加过。

    Runspace runSpace;
    public void button1_Click(object sender, EventArgs e)
    {
        Pipeline pipeline = runSpace.CreatePipeline();
        pipeline.Commands.Add("get-mailbox cbilgic");
            pipeline.Invoke();         
    }

    private void button2_Click(object sender, EventArgs e)
    {
        RunspaceConfiguration runspaceConfig = RunspaceConfiguration.Create();
        PSSnapInException snapEx = null;
            PSSnapInInfo psinfo = runspaceConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out snapEx);
        runSpace = RunspaceFactory.CreateRunspace(runspaceConfig);
        runSpace.Open();
    }

1 个答案:

答案 0 :(得分:1)

首先,使用Runspaces,它会让事情变得更加轻松。用RunspaceConfiguration加载PSSnapin并使用pipeline.invoke()输出命令。加载管理单元并在表单加载时实例化运行空间,然后在用户单击按钮时调用命令。

当我尝试类似的任务时,阅读this文章帮助了我很多。