Programatic execution of New-MailboxSearch fails but success by using EMS RRS feed

  • Question

  • I write a program to execute Exchange Cmdlet, it succeed for almost every command except multi-mailbox search command, like "New-MailboxSearch". But I can run this Cmdlet by using EMS. From Windows Task Manager, I can see my program is running under "Administrator" who is a member of "Discovery Management Group".

    Here is my code, after execute "New-MailboxSearch", I caught following exception:"Unable to execute the task. Reason: The task can't identify the user that is executing the task." 

        RunspaceConfiguration runspaceCfg = RunspaceConfiguration.Create();
        PSSnapInException mshSnapInException = null;
        RegistryKey hklm = Registry.LocalMachine;
        RegistryKey hkE14 = hklm.OpenSubKey("SOFTWARE\\Microsoft\\ExchangeServer\\v14");
        runspaceCfg.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", out mshSnapInException);
        Runspace m_runspace = RunspaceFactory.CreateRunspace(runspaceCfg);
        RunspaceInvoke m_runspaceInvoke = new RunspaceInvoke(m_runspace);
        string strCommand = "New-MailboxSearch -Name test + TargetMailbox DiscoveryMailbox";
    catch (Exception e)

    I found a post in MSDN, he has almost the same problem, but I've tried the solution, it doesn't work.

    Wednesday, July 25, 2012 10:35 AM


  • As mentioned in your identical post from Wednesday, August 01, 2012 2:31 AM: (Please don't keep posting the same question)

    For one thing, in the small example provided, you are invoking strCmdlet - however, you are defining strCommand

    Additionally, you have not shown one of the working samples - I'd bet they are subtly different in the method you are using to create the runspace....


    When you see answers and helpful posts, please click Vote As Helpful, Propose As Answer, and/or Mark As Answer

    My Blog:
    My Book: Windows PowerShell 2.0 Bible
    My E-mail: -join ("6B61726C6D69747363686B65406D742E6E6574"-split"(?<=\G.{2})",19|%{[char][int]"0x$_"})

    • Marked as answer by Carly Gui Wednesday, December 12, 2012 8:53 AM
    Monday, August 6, 2012 5:48 PM