Power Shell Command Output not displayed in ASP.Net Textbox
I create ASP.Net webform application using MS VS 2015. According to code, I install System.Management.Automation library from NuGet packages.
And I'm trying to display my MQTT service command output in asp textbox. After click on ExecuteCode button, Code not showing error but output not showing.
page showing loading symbol in top corner of tab continuously. PowerShell command works at PowerShell console IDE as well.
I hope you solutions for that.
My PowerShell Command as:
mosquitto_sub -h 192.168.8.184 -p 1883 -t "topic"
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());
}
}