title: Execute PowerShell Script from C# MVC Web Application
body: <p>I need to execute a powershell script from my asp.net MVC Web application. My requirement is to create site collections dynamically. I have the script for it and it works perfectly.There are no arguments which are to be passed to the script. The code which I have been using has been displayed below:</p>
  
  <pre><code>RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
  
  Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
  runspace.Open();
  
  RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
  
  Pipeline pipeline = runspace.CreatePipeline();
  
  //Here's how you add a new script with arguments
  Command myCommand = new Command(scriptfiellocation);
  
  pipeline.Commands.Add(myCommand);
  pipeline.Commands.Add("Out-String");
  
  // Execute PowerShell script
  var result = pipeline.Invoke();
  </code></pre>
  
  <p>On executing the code, when I check the count of variable result it gives the count as 1. However on checking my site, there is no site collection that has been created. I am not able to identify where I am going wrong as there is no run time error and the Invoke command also seems to be running properly. </p>
  
  <p>Could anyone tell me where I might be going haywire ? Considering that the PowerShell script works perfectly when running through Management shell.</p>