将C#变量传递给Powershell引擎
我正在用C编写一个WinForms应用程序,它最终将Exchange 2010邮箱迁移到.pst格式的文件位置(pstStore)。表单由一组文本框、组合框和单选按钮组成。单击按钮后,将执行此工作的命令是New-MailboxExportRequest–Mailbox…-FilePath 我正在访问Exchange命令行管理程序,并使用运行空间传递cmdlet和参数。在参数(-Mailbox and–FilePath)中,我想传递文本框和组合框的值。我如何在C#中实现这一点 仅供参考…我正在使用相同的代码填充包含exchange数据库中所有邮箱的组合框。代码就是为了这个目的而工作的,所以,我想我也可以用它通过AddParameter方法将一些变量传递到 以下是单击事件的代码:将C#变量传递给Powershell引擎,c#,powershell,C#,Powershell,我正在用C编写一个WinForms应用程序,它最终将Exchange 2010邮箱迁移到.pst格式的文件位置(pstStore)。表单由一组文本框、组合框和单选按钮组成。单击按钮后,将执行此工作的命令是New-MailboxExportRequest–Mailbox…-FilePath 我正在访问Exchange命令行管理程序,并使用运行空间传递cmdlet和参数。在参数(-Mailbox and–FilePath)中,我想传递文本框和组合框的值。我如何在C#中实现这一点 仅供参考…我正在使用
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();
}
InitialSessionState iss=InitialSessionState.CreateDefault();
异常预警;iss.ImportPSSnapIn(“Microsoft.Exchange.Management.PowerShell.E2010”,out警告);
使用(Runspace myrunspace=RunspaceFactory.CreateRunspace(iss))
{
myrunspace.Open();
使用(PowerShell PowerShell=PowerShell.Create())
{powershell.AddCommand(“Microsoft.Exchange.Management.powershell.E2010\\New MailboxExportRequest”)
powershell.AddParameter(“邮箱”、“用户邮箱”);
AddParameter(“FilePath”、“ExchAdmin”);
powershell.AddParameter(“,”);
powershell.Runspace=myrunspace;
收集结果=空;
尝试
{
results=powershell.Invoke();//同步运行cmdlet
}
捕获(RuntimeException ex)
{
foreach(PSObject thisResult in results)
{
lstBoxStatus.Items.Add(thisResult);//将结果发送到状态窗口
}
}
myrunspace.Close();
}
调用包含两个参数的AddParameter重载时,第二个参数是值。只需使用其中的C变量名称,例如:
string mailbox = _mailBoxTextBox.Text;
...
powershell.AddParameter("Mailbox", mailbox);
当调用带有两个参数的AddParameter重载时,第二个参数是值。只需使用C变量的名称即可,例如:
string mailbox = _mailBoxTextBox.Text;
...
powershell.AddParameter("Mailbox", mailbox);
现在这似乎起到了作用。我把变量放在了错误的范围内。一旦我确定了参数被排除在外。还有更多的工作要做,所以它不会太笨重,但是,感谢清楚。现在这似乎起到了作用。我把变量放在了错误的范围内。一旦我确定了参数被排除在外。还有更多的工作要做这样做不会太笨重,但是,谢谢你的清晰。