首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用读取C#中Cmdlet的详细输出

如何使用读取C#中Cmdlet的详细输出
EN

Stack Overflow用户
提问于 2010-12-20 01:53:52
回答 2查看 2.6K关注 0票数 1

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

2008,.Net 3.5

我正在使用Exchange移动邮箱cmdlet,并注意到当我从(使用详细的开关)这样做时,提供了大量的实时信息。

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

这是我目前的大致情况,但我不确定是否有需要登记的活动或什么.另外,我希望实时获取这些信息,这样我就可以更新UI,以反映相应用户在移动序列中发生了什么(就像管理控制台中提供的本机功能一样)。如果您想知道,我不满足于管理控制台功能的原因是,我有一个算法,用于根据存储限制、黑莓使用、日志记录、异常邮箱大小等来平衡用户,该算法要求将用户映射到特定的位置.我也不希望为每个公共目的地创建许多/几个移动组,也不希望通过管理控制台UI单独搜索用户列表。

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

我已经研究过Invoke和InvokeAsync方法以及StateChanged & DataReady事件,但这些方法似乎都没有提供我所追求的信息(冗长的注释)。

任何方向或例子,可以提供将非常感谢!

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

代码语言:javascript
运行
复制
// 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();
}
EN

回答 2

Stack Overflow用户

发布于 2010-12-20 21:33:56

看看PowerShell类。它有一个Streams属性,允许您访问详细流的内容。请注意,PowerShell类是在PowerShell 2.0中引入的,但是您可能希望在宿主应用程序中嵌入PowerShell时使用这个类。事实上,医生们说:

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

票数 2
EN

Stack Overflow用户

发布于 2010-12-21 15:51:07

我在网站上开了个账号这样我就可以互动了..。我就是问这个问题的那个人。

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

现在我理解了如何在streams属性中使用PS类,我希望能够添加-就像我以前所做的一样,我应该在路上了。

谢谢你为我指出正确的方向,这似乎正是我所需要的!

你好,基思

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4486331

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档