0

I am receiving CmdletInvocationException was unhandled, Cannot invoke this function because the current host does not implement it.

when i replace the Export-Mailbox cmdlet with a get-command as a test I get no errors.

Here is my sample code. When i run sScript1, its runs just fine, when i run sScript2 thats when i get the error. When im on my testing machine i did the get-excommand and I couldnt find the 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;
    }

1 Answers1

0

I found this in another post, which worked for my error.

If that command would normally prompt for confirmation then you will need to either:

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

originally posted/answered by Jaykul

Community
  • 1
  • 1