【发布时间】: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