PowerShell命令不会在C#中运行 [英] Powershell Command Will not run in C#
问题描述
我写一个C#Windows窗体应用程序到Exchange 2010邮箱迁移到文件位置的.pst格式在服务器上。我使用PowerShell的SDK(Runspace05)为例来访问Exchange的cmdlet(获取邮箱)和填充组合框与没有问题的用户邮箱。
我在与获得新-MailboxExportRequest cmdlet来运行(执行出口该cmdlet),并返回它的对象,并展示它们的能力麻烦的部分在ListBox控件。我在想什么? 。在此先感谢您的帮助。
代码:
InitialSessionState ISS = InitialSessionState.CreateDefault();
PSSnapInException警告;
iss.ImportPSSnapIn(Microsoft.Exchange.Management.PowerShell.E2010,出警告);
使用(运行空间myrunspace = RunspaceFactory.CreateRunspace(ISS))
{
myrunspace.Open();使用
(PowerShell的PowerShell的= PowerShell.Create())
{
VAR邮箱= cmbEmailUserName.Text;
VAR pstFile = txtFileSaveLocation.Text;
const int的badLimit = 100; //可以在必要的
powershell.AddCommand(Microsoft.Exchange.Management.PowerShell.E2010\\New-MailboxExportRequest)增加;
powershell.AddParameter(邮箱,邮箱);
powershell.AddParameter(文件路径,pstFile);
powershell.Runspace = myrunspace;
收集和LT; PSObject>结果= powershell.Invoke();
的foreach(PSObject thisResult在结果)
{
lstBoxStatus.Items.Add(thisResult);
}
myrunspace.Close();
}
}
您要访问的 PSObject
,而不是对象本身的属性。
试试这个:
的foreach(PSObject thisResult在结果)
{
的foreach(PSPropertyInfo thisResultInfo在thisResult.Properties)
{
lstBoxStatus.Items.Add(名称:{0}值:{1}类型:{2},thisResultInfo.Name,thisResultInfo.Value,thisResultInfo.MemberType);
}
}
I am writing a c# Windows Form app to migrate Exchange 2010 mailboxes to a file location on the server in .pst format. I used an example from the Powershell SDK (Runspace05) to access the exchange cmdlets (Get-Mailbox) and populate a combo-box with the users mailboxes with no problem.
The parts i'm having trouble with is getting the New-MailboxExportRequest cmdlet (the cmdlet that performs the export) to run and the ability to return it's objects and show them in a listbox control. What am I missing? Thanks in advance for your help.
The Code:
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())
{
var mailbox = cmbEmailUserName.Text;
var pstFile = txtFileSaveLocation.Text;
const int badLimit = 100; //can be increased in necessary
powershell.AddCommand("Microsoft.Exchange.Management.PowerShell.E2010\\New-MailboxExportRequest");
powershell.AddParameter("Mailbox", mailbox);
powershell.AddParameter("FilePath", pstFile);
powershell.Runspace = myrunspace;
Collection<PSObject> results = powershell.Invoke();
foreach (PSObject thisResult in results)
{
lstBoxStatus.Items.Add(thisResult);
}
myrunspace.Close();
}
}
You want to access the properties of the PSObject
, not the object itself.
Try this:
foreach (PSObject thisResult in results)
{
foreach (PSPropertyInfo thisResultInfo in thisResult.Properties)
{
lstBoxStatus.Items.Add("Name: {0} Value: {1} Type: {2}", thisResultInfo.Name, thisResultInfo.Value, thisResultInfo.MemberType);
}
}
这篇关于PowerShell命令不会在C#中运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!