Getting error "CmdletInvocationException was unhandled" when Exporting Exchange Mailboxes with C#

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

Answers


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


Need Your Help

Code appended with JQuery does not fire click event on input file

javascript jquery html mouseclick-event

I am appending a small block of code that has inside of it an image that when clicked triggers a click event on the input for file uploading. When the page is loaded and/or refreshed, there is only...

MVC3 One View - Multiple PartialViews - One Model Per PartialView

asp.net-mvc asp.net-mvc-3 razor

I'm new to the whole MVC 3 style coding. I'm running into an issue, but first here's how I have my website laid out.