簡體   English   中英

使用C#導出Exchange郵箱時出現錯誤“未處理CmdletInvocationException”

[英]Getting error “CmdletInvocationException was unhandled” when Exporting Exchange Mailboxes with C#

我收到未處理的CmdletInvocationException,無法調用此函數,因為當前主機未實現它。

當我用get命令替換Export-Mailbox cmdlet作為測試時,我沒有收到任何錯誤。

這是我的示例代碼。 當我運行sScript1時,它運行得很好,當我運行sScript2時,即出現錯誤。 當我在我的測試機上執行get-excommand時,我找不到導出郵箱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;
    }

在另一篇文章中找到了這個,它可以解決我的錯誤。

如果該命令通常會提示您進行確認,那么您將需要:

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發布/回答

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM