none
How to deal with the error "Cannot invoke this function because the current host does not implement it" when excute EMS by C# code RRS feed

  • Question

  • Hi,

    I try to excute EMS "Restore-Mailbox -Identity test1 -RecoveryDatabase RDB8" in C# programme.

    If I manually input the commad "Restore-Mailbox -Identity test1 -RecoveryDatabase RDB8" EMS window. It can be excuted successfully.

    But I call it in my C# programme , I encounter the error  "Cannot invoke this function because the current host does not implement it"

    bellow is my code,

     Runspace EMSRunSpace = CreatEMSRunSpace();
                PowerShell psh = PowerShell.Create();
                psh.Runspace = EMSRunSpace;
                EMSRunSpace.Open();
                string RecoverMailCommand="Restore-Mailbox";
                psh.AddCommand(RecoverMailCommand);
                psh.AddParameter("Identity", MailBox);
                psh.AddParameter("RecoveryDatabase", string.Format("{0}{1}", RDBNAME, RdbID));
                Collection<PSObject> results = psh.Invoke();

    I can run other EMS command by C# using the same way.

    How to deal with it ?

    Wednesday, June 6, 2012 3:42 AM

Answers

  • Did you try using Confirm ? that should work

    Parameters.Add("Confirm", false);

    If you want to test this at the shell use -Confirm:$false

    Cheers
    Glen

    • Marked as answer by perfect strom Wednesday, June 6, 2012 9:39 AM
    Wednesday, June 6, 2012 9:08 AM

All replies

  • Generally you would see error when your trying to use a cmdlet that requires some form of confirmation at the console or you haven't provided enough parameters for the cmdlet to run successfully. I would suggest you test this first from a normal Exchange Management Shell session and see if you are providing enough parameters (or if there are other isssue) or see if its asking for confirmation (some cmdlets do some don't). If you need to provide confirmation you can do something like this

                                Command apAddmbPerms = new Command("Add-RecipientPermission");
                                apAddmbPerms.Parameters.Add("Identity", Mailbox);
                                apAddmbPerms.Parameters.Add("Trustee", String);
                                apAddmbPerms.Parameters.Add("AccessRights", "SendAs");
                                apAddmbPerms.Parameters.Add("Confirm", false);
                                plPileLine = runspace.CreatePipeline();
                                plPileLine.Commands.Add(apAddmbPerms);
    Cheers
    Glen
    Wednesday, June 6, 2012 6:35 AM
  • It is seems that the command "Restore-Mailbox" is interacting command.

    When I run  command "Restore-Mailbox" in ESM window manually, there will be the information as bellow:

    Confirm
    Are you sure you want to perform this action?
    Recovering mailbox content from mailbox 'test1' in the recovery database 'RDB8' to the mailbox for 'test1
    (test1@seg.com)'. This operation may take a long time to complete.
    [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [?] Help (default is "Y"):

    then I input "Y", restore run. 

    If I call the command "Restore-Mailbox" in C# code, I can't input the "Y" to system.

    So ,Is there some way to  run the interacting EMS in C# code ?

     
    Wednesday, June 6, 2012 8:17 AM
  • Did you try using Confirm ? that should work

    Parameters.Add("Confirm", false);

    If you want to test this at the shell use -Confirm:$false

    Cheers
    Glen

    • Marked as answer by perfect strom Wednesday, June 6, 2012 9:39 AM
    Wednesday, June 6, 2012 9:08 AM
  • It works, Thank you
    Wednesday, June 6, 2012 9:40 AM