Saturday, 15 January 2011

c# - Get variable info from PowerShell to asp.net page -


I am currently working on an asp.net page that will run a power shell script on a server.

PowerShell script checks to see if the user input that is stored in the $ userinput variable, matches the variable $ name.

I am managed to get the variable on the PowerShell script from ASP.NET page. Default.aspx.cs searches and replaces the script on userinput and then saves the script as a modified version.

I would like to print on the asp.net page I have defined an output label on the asp.net page named output that has variable $ results from PowerShell script.

While going on, the label does not display information from the $ $ convertible result.

Thank you in advance

Test.ps1

  $ name = "test" $ userinput = "* userinput *" if (($ userinput) -a $ name) {$ result = "name $ Userinput already taken!"} And {$ result = "name $ userinput available!" }  

Default.aspx

  & lt ;! DOCTYPE html public "- // W3C // DTD XHTML 1.0 Transcription // N http: //www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> & Lt; Html xmlns = "http://www.w3.org/1999/xhtml" & gt; & Lt; Head id = "head1" runat = "server" & gt; & Lt; Title & gt; Analog cloud & lt; / Title & gt; & Lt; Link rel = "stylesheet" href = "stylesheets \ style.css" type = "text / css" /> & Lt; / Head & gt; & Lt; Body & gt; & Lt; Div id = "logo" & gt; & Lt; Img src = "images \ logo.png" alt = "logo" height = "100" width = "250" /> & Lt; / Div & gt; & Lt; Form id = "form1" runat = "server" & gt; & Lt; Table & gt; & Lt; TR & gt; & Lt; TD & gt; & Nbsp; & Lt; / TD & gt; & Lt; TD & gt; & Nbsp; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; Tr & gt; & Lt; TD & gt; & Amp; Nbsp; & Lt; / Td> & Lt; Td> Enter Application Name & lt; / Td> & Lt; / Tr & gt; & Lt; TR & gt; & Lt; TD & gt; & Lt; Br / & gt; & Lt; / TD & gt; & Lt; TD & gt; & Lt; ASP: Text box id = "Input" run = "server" textual = "single line" width = "200px" height = "20px" & gt; & Lt; / Asp: text box & gt; & Lt; ASP: label id = "output" runat = "server" text = "output text" width = "300px" height = "20px" & gt; & Lt; / ASP: Labels & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; TR & gt; & Lt; TD & gt; & Amp; Nbsp; & Lt; / TD & gt; & Lt; TD & gt; & Lt; Asp: button id = "ExecuteCode" runat = "server" text = "execute" width = "200" onclick = "ExecuteCode_Click" /> & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Form & gt; & Lt; / Body & gt; & Lt; / Html & gt;  

Default.aspx.cs

  using the 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; Using System.IO; Namespace PowerShellExecution {Public Partial Square Default: System.Web.UI.Page {Secure Zero Page_load (Object Sender, EventArgs E) {} Secure Zero ExecuteCode_Click (Object Sender, EventArgs E) {// Clean Label Output. Text = string.mpty; // Start PowerShell engine var shell = PowerShell.Create (); String strContent = null; (Using the StreamReader objReader = new StreamReader ("C: \\ user \\ sysadmin \\ Desktop \\ Test.ps1") {strContent = objReader.ReadToEnd (); } StrContent = strContent.Replace ("* userinput *", input text.); File. Type all text ("C: \\ user \\ sysadmin \\ Desktop \\ Test_modified.ps1", strContent); //this.executePowerShellCode(strContent); // Add Script to PowerShell Object Shell. Commands.AddScript ("C: \\ user \\ sysadmin \\ Desktop \\ Test_modified.ps1"); // execute script var = shell.Invoke (); // Display results, convert string to BaseObject // Note: Use Out String for Console-Output (Results.Count & gt; 0) {// We use a string builder ton, our result Make text, var builder = new StringBuilder (); Foreach (result in var psObject) {// Convert base object to a string and add it to string builder. // Add to line break builder \ r \ n Append (psObject.BaseObject.ToString () + "\ r \ n"); } // Encode the string in HTML ('dangerous' carriers such as  Text = Server.HtmlEncode (builder.ToString ());} // string url = "testpage.html"; / / response Redirect (url);}}}  


No comments:

Post a Comment