c# - Powershell 命令不会在 C# 中运行

标签 c# powershell exchange-server-2010

我正在编写一个 c# Windows 窗体应用程序,以将 Exchange 2010 邮箱以 .pst 格式迁移到服务器上的文件位置。我使用 Powershell SDK (Runspace05) 中的一个示例来访问交换 cmdlet (Get-Mailbox) 并使用用户邮箱填充组合框,没有问题。

我遇到问题的部分是运行 New-MailboxExportRequest cmdlet(执行导出的 cmdlet)以及返回它的对象并在列表框控件中显示它们的能力。我错过了什么?预先感谢您的帮助。

代码:

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

            }
        }

最佳答案

您想访问 PSObject 的属性,而不是对象本身。

试试这个:

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

关于c# - Powershell 命令不会在 C# 中运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18472318/

相关文章:

c# - 在编辑时处理 dataGridView FormatExeption

xml - Powershell:从函数正确返回XML

c# - New-MailContact 的等效 API

c# - 使用 ExchangeService 从 Exchange Server 2010 SP1 获取全局地址列表 (GAL)

c# - Exchange Web 服务托管 API : How can I perform Recurrence Expansion through the FindItem Method?

c# - 如何从通过 API 返回的 Task<IActionResult> 获取值进行单元测试

c# - MVVM light application - 如何正确清理 ViewModels

c# - 在 ASP.NET 回发期间如何安全地保留密码字段的值?

javascript - 从 NodeJS 调用 PowerShell

powershell - 如何使用 Invoke-WebRequest 的 -OutFile 参数转义文件路径中的方括号