0

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

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

这是我的示例代码。当我运行 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;
    }
4

1 回答 1

0

在另一篇文章中发现了这一点,这对我的错误有用。

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

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

于 2012-06-22T15:14:35.973 回答