简体   繁体   English

Powershell命令无法在C#中运行

[英]Powershell Command Will not run in C#

I am writing ac# Windows Form app to migrate Exchange 2010 mailboxes to a file location on the server in .pst format. 我正在编写ac #Windows Form应用程序,以便以.pst格式将Exchange 2010邮箱迁移到服务器上的文件位置。 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. 我使用Powershell SDK(Runspace05)中的示例来访问Exchange cmdlet(Get-Mailbox)并使用用户邮箱填充组合框,没有任何问题。

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. 我遇到问题的部分是运行New-MailboxExportRequest cmdlet(执行导出的cmdlet)以及返回它的对象并在列表框控件中显示它们的能力。 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. 您想要访问PSObject的属性,而不是对象本身。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM