【问题标题】:Getting error "CmdletInvocationException was unhandled" when Exporting Exchange Mailboxes with C#使用 C# 导出 Exchange 邮箱时出现错误“CmdletInvocationException 未处理”
【发布时间】:2012-10-10 22:50:25
【问题描述】:

我收到 CmdletInvocationException 未处理,无法调用此函数,因为当前主机没有实现它。

当我将 Export-Mailbox cmdlet 替换为 get-command 作为测试时,我没有收到任何错误。

这是我的示例代码。当我运行 sScript1 时,它运行得很好,当我运行 sScript2 时,我得到了错误。当我在我的测试机器上执行 get-ex 命令时,我找不到 export-mailbox cmdlet。

    public void RunPowerShell()
    {
        Cursor.Current = Cursors.WaitCursor;
        RunspaceConfiguration rsConfig = RunspaceConfiguration.Create();
        PSSnapInException snapInException = null;
        Runspace runSpace;

        //create the runspace
        runSpace = RunspaceFactory.CreateRunspace(rsConfig);
        runSpace.Open();
        rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapInException);
        Pipeline pipeLine = runSpace.CreatePipeline();
        //Add-MailboxPermission -Identity " + FromEmailAccount.Text + " -User Administrator -AccessRights FullAccess


        String sScript1 = "Add-MailboxPermission -Identity " + ToEmailAccount.Text + " -User Administrator -AccessRights FullAccess";
        String sScript2 = "Export-Mailbox -Identity " + FromEmailAccount.Text + " -TargetMailbox " + ToEmailAccount.Text + " -TargetFolder " + ToEmailAccount.Text + " -Confirm:$false";

        //pipeLine.Commands.AddScript(sScript1);
        pipeLine.Commands.AddScript(sScript2);

        Collection<PSObject> commandResults = pipeLine.Invoke();

        //loop through the results of the command and load the SamAccountName into the list
        foreach (PSObject results in commandResults)
        {
            //MessageBox.Show(results.ToString(), @"test");
        }
        pipeLine.Dispose();
        runSpace.Close();
        MessageBox.Show(@"complete", @"Done");
        Cursor.Current = Cursors.Default;
    }

【问题讨论】:

    标签: c# powershell exchange-server


    【解决方案1】:

    我发现了这个in another post,它解决了我的错误。

    如果该命令通常会提示确认,那么您需要:

    1) Set -Confirm:$false as a parameter (and possibly -Force as well)
    2) Set $ConfirmPreference = "None" before calling Set-Mailbox (and possibly -Force too)
    3) Create a Host and implement the Confirm functionality ;-)
    

    最初由 Jaykul 发布/回答

    【讨论】:

      猜你喜欢
      • 2010-10-01
      • 2019-02-25
      • 1970-01-01
      • 2022-10-25
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多