Run Sharepoint Powershell with C#

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to write a Sharepoint Powershell from C#. Below is my code, but please tell me how I am to do this. I want to have the following Sharepoint Powershell script run from the program below:
Enable-SPFeature -Identity PublishingSite -Url http://thissharepointsite/site/bs/
http://thissharepointsite/site/bs/ -Force

Please help!


<pre class="prettyprint public string addSharepointPublishingSitebyPowerShell(string siteAddress)
{

PowerShell ps = PowerShell.Create();

string scriptText = "Add-PSSnapin Microsoft.SharePoint.Powershell Enable-SPFeature -Identity PublishingSite -URL " + siteAddress + " -Force";

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();
}[/code]
<br/>

View the full article
 
Back
Top