1
我正在C#中编写一个WinForms应用程序,最终将以.pst格式将Exchange 2010邮箱迁移到文件位置(pstStore)。该表格由一组文本框,组合框和单选按钮组成。完成这项工作的命令是New-MailboxExportRequest -Mailbox ... -FilePath ...按钮单击后。将C#变量传递给Powershell引擎
我正在访问Exchange管理外壳并使用运行空间传递该cmdlet和参数。在参数(-Mailbox和-FilePath)中,我想传递文本框和组合框的值。我如何在C#中执行此操作?
仅供参考...我使用相同的代码来填充交换数据库中所有邮箱的组合框。因此,代码适用于此目的,所以我想我也可以使用它将一些变量传入到AddParameter方法中。
下面是从点击事件代码:
InitialSessionState iss = InitialSessionState.CreateDefault();
PSSnapInException warning; iss.ImportPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010",out warning);
using (Runspace myrunspace = RunspaceFactory.CreateRunspace(iss))
{
myrunspace.Open();
using (PowerShell powershell = PowerShell.Create())
{ powershell.AddCommand("Microsoft.Exchange.Management.PowerShell.E2010\\New-MailboxExportRequest")
powershell.AddParameter("Mailbox", "UserMailbox");
powershell.AddParameter("FilePath", "ExchAdmin");
powershell.AddParameter("","");
powershell.Runspace = myrunspace;
Collection<PSObject> results = null;
try
{
results = powershell.Invoke(); //Runs the cmdlet synchronously
}
catch (RuntimeException ex)
{
foreach (PSObject thisResult in results)
{
lstBoxStatus.Items.Add(thisResult); //sending the result to a status window
}
}
myrunspace.Close();
}
现在看起来似乎有诀窍。我有错误范围内的变量。一旦我确定参数被排除在外。还有更多的工作要做,所以它不是太笨重,但是,为了清晰起见。 – user2704769