none
How to use Powershell script API in C# for Sharepoint RRS feed

  • Question

  • I am writing a windows form to execute Powershell script using the Powershell API.

    My runspace doesn't seems to have Sharepoint cmdlets available.

    This is part of my execution

    ..................................
    this.runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault());
    this.runspace.Open();

    PowerShell powerShellCommand = PowerShell.Create();
    powerShellCommand.AddScript(script);
    powerShellCommand.Runspace = this.runspace;

    ..................................

    I executed the script "Add-PsSnapin Microsoft.SharePoint.PowerShell" and it returns ok.
    What do I need to do to get Sharepoint cmdlets available?

     

    Thank You in advance


    M Sariman RKO Business Solutions Inc.
    Saturday, January 14, 2012 12:36 AM

Answers

  • Hi Mohamad,

    Please take a look at this console application that works fine for me. But bear in mind that if you are using Share Point commands your project must be compiled to target either x64 platform or Any CPU platform

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {            
                Runspace runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault());
                runspace.Open();
    
                string script = "Get-SPSite";
                PowerShell powerShellCommand = PowerShell.Create();
                powerShellCommand.Runspace = runspace;
                powerShellCommand.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");            
                powerShellCommand.AddScript(script);
                foreach (string result in powerShellCommand.Invoke<string>())
                {
                    Console.WriteLine(result);
                }
                runspace.Close();
            }
        }
    }
    


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Sunday, January 15, 2012 9:29 AM

All replies

  • Hi Mohamad,

    Please take a look at this article and video:


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Saturday, January 14, 2012 7:27 AM
  • Hi,

    Try this:

    http://www.c-sharpcorner.com/uploadfile/anavijai/how-to-create-custom-powershell-cmdlet-using-visual-studio-2010/

    Hope this will help you.


    Regards Ravi [url]http://sharepointwithravi.blogspot.com[/url]
    Saturday, January 14, 2012 7:37 PM
  • Hi Ravi,

       Out team had gone thru all those and not trying to be rude here but there are some people never test what they wrote.

     


    M Sariman RKO Business Solutions Inc.
    Saturday, January 14, 2012 8:54 PM
  • Hi Dimitri,

        Just reading at the comments from the link you sent me, no one can get that to work.


    M Sariman RKO Business Solutions Inc.
    Saturday, January 14, 2012 8:58 PM
  • Hi Mohamad,

    Please take a look at this console application that works fine for me. But bear in mind that if you are using Share Point commands your project must be compiled to target either x64 platform or Any CPU platform

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Management.Automation;
    using System.Management.Automation.Runspaces;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {            
                Runspace runspace = RunspaceFactory.CreateRunspace(InitialSessionState.CreateDefault());
                runspace.Open();
    
                string script = "Get-SPSite";
                PowerShell powerShellCommand = PowerShell.Create();
                powerShellCommand.Runspace = runspace;
                powerShellCommand.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");            
                powerShellCommand.AddScript(script);
                foreach (string result in powerShellCommand.Invoke<string>())
                {
                    Console.WriteLine(result);
                }
                runspace.Close();
            }
        }
    }
    


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Sunday, January 15, 2012 9:29 AM
  • Thanks Dmitry,

       We had that set to 64 already. Anyway reinstalling some files seems to make it work.

    Your reply is marked as answered and its a good reply.


    M Sariman RKO Business Solutions Inc.
    Monday, January 16, 2012 5:57 PM
  • Hi Mohamad,

    I'm glad you got it working as needed.


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts
    Monday, January 16, 2012 7:44 PM
  • Hi, Dmitry

    Would you be able to tell me how we can use that code for a remoting server?

    For example, to get the "Get-SPSite" on remote server.  

    Please let me know

    Thanks,

    Adnan

    Tuesday, May 29, 2012 8:06 PM
  • Hi Adnan,

    Please take a look at this article about how you can use PowerShell remoting SharePoint 2010 PowerShell Cheat Sheet


    Dmitry

    Lightning Tools LogoLightning Tools Check out our SharePoint tools and web parts | Lightning Tools Blog

    Tuesday, May 29, 2012 9:09 PM
  • Dimitry, thanks for the link above, they are helpful. However, I am coding C#.net . I am able to run the above code successfully in my local machine, but not able to do it remotely on servers. My credentials are admin, working properly. I get the error at runspace.Open(); any ideas?

    string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
    PSCredential psc = new PSCredential(domainAndUserName, password);
    string serverUri = "http://servername/Powershell?serializationLevel=Full";
    WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri(serverUri), shellUri, psc); 

    connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
                connectionInfo.ProxyAuthentication = AuthenticationMechanism.Negotiate;

    Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
                runspace.Open(); // Notes: i get error here

                string script = "Get-SPSite";
                PowerShell powerShellCommand = PowerShell.Create();
                powerShellCommand.Runspace = runspace;
                powerShellCommand.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");           
                powerShellCommand.AddScript(script);
                foreach (string result in powerShellCommand.Invoke<string>())
                {
                    Console.WriteLine(result);
                }
                runspace.Close();

    Connecting to remote server failed with the following error message : The WinRM client cannot process the request. Unencrypted traffic is currently disabled in the client configuration. Change the client configuration and try the request again. For more information, see the about_Remote_Troubleshooting Help topic.

    When using same concept via PowerShell, i can connect to remote server and able to run the powershell cmdlet there.


    • Edited by adnan1984 Tuesday, May 29, 2012 11:51 PM
    Tuesday, May 29, 2012 9:33 PM
  • Hi Dmitry,

    I cant able to run powershell script from sharepoint solution. Its giving exception when runspace.open() executes. Though I am able to run the powershell script from console application. Whats the reason?

    Thanks in advance....

    Thursday, February 14, 2013 6:49 AM
  • Dmitry,

    Just wanted to say thanks, you saved me a ton of time!

    -Scott

    Friday, February 22, 2013 10:12 PM