加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Powershell命令无法在C#中运行

发布时间:2020-12-15 21:55:41 所属栏目:百科 来源:网络整理
导读:我正在编写一个c# Windows窗体应用程序,以便以.pst格式将Exchange 2010邮箱迁移到服务器上的文件位置.我使用Powershell SDK(Runspace05)中的示例来访问Exchange cmdlet(Get-Mailbox)并使用用户邮箱填充组合框,没有任何问题. 我遇到问题的部分是运行New-Mailb
我正在编写一个c# Windows窗体应用程序,以便以.pst格式将Exchange 2010邮箱迁移到服务器上的文件位置.我使用Powershell SDK(Runspace05)中的示例来访问Exchange 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.E2010New-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);
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读