2

I use a PS script to create a list. Instead of a scheduled task, I wondered if I can put a link or a button on a sharepoint page, that when you click on it, it runs a PS Script on the SharePoint server (with Administrator privilege).

I saw this plugin that allows us to run PS script within a workflow. This is a cool thing. But I prefer running the script whenever I want (or my users want).

Do you have a good reference for this ? I saw this one. But not sure about what I should Googled.

NEW EDIT :

Hey, I'm back.

I manage to create a empty projet thanks to this.

So now, from my workstation (not SP server), I'm able to use PS script or commands. I also manage to use a dropdown list and select which script I want to execute.

I can't find how to add this custom application on my SP site (I have a aspx page, and aspx.cs). Well a kind of WebApplication if I'm right.

1
  • you want to hard code the script in C# or you want to load the script dynamically? Jul 7, 2014 at 12:09

2 Answers 2

6

You could create a Custom WebPart. A simple button Handler could execute the under given code to fire your PowerShell script.

scriptText parameter is the script which you should pass. You can HardCode it or load it from any file.

private string RunScript(string scriptText)
{
    // create Powershell runspace

    Runspace runspace = RunspaceFactory.CreateRunspace();

    // open it

    runspace.Open();

    // create a pipeline and feed it the script text

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    // add an extra command to transform the script
    // output objects into nicely formatted strings

    // remove this line to get the actual objects
    // that the script returns. For example, the script

    // "Get-Process" returns a collection
    // of System.Diagnostics.Process instances.

    pipeline.Commands.Add("Out-String");

    // execute the script

    Collection<psobject /> results = pipeline.Invoke();

    // close the runspace

    runspace.Close();

    // convert the script result into a single string

    StringBuilder stringBuilder = new StringBuilder();
    foreach (PSObject obj in results)
    {
        stringBuilder.AppendLine(obj.ToString());
    }

    return stringBuilder.ToString();
}

Reference

Please let me know if you want further clarification.

2
  • Please mark it as answer if it solves your query Jul 7, 2014 at 12:18
  • This is an answer. But now, I'm not enable to add this custom application in my SP test site. Updated question
    – Nico
    Jul 16, 2014 at 10:39
0

Very important addition. This command dont launch without next edits:

  1. This code need use in SPSecurity.RunWithElevatedPrivileges(delegate() { // code above });
  2. The account under which the portal is launched must have rights, in particular, be in the group "Recipient manager"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.