Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to execute Powershell script in Sp-2013 from C# code?

$
0
0

I'm trying to execute powershell script from C# code. The code is following:-

       string scriptText = "Get-SPWOPIBinding";

        private string RunScript(string scriptText)
        {
           
            Runspace runspace = RunspaceFactory.CreateRunspace();
           
            runspace.Open();
            
            Pipeline pipeline = runspace.CreatePipeline();
            pipeline.Commands.AddScript(scriptText);
            
            pipeline.Commands.Add("Out-String");
            
            Collection<PSObject> results = pipeline.Invoke();
            
            runspace.Close();
            
            StringBuilder stringBuilder = new StringBuilder();
            foreach (PSObject obj in results)
            {
                stringBuilder.AppendLine(obj.ToString());
            }
            return stringBuilder.ToString();
        }

I am getting this error:- The term 'Get-SPWOPIBinding' 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.

Can anyone help me to find a solution for this?


Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>