Swatismita Biswal
How to run a PowerShell script from SharePoint page?
By Swatismita Biswal in SharePoint on Aug 02 2018
  • Swatismita Biswal
    Aug, 2018 2

    A button handler is created using custom webpart & the below code is executed to fire your PowerShell script.Ref below code- //scriptText parameter is the script which requires to pass private string RunScript(string scriptText) {// create PowerShell runspaceRunspace runspace = RunspaceFactory.CreateRunspace();// open itrunspace.Open();// create a pipeline and feed the script textPipeline pipeline = runspace.CreatePipeline();pipeline.Commands.AddScript(scriptText);// add an extra command to transform the script// output objects into nicely formatted stringspipeline.Commands.Add("Out-String");// execute the scriptCollection results = pipeline.Invoke();// close the runspacerunspace.Close();// convert the script result into a single stringStringBuilder stringBuilder = new StringBuilder();foreach (PSObject obj in results){stringBuilder.AppendLine(obj.ToString());}return stringBuilder.ToString(); }

    • 4


Most Popular Job Functions


MOST LIKED QUESTIONS