我有一个 Exchange Powershell 脚本,可以将电子邮件从一个帐户移动到另一个帐户。当您通过 Powershell 调用代码时,它会为您提供实时数据统计信息。我怎样才能捕捉到它并将其输入到将提供“实时提要”的文本框中。提前感谢您的任何帮助。
public void RunPowerShell()
{
Cursor.Current = Cursors.WaitCursor;
RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
PSSnapInException snapInException = null;
Runspace runSpace;
//create the runspace
runSpace = RunspaceFactory.CreateRunspace(rsConfig);
//insert try here
runSpace.Open();
//for exchange 2010 use "Microsoft.Exchange.Management.PowerShell.E2010"
rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
//set up the pipeline to run the powershell command
Pipeline pipeLine = runSpace.CreatePipeline();
//create the scripts to use
String sScript = "Add-MailboxPermission -Identity " + FromEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
String sScript1 = "Add-MailboxPermission -Identity " + ToEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
String sScript2 = "Export-Mailbox -Identity " + FromEmailAccount.Text + " -TargetMailbox " + ToEmailAccount.Text + " -TargetFolder " + FromEmailAccount.Text + " -Confirm:$false";
//invoke the scripts
pipeLine.Commands.AddScript(sScript);
pipeLine.Commands.AddScript(sScript1);
pipeLine.Commands.AddScript(sScript2);
Collection<PSObject> commandResults = pipeLine.Invoke();
//loop through the results of the command and load the ?
foreach (PSObject results in commandResults)
{
richTextBox1.Text = results.ToString();
//MessageBox.Show(results.Properties["DisplayName"].Value.ToString(),@"Name");
}
//close the pipelin and runspace
pipeLine.Dispose();
runSpace.Close();
//create completed message
MessageBox.Show(@"Email migration is complete", @"Done");
Cursor.Current = Cursors.Default;
}