Running PowerShell file in C #, how to do it in ASP.NET form?

I made a PowerShell script that works fine and generates a text file when I run it offline. I wanted to automate this when my ASP.NET page loads, I invoke a C # process that invokes my PowerShell script and executes, resulting in a generated text file.

The problem is the script call, but not called. Giving some error about permissions etc.

+3
source share
4 answers

To start powershell using a C # program, you can use Process.Start () with "C: \ WINDOWS \ system32 \ windowspowershell \ v1. 0 \ powershell.exe" and provide the corresponding argument to the powershell file.

For more information, run

C: \ WINDOWS \ system32 \ WindowsPowerShell \ v1.0 \ powershell.exe -

Regarding the permission issue, read Running scripts from within Windows PowerShell and make sure powershell has the correct ExecutionPolicy.

Additional information: http://www.microsoft.com/technet/scriptcenter/resources/qanda/sept06/hey0926.mspx

+5
source

, ASP.NET script ( VS IIS), , "", script, , , , , , .

, , , " ..", , , .

, ProcMon ( www.sysinternals.com) - .

+4

System.Management.Automation PowerShell. .ps1 - - . , , powershell.exe.

. , , PowerShell / . , Set-ExecutionPolicy , script.

, - TextBox, PowerShell , :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Management.Automation;
using System.Text;

namespace PowerShellExecution
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ExecuteCode_Click(object sender, EventArgs e)
        {
            // Clean the Result TextBox
            ResultBox.Text = string.Empty;

            // Initialize PowerShell engine
            var shell = PowerShell.Create();

            // Add the script to the PowerShell object
            shell.Commands.AddScript(Input.Text);

            // Execute the script
            var results = shell.Invoke();

            // display results, with BaseObject converted to string
            // Note : use |out-string for console-like output
            if (results.Count > 0)
            {
                // We use a string builder ton create our result text
                var builder = new StringBuilder();

                foreach (var psObject in results)
                {
                    // Convert the Base Object to a string and append it to the string builder.
                    // Add \r\n for line breaks
                    builder.Append(psObject.BaseObject.ToString() + "\r\n");
                }

                // Encode the string in HTML (prevent security issue with 'dangerous' caracters like < >
                ResultBox.Text = Server.HtmlEncode(builder.ToString());
            }
        }
    }
}

To do this when loading a page, you need to create a function that suits your needs and call it from the "Page_Load" function.

Below is information on how to create a page from start to finish using Visual Studio and do it, http://grokgarble.com/blog/?p=142 .

Please vote and check as appropriate if this is helpful.

Thanks Jeff

-1
source

Source: https://habr.com/ru/post/1706505/


All Articles