Custom action powershell execution

Elmata 136 Reputation points
2021-01-11T17:49:03.87+00:00

Hello,
Someone knows a way to execute powershell through link, button... without visual studio.
Regards,
EM

SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,575 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,381 questions
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 45,091 Reputation points
    2021-01-11T19:38:19.32+00:00

    You can try using a HTA application and VBScript to run PowerShell. Or you could use Windows Forms and add the code for the form to a PowerShell script . . . and then the rest is easy.

    2166.how-to-add-a-graphical-user-interface-to-a-powershell-script-by-using-html-applications.aspx

    1 person found this answer helpful.
    0 comments No comments

  2. Echo Du_MSFT 17,116 Reputation points
    2021-01-12T08:47:26.333+00:00

    Hello @Elmata ,

    A very simple method is combin HTAs with PowerShell scripts.

    While you can't include PowerShell script code directly into your HTA, you can use VBScript to launch external processes.

    ------------------------

    Note:
    To run PowerShell scripts from VBScript code, you need to set the execution policy on your computer to enable this activity. For example, Set-ExecutionPolicy Unrestricted enables all PowerShell scripts to run on your system.

    ----------------------

    More information, please refer to this article:

    Thanks,
    Echo Du

    ======================
    Updated Answer ======================
    Hi @Elmata ,

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

    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();  
    }  
    

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

    Script Reference

    Thanks,
    Echo Du

    =====================

    If an Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  3. Elmata 136 Reputation points
    2021-01-14T16:08:33.117+00:00

    No way to manage it with a webpart script and a button ?


  4. Trevor Seward 11,681 Reputation points
    2021-01-15T01:48:37.17+00:00

    As this would cross browser security boundaries (allowing a website to execute code on the local host), this really isn't possible.