תשובה Pipeline.Invoke error when calling sharepoint powershell scripts from WCF

  • יום חמישי 29 ספטמבר 2011 10:29
     
     

    Hi, 

    I'm trying to call sharepoint powershell scripts from WCF through this code:

     public string RunScript(string scriptText)

            {

                Collection<PSObject> results;

     

                Runspace runspace = RunspaceFactory.CreateRunspace();

                runspace.Open();

     

                Pipeline pipeline = runspace.CreatePipeline();

                pipeline.Commands.AddScript(scriptText);

                pipeline.Commands.Add("Out-String");

                results = pipeline.Invoke();

     

                runspace.Close();

     

                // convert the script result into a single string 

                StringBuilder stringBuilder = new StringBuilder();

                foreach (PSObject obj in results)

                {

                    stringBuilder.AppendLine(obj.ToString());

                }

        return stringBuilder.ToString(); 

    }

    Where the scriptText being passed has a value of:

    string script = "Add-PSSnapin Microsoft.SharePoint.PowerShell -EA 0; New-SPManagedPath TEST2 -HostHeader -Explicit";

     

    However, it yields an error at Pipeline.Invoke():

    System.Management.Automation.CommandNotFoundException was unhandled by user code

      Message=The term 'New-SPManagedPath' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

      Source=System.Management.Automation

      WasThrownFromThrowStatement=false

      CommandName=New-SPManagedPath

      StackTrace:

           at System.Management.Automation.CommandDiscovery.LookupCommandInfo(String commandName, CommandOrigin commandOrigin)

           at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)

           at System.Management.Automation.CommandFactory._CreateCommand(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)

           at System.Management.Automation.CommandFactory.CreateCommand(String commandName, CommandOrigin commandOrigin)

           at System.Management.Automation.ExecutionContext.CreateCommand(String command)

           at System.Management.Automation.CommandNode.CreateCommandProcessor(Int32& index, ExecutionContext context)

           at System.Management.Automation.CommandNode.AddToPipeline(PipelineProcessor pipeline, ExecutionContext context)

           at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

           at System.Management.Automation.StatementListNode.ExecuteStatement(ParseTreeNode statement, Array input, Pipe outputPipe, ArrayList& resultList, ExecutionContext context)

      InnerException: 

     

    Any ideas why this happens? I have SharePoint Server 2010 running on Windows Server 2008 R2. The WCF is created through Visual Studio 2010. 

     

    Thank you very much. Any help would be greatly appreciated!

     

     

     

כל התגובות

  • יום שני 03 אוקטובר 2011 14:08
    מנחה דיון
     
     

    Hi Nicole07,

    Could you please add the PSSaddin before the RunSpace is initiated, and check if it works?

    RunspaceConfiguration config = RunspaceConfiguration.Create();
    PSSnapInException ex = null;
    config.AddPSSnapIn("Microsoft.SharePoint.PowerShell", ex)

    Runspace runspace = RunspaceFactory.CreateRunspace(config);

    Thanks,
    Jinchun Chen


    Jinchun Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff AT microsoft.com(Please replace AT with @)
  • יום שני 03 אוקטובר 2011 16:16
     
     

    Hi Jinchun,

    Thank you for your reply. I'm encountering a new error right now. I deployed the WCF in a separate IIS server from Sharepoint server. Calling the WCF from silverlight yields an error as follows (from Fiddler web debugger tool):

     

    "/><Message>Access is denied. You need to run this cmdlet from an elevated process.</Message><StackTrace>   at Microsoft.PowerShell.Commands.PSSessionConfigurationCommandUtilities.ThrowIfNotAdministrator()&#xD;

       at System.Management.Automation.Cmdlet.DoBeginProcessing()&#xD;

       at System.Management.Automation.CommandProcessorBase.DoBegin()</StackTrace><Type>System.InvalidOperationException</Type></InnerException><Message>Access is denied. You need to run this cmdlet from an elevated process.</Message><StackTrace>   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)&#xD;

       at System.Management.Automation.PipelineNode.Execute(Array input, Pipe outputPipe, ArrayList&amp; resultList, ExecutionContext context)&#xD;

    The code I am running is as follows:

     

    using (RunspaceInvoke powershell = new System.Management.Automation.RunspaceInvoke())

                {

                    IList errors = null;

                    script = "Enable-PSRemoting -force; Enable-WSManCredSSP -Role client -DelegateComputer SPSERVER -force; $user = \"DOMAIN\\sp.farm\";$adminPass = ConvertTo-SecureString \"Password\" -asplaintext -force; $spcred = new-Object management.automation.pscredential $user,$adminPass; $session = New-PSSession 'SPSERVER' -Authentication CredSSP -Credential: $spcred ;invoke-command -session $session -scriptblock{ Add-PSSnapin Microsoft.SharePoint.Powershell -EA 0}; invoke-command -session $session -scriptblock{ New-SPManagedPath mercy -HostHeader -Explicit}";

     

                    Collection<PSObject> results = powershell.Invoke(script, null, out errors);

                    powershell.Dispose();

     

                    return results;

                }

     

    Testing from WCF itself is successful; however, when I call my WCF service which is deployed in a separate IIS server, it yields the above error. I'm guessing this is because the powershell is not run as administrator from WCF. If so, do you know how to resolve this? Thank you so much!

  • יום שלישי 04 אוקטובר 2011 08:04
     
     תשובה

    Hi,

    This issue has been solved by adding the account of the SP administrator in the application pool where WCF is hosted. The code above now runs successfully.

     

    Thank you very much! This also solves the problem in http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/6a9d48a8-9474-4575-852f-1ff189b3e8e5/ and http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/93884b58-92eb-43f2-b1e5-9e61ad30430d/?prof=required

     

    Sorry for the multiple posts because I encountered different errors upon trying various things! Thanks!

    • סומן כתשובה על-ידי Wayne Fan יום חמישי 06 אוקטובר 2011 06:55
    •  
  • יום רביעי 25 יולי 2012 04:41
     
     

    Hi,

    This issue has been solved by adding the account of the SP administrator in the application pool where WCF is hosted. The code above now runs successfully.

    Thank you very much! This also solves the problem in http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/6a9d48a8-9474-4575-852f-1ff189b3e8e5/ and http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/93884b58-92eb-43f2-b1e5-9e61ad30430d/?prof=required

    Sorry for the multiple posts because I encountered different errors upon trying various things! Thanks!

    Hey Hi ,

               I also have the same problem as you,could you explain how did u add the account to the application pool of the WCF

    Thanx in advance