Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Power Shell命令输出未显示在ASP.net文本框中_C#_Asp.net_Powershell - Fatal编程技术网

C# Power Shell命令输出未显示在ASP.net文本框中

C# Power Shell命令输出未显示在ASP.net文本框中,c#,asp.net,powershell,C#,Asp.net,Powershell,我使用MS VS 2015创建ASP.net Web表单应用程序。根据代码,我从NuGet软件包安装System.Management.Automation库。我正在尝试在asp文本框中显示我的PowerShell命令输出。单击ExecuteCode按钮后,代码不显示错误。但产出没有显示出来。在选项卡的上角连续显示加载符号的页面。PowerShell命令也适用于PowerShell控制台IDE。我希望你能找到解决办法 我的PowerShell命令为: mosquitto_sub -h 192.

我使用MS VS 2015创建ASP.net Web表单应用程序。根据代码,我从NuGet软件包安装System.Management.Automation库。我正在尝试在asp文本框中显示我的PowerShell命令输出。单击ExecuteCode按钮后,代码不显示错误。但产出没有显示出来。在选项卡的上角连续显示加载符号的页面。PowerShell命令也适用于PowerShell控制台IDE。我希望你能找到解决办法

我的PowerShell命令为:

 mosquitto_sub -h 192.168.8.184 -p 1883 -t "topic"

My.aspx代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="FFAS.WebForm2" %>
<!DOCTYPE html>
<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="MultiLine" Width="433px" Height="73px" ></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>

PowerShell命令线束
PowerShell命令

结果
我的aspx.cs代码为:

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());
    }
}
protectedvoid ExecuteCode\u单击(对象发送方,事件参数e)
{
//清除结果文本框
ResultBox.Text=string.Empty;
//初始化PowerShell引擎
var shell=PowerShell.Create();
//将脚本添加到PowerShell对象
shell.Commands.AddScript(Input.Text);
//执行脚本
var results=shell.Invoke();
//显示结果,将BaseObject转换为字符串
//注意:对于类似控制台的输出,使用| out字符串
如果(results.Count>0)
{
//我们使用字符串生成器来创建结果文本
var builder=新的StringBuilder();
foreach(结果中的var psObject)
{
//将基础对象转换为字符串并将其附加到字符串生成器。
//为换行符添加\r\n
Append(psObject.BaseObject.ToString()+“\r\n”);
}
//用HTML编码字符串(防止出现“危险”字符的安全问题,如<>
ResultBox.Text=Server.HtmlEncode(builder.ToString());
}
}

mosquitto_sub-h192.168.8.184-p1883-t“主题”是什么do/return当您直接从powershell运行它时会发生什么?另外,如果您使用更可预测的命令作为输入,如
$env:computername
或'dir c:`?感谢您的响应。此命令通过mqtt服务返回IOT传感器设备数据。当直接在power shell中运行代码时,会成功返回。根据“当您启动此客户端时,它将无限期运行,直到停止使用CTRL+C.”
shell.Invoke()
可能会在此处被阻止,因为客户端将正在运行且未完成。如果您希望保持此客户端运行并从其获得响应,您可能希望使用
BeginInvoke(inputObject,outputObject)
EndInvoke()
。检查
ExecuteAsynchronously()
这次在这个页面上,输出记录行更新,每5秒钟逐行降低一次。想显示吗?我现在正在代码中挣扎。你能把我的代码修改成新的吗?你在执行时插入了断点吗?如果知道建筑变量在赋值ResultBox.Tex时正确保存了什么,那将很有趣T