I need to associate a text box with a command

I have a webpage that allows to run a powershell command from a text field using

shell.Commands.AddScript(Input.Text);

This works but I want to make it work with a script on the server plus a parameter I add to it from the text feild instead of the powershell command itself - eg;

shell.Commands.AddScript("C:\\Users\\user001\\Desktop\\ping_host.ps1" + Input.Text);

Above does not seem to work but if I just use this it does:

shell.Commands.AddScript("C:\\Users\\user001\\Desktop\\ping_host.ps1");

What am I doing wrong? below is the entire program:
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("C:\\Users\\user001\\Desktop\\ping_host.ps1" + 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());
            }
        }
    }
}

Here is the default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="PowerShellExecution.Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
</head>
<body>
<form id="form1" runat="server">
    <div>
        <table>
            <tr><td>&nbsp;</td><td><h1 align="left">PowerShell Command Harness</h1></td></tr>
            <tr><td>&nbsp;</td><td>&nbsp;</td></tr>
            <tr><td>&nbsp;</td><td>PowerShell Command</td></tr>
            <tr><td>
                <br />
                </td><td>
                <asp:TextBox ID="Input" runat="server" TextMode="SingleLine" Width="433px"  ></asp:TextBox>
            </td></tr>
            <tr><td>
                &nbsp;</td><td>
                <asp:Button ID="ExecuteCode" runat="server" Text="Execute" Width="200" onclick="ExecuteCode_Click" />
            </td></tr>
                <tr><td>&nbsp;</td><td><h3>Result</h3></td></tr>
                <tr><td>
                    &nbsp;</td><td>
                    <asp:TextBox ID="ResultBox" TextMode="MultiLine" Width="700" Height="200" runat="server"></asp:TextBox>
                </td></tr>
        </table>
    </div>
</form>
</body>
</html>

Comments

  • maybe needs a space?

    hell.Commands.AddScript("C:\\Users\\user001\\Desktop\\ping_host.ps1 " + Input.Text);
  • A space where? If you mean on either side of the plus symbol it does and that did not do it.
  • dont know much about this.. but can you echo out

    "C:\\Users\\user001\\Desktop\\ping_host.ps1" + Input.Text;

    and then open command line and paste exactly what was echoed out .. and see if it runs?

    Just to make sure the issues is not the string
  • Turns out I needed a space after the script:

    shell.Commands.AddScript("C:\\Users\\user001\\Desktop\\ping_host.ps1 " + Input.Text);

    Notice the space after ping_host.ps1 - you were on to it :)
  • yea thats where i put the space at above :D
Sign In or Register to comment.