【问题标题】:How can I read the verbose output from a Cmdlet in C# using Exchange Powershell如何使用 Exchange Powershell 从 C# 中的 Cmdlet 读取详细输出
【发布时间】:2011-05-28 01:04:56
【问题描述】:

环境:Exchange 2007 sp3(2003 sp2混合模式)

Visual Studio 2008、.Net 3.5


我正在使用 Exchange powershell move-mailbox cmdlet,并注意到当我从 Exchange Management shell(使用 Verbose 开关)执行此操作时,会提供大量实时信息。

为了提供一些上下文,我正在尝试创建一个 UI 应用程序,该应用程序类似于 Exchange 管理控制台移动邮箱,但希望支持每个条目(和线程)的输入文件和特定服务器/数据库目标。

这大概是我目前所拥有的,但我不确定是否有需要注册的活动或什么......并且明确地说,我希望实时获取这些信息,以便我可以更新我的 UI 以反映相应用户在移动序列中发生的情况(非常类似于管理控制台中提供的本机功能)。如果您想知道,我对管理控制台功能不满意的原因是,我有一个算法用于根据存储限制、黑莓使用、日记、异常邮箱大小等来平衡用户用户被映射到特定位置...并且我不希望为每个公共目的地创建许多/多个移动组或通过管理控制台 UI 单独寻找用户列表。

我似乎找不到任何好的文档或示例来说明如何使用 C# 来阅读控制台中提供的详细消息(我认为能够在许多不同场景中读取此类信息的价值)。

我探索了 Invoke 和 InvokeAsync 方法以及 StateChanged 和 DataReady 事件,但这些似乎都没有提供我所追求的信息(详细的 cmets)。

任何可以提供的方向或示例将不胜感激!

下面是一个代码示例,它与我通常调用任何其他 powershell 命令的方式差不多:

// config to use ExMgmt shell, create runspace and open it
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();

PSSnapInException snapInException = null;
PSSnapInInfo info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);

if (snapInException != null) throw snapInException;

Runspace runspace = RunspaceFactory.CreateRunspace(rsConfig);

try
{
  runspace.Open();

  // create a pipeline and feed script text
  Pipeline pipeline = runspace.CreatePipeline();

  string targetDatabase = @"myServer\myStorageGroup\myDB";
  string mbxOwner = "user@company.com";

  Command myMoveMailbox = new Command("Move-Mailbox", false, false);
  myMoveMailbox.Parameters.Add("Identity", mbxOwner);
  myMoveMailbox.Parameters.Add("TargetDatabase", targetDatabase);
  myMoveMailbox.Parameters.Add("Verbose");
  myMoveMailbox.Parameters.Add("ValidateOnly");
  myMoveMailbox.Parameters.Add("Confirm", false);
  pipeline.Commands.Add(myMoveMailbox);

  System.Collections.ObjectModel.Collection<PSObject> output = null;

  // these next few lines that are commented out are where I've tried
  // registering for events and calling asynchronously but this doesn't 
  // seem to get me anywhere closer
  //
  //pipeline.StateChanged += new EventHandler<PipelineStateEventArgs>(pipeline_StateChanged);
  //pipeline.Output.DataReady += new EventHandler(Output_DataReady);
  //pipeline.InvokeAsync();
  //pipeline.Input.Close();
  //return; tried these variations that are commented out but none seem to be useful

  output = pipeline.Invoke();

  // Check for errors in the pipeline and throw an exception if necessary
  if (pipeline.Error != null && pipeline.Error.Count > 0)
  {
     StringBuilder pipelineError = new StringBuilder();
     pipelineError.AppendFormat("Error calling Test() Cmdlet.  ");
     foreach (object item in pipeline.Error.ReadToEnd())
       pipelineError.AppendFormat("{0}\n", item.ToString());

     throw new Exception(pipelineError.ToString());
   }

   foreach (PSObject psObject in output)
   {
     // blah, blah, blah
     // this is normally where I would read details about a particular PS command
     // but really pertains to a command once it finishes and has nothing to do with
     // the verbose messages that I'm after... since this part of the methods pertains
     // to the after-effects of a command having run, I'm suspecting I need to look to
     // the asynch invoke method but am not certain or knowing how.
   }
}
finally
{
  runspace.Close();
}

【问题讨论】:

    标签: c# powershell exchange-server


    【解决方案1】:

    看看PowerShell 类。它有一个Streams 属性,允许您访问详细流的内容。请注意,PowerShell 类是在 PowerShell 2.0 中引入的,但可能是您在将 PowerShell 嵌入托管应用程序时想要使用的类。事实上,文档说:

    提供用于 创建命令管道和 调用这些命令 同步或异步内 运行空间。该类还提供 访问输出流 包含时生成的数据 命令被调用。 本课 主要用于主机 以编程方式使用的应用程序 用于执行任务的 Windows PowerShell。 此类在 Windows 中引入 PowerShell 2.0。

    【讨论】:

      【解决方案2】:

      我已经在网站上创建了一个帐户,这样我就可以进行互动了……我就是问这个问题的人。

      非常感谢您提供的信息和链接。正如您已经指出的那样,我注意到我必须升级到 PS 2.0 才能“看到”这个 Powershell 类(由于某种原因,这被证明有点麻烦)。

      现在我了解如何将 PS 类与流属性一起使用,我希望我能够添加 Exchange 管理管理单元,类似于我之前所做的那样,我应该正在路上。

      感谢您为我指明了正确的方向,这似乎正是我所需要的!

      问候, 基思

      【讨论】:

      • 我已经合并了你的账户,所以现在你可以接受 Keith Hill 的回答了。此外,这应该是对他的答案的评论,而不是单独的答案。
      猜你喜欢
      • 1970-01-01
      • 2018-12-12
      • 2017-12-24
      • 2017-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-20
      • 1970-01-01
      相关资源
      最近更新 更多