![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | call powershell from ASP can someone post an example or point me to one where I can write an ASP page that calls powershell commands and displays results of a query like "get-disks"? thanks. |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: call powershell from ASP Here's a cheesy example that takes input from a form, evaluates it and returns the result in a listbox on the page. Of course you should never put this on a public website but it does illustrate how to host the PowerShell engine in an ASP, how to expose Web controls to the script and how to use the session object to manage the runspace. -bruce ---------------------------------------------------------------------- <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_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 runat="server"> <title>PowerAsp Demo Page</title> </head> <body> <form id="form1" runat="server"> <div> <h3>PowerShell ASP Test!</h3> <p> <asp:Label ID="DateLabel" runat="server" Text="Todays date"></asp:Label> </p> <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3" TextMode="MultiLine"> function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3 </asp:TextBox> <p> <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval Expression" Visible="true" /> <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear Listbox " Visible="true" /> </p> <asp:TextBox ID="Script" visible="false" runat="server"> # mark the date $SessionProxy.DateLabel.text = "Evaluated at $(get-date)"; function out-listbox { $input | out-string -stream | %{ $SessionProxy.ResultListBox.Items.Add($_) } } $r = $SessionProxy.Expression.text + ' = '; $r += $(if ($SessionProxy.Expression.text) { invoke-expression $SessionProxy.Expression.Text } else { 'no expression: ' + $SessionProxy.Expression.text }; ); $SessionProxy.ResultListBox.Items.Add($r); </asp:TextBox> <p> <asp:ListBox ID="ResultListBox" Width="600" runat="server" Rows="12"></asp:ListBox> </p> <script runat="server" language="VB"> Sub Evaluate(ByVal s As Object, ByVal e As EventArgs) If Me.Session("runspace") Is Nothing Then MyRunspace = _ System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() MyRunspace.Open() Me.Session.Add("runspace", MyRunspace) Me.SessionProxy = System.Collections.Hashtable.Synchronized(New Hashtable()) MyRunspace.SessionStateProxy.SetVariable("SessionProxy", Me.SessionProxy) Me.Session.Add("SessionProxy", SessionProxy) Else SessionProxy = Me.Session("SessionProxy") MyRunspace = Me.Session("runspace") End If SessionProxy("DateLabel") = DateLabel SessionProxy("Expression") = Expression SessionProxy("ResultListBox") = ResultListBox MyInvoke = New System.Management.Automation.RunspaceInvoke(MyRunspace) MyInvoke.Invoke(Script.Text) End Sub Sub Clear(ByVal s As Object, ByVal e As EventArgs) ResultListBox.Items.Clear() End Sub </script> </div> </form> </body> </html> --------------------------------------------------------------------- -bruce -- Bruce Payette [MSFT] Windows PowerShell Technical Lead Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. "GC Postmaster" <gc_postmaster@newsgroups.nospam> wrote in message news:%23PiJnJDBHHA.4060@TK2MSFTNGP03.phx.gbl... > can someone post an example or point me to one where I can write an ASP > page that calls powershell commands and displays results of a query like > "get-disks"? > > thanks. > |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: call powershell from ASP thanks so much! I'll take a look. "Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message news:OI7$WyGBHHA.3368@TK2MSFTNGP03.phx.gbl... > Here's a cheesy example that takes input from a form, evaluates it and > returns the result in a listbox on the page. Of course you should never > put this on a public website but it does illustrate how to host the > PowerShell engine in an ASP, how to expose Web controls to the script and > how to use the session object to manage the runspace. > > -bruce > > ---------------------------------------------------------------------- > <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" > Inherits="_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 runat="server"> > <title>PowerAsp Demo Page</title> > </head> > <body> > <form id="form1" runat="server"> > <div> > <h3>PowerShell ASP Test!</h3> > <p> > <asp:Label ID="DateLabel" runat="server" Text="Todays > date"></asp:Label> > </p> > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3" > TextMode="MultiLine"> > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3 > </asp:TextBox> > <p> > <asp:Button ID="Button1" OnClick="Evaluate" runat="server" > Text="Eval Expression" Visible="true" /> > <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear > Listbox " Visible="true" /> > </p> > <asp:TextBox ID="Script" visible="false" runat="server"> > # mark the date > $SessionProxy.DateLabel.text = "Evaluated at $(get-date)"; > function out-listbox > { > $input | out-string -stream | %{ > $SessionProxy.ResultListBox.Items.Add($_) > } > } > $r = $SessionProxy.Expression.text + ' = '; > $r += $(if ($SessionProxy.Expression.text) > { > invoke-expression $SessionProxy.Expression.Text > } else { > 'no expression: ' + $SessionProxy.Expression.text > }; > ); > $SessionProxy.ResultListBox.Items.Add($r); > </asp:TextBox> > <p> > <asp:ListBox ID="ResultListBox" Width="600" runat="server" > Rows="12"></asp:ListBox> > </p> > <script runat="server" language="VB"> > Sub Evaluate(ByVal s As Object, ByVal e As EventArgs) > If Me.Session("runspace") Is Nothing Then > MyRunspace = _ > > System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() > MyRunspace.Open() > Me.Session.Add("runspace", MyRunspace) > Me.SessionProxy = > System.Collections.Hashtable.Synchronized(New Hashtable()) > MyRunspace.SessionStateProxy.SetVariable("SessionProxy", > Me.SessionProxy) > Me.Session.Add("SessionProxy", SessionProxy) > Else > SessionProxy = Me.Session("SessionProxy") > MyRunspace = Me.Session("runspace") > End If > SessionProxy("DateLabel") = DateLabel > SessionProxy("Expression") = Expression > SessionProxy("ResultListBox") = ResultListBox > MyInvoke = New > System.Management.Automation.RunspaceInvoke(MyRunspace) > MyInvoke.Invoke(Script.Text) > End Sub > Sub Clear(ByVal s As Object, ByVal e As EventArgs) > ResultListBox.Items.Clear() > End Sub > </script> > </div> > </form> > </body> > </html> > --------------------------------------------------------------------- > > -bruce > -- > Bruce Payette [MSFT] > Windows PowerShell Technical Lead > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no > rights. > > "GC Postmaster" <gc_postmaster@newsgroups.nospam> wrote in message > news:%23PiJnJDBHHA.4060@TK2MSFTNGP03.phx.gbl... >> can someone post an example or point me to one where I can write an ASP >> page that calls powershell commands and displays results of a query like >> "get-disks"? >> >> thanks. >> > > |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: call powershell from ASP What's the best way then to test this page? Do I save it as an aspx or asp page? I guess I'm kinda new to ASP, cuz I'm totally confused by the Default.aspx.vb name in the code. Is this what I name the file? "Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message news:OI7$WyGBHHA.3368@TK2MSFTNGP03.phx.gbl... > Here's a cheesy example that takes input from a form, evaluates it and > returns the result in a listbox on the page. Of course you should never > put this on a public website but it does illustrate how to host the > PowerShell engine in an ASP, how to expose Web controls to the script and > how to use the session object to manage the runspace. > > -bruce > > ---------------------------------------------------------------------- > <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" > Inherits="_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 runat="server"> > <title>PowerAsp Demo Page</title> > </head> > <body> > <form id="form1" runat="server"> > <div> > <h3>PowerShell ASP Test!</h3> > <p> > <asp:Label ID="DateLabel" runat="server" Text="Todays > date"></asp:Label> > </p> > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3" > TextMode="MultiLine"> > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3 > </asp:TextBox> > <p> > <asp:Button ID="Button1" OnClick="Evaluate" runat="server" > Text="Eval Expression" Visible="true" /> > <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear > Listbox " Visible="true" /> > </p> > <asp:TextBox ID="Script" visible="false" runat="server"> > # mark the date > $SessionProxy.DateLabel.text = "Evaluated at $(get-date)"; > function out-listbox > { > $input | out-string -stream | %{ > $SessionProxy.ResultListBox.Items.Add($_) > } > } > $r = $SessionProxy.Expression.text + ' = '; > $r += $(if ($SessionProxy.Expression.text) > { > invoke-expression $SessionProxy.Expression.Text > } else { > 'no expression: ' + $SessionProxy.Expression.text > }; > ); > $SessionProxy.ResultListBox.Items.Add($r); > </asp:TextBox> > <p> > <asp:ListBox ID="ResultListBox" Width="600" runat="server" > Rows="12"></asp:ListBox> > </p> > <script runat="server" language="VB"> > Sub Evaluate(ByVal s As Object, ByVal e As EventArgs) > If Me.Session("runspace") Is Nothing Then > MyRunspace = _ > > System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() > MyRunspace.Open() > Me.Session.Add("runspace", MyRunspace) > Me.SessionProxy = > System.Collections.Hashtable.Synchronized(New Hashtable()) > MyRunspace.SessionStateProxy.SetVariable("SessionProxy", > Me.SessionProxy) > Me.Session.Add("SessionProxy", SessionProxy) > Else > SessionProxy = Me.Session("SessionProxy") > MyRunspace = Me.Session("runspace") > End If > SessionProxy("DateLabel") = DateLabel > SessionProxy("Expression") = Expression > SessionProxy("ResultListBox") = ResultListBox > MyInvoke = New > System.Management.Automation.RunspaceInvoke(MyRunspace) > MyInvoke.Invoke(Script.Text) > End Sub > Sub Clear(ByVal s As Object, ByVal e As EventArgs) > ResultListBox.Items.Clear() > End Sub > </script> > </div> > </form> > </body> > </html> > --------------------------------------------------------------------- > > -bruce > -- > Bruce Payette [MSFT] > Windows PowerShell Technical Lead > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no > rights. > > "GC Postmaster" <gc_postmaster@newsgroups.nospam> wrote in message > news:%23PiJnJDBHHA.4060@TK2MSFTNGP03.phx.gbl... >> can someone post an example or point me to one where I can write an ASP >> page that calls powershell commands and displays results of a query like >> "get-disks"? >> >> thanks. >> > > |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: call powershell from ASP I tried running it on a test front end server and I got the following error. Should I save the file as Default.aspx.vb? Sorry for the newbie question -- Regards, GC Postmaster "Bruce Payette [MSFT]" wrote: > Here's a cheesy example that takes input from a form, evaluates it and > returns the result in a listbox on the page. Of course you should never put > this on a public website but it does illustrate how to host the PowerShell > engine in an ASP, how to expose Web controls to the script and how to use > the session object to manage the runspace. > > -bruce > > ---------------------------------------------------------------------- > <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" > Inherits="_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 runat="server"> > <title>PowerAsp Demo Page</title> > </head> > <body> > <form id="form1" runat="server"> > <div> > <h3>PowerShell ASP Test!</h3> > <p> > <asp:Label ID="DateLabel" runat="server" Text="Todays > date"></asp:Label> > </p> > <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3" > TextMode="MultiLine"> > function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3 > </asp:TextBox> > <p> > <asp:Button ID="Button1" OnClick="Evaluate" runat="server" Text="Eval > Expression" Visible="true" /> > <asp:Button ID="Button2" OnClick="Clear" runat="server" Text="Clear > Listbox " Visible="true" /> > </p> > <asp:TextBox ID="Script" visible="false" runat="server"> > # mark the date > $SessionProxy.DateLabel.text = "Evaluated at $(get-date)"; > function out-listbox > { > $input | out-string -stream | %{ > $SessionProxy.ResultListBox.Items.Add($_) > } > } > $r = $SessionProxy.Expression.text + ' = '; > $r += $(if ($SessionProxy.Expression.text) > { > invoke-expression $SessionProxy.Expression.Text > } else { > 'no expression: ' + $SessionProxy.Expression.text > }; > ); > $SessionProxy.ResultListBox.Items.Add($r); > </asp:TextBox> > <p> > <asp:ListBox ID="ResultListBox" Width="600" runat="server" > Rows="12"></asp:ListBox> > </p> > <script runat="server" language="VB"> > Sub Evaluate(ByVal s As Object, ByVal e As EventArgs) > If Me.Session("runspace") Is Nothing Then > MyRunspace = _ > System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() > MyRunspace.Open() > Me.Session.Add("runspace", MyRunspace) > Me.SessionProxy = > System.Collections.Hashtable.Synchronized(New Hashtable()) > MyRunspace.SessionStateProxy.SetVariable("SessionProxy", > Me.SessionProxy) > Me.Session.Add("SessionProxy", SessionProxy) > Else > SessionProxy = Me.Session("SessionProxy") > MyRunspace = Me.Session("runspace") > End If > SessionProxy("DateLabel") = DateLabel > SessionProxy("Expression") = Expression > SessionProxy("ResultListBox") = ResultListBox > MyInvoke = New > System.Management.Automation.RunspaceInvoke(MyRunspace) > MyInvoke.Invoke(Script.Text) > End Sub > Sub Clear(ByVal s As Object, ByVal e As EventArgs) > ResultListBox.Items.Clear() > End Sub > </script> > </div> > </form> > </body> > </html> > --------------------------------------------------------------------- > > -bruce > -- > Bruce Payette [MSFT] > Windows PowerShell Technical Lead > Microsoft Corporation > This posting is provided "AS IS" with no warranties, and confers no rights. > > "GC Postmaster" <gc_postmaster@newsgroups.nospam> wrote in message > news:%23PiJnJDBHHA.4060@TK2MSFTNGP03.phx.gbl... > > can someone post an example or point me to one where I can write an ASP > > page that calls powershell commands and displays results of a query like > > "get-disks"? > > > > thanks. > > > > > |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: call powershell from ASP Woops. Error was: Active Server Pages error 'ASP 0221' Invalid @ Command directive /exchweb/bin/auth/usa/ad/test.asp, line 1 The specified 'Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" ' option is unknown or invalid. "GC Email Manager" <GCEmailManager@discussions.microsoft.com> wrote in message news:800A32EA-78D1-4CD4-A0ED-9109DC63C65C@microsoft.com... >I tried running it on a test front end server and I got the following >error. > Should I save the file as Default.aspx.vb? > > Sorry for the newbie question > > > -- > Regards, > > GC Postmaster > > > "Bruce Payette [MSFT]" wrote: > >> Here's a cheesy example that takes input from a form, evaluates it and >> returns the result in a listbox on the page. Of course you should never >> put >> this on a public website but it does illustrate how to host the >> PowerShell >> engine in an ASP, how to expose Web controls to the script and how to use >> the session object to manage the runspace. >> >> -bruce >> >> ---------------------------------------------------------------------- >> <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" >> Inherits="_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 runat="server"> >> <title>PowerAsp Demo Page</title> >> </head> >> <body> >> <form id="form1" runat="server"> >> <div> >> <h3>PowerShell ASP Test!</h3> >> <p> >> <asp:Label ID="DateLabel" runat="server" Text="Todays >> date"></asp:Label> >> </p> >> <asp:TextBox ID="Expression" Width="500" runat="server" Rows="3" >> TextMode="MultiLine"> >> function fact($x) {if ($x -lt 2 ) {1} else {$x * (fact ($x-1))}}; fact 3 >> </asp:TextBox> >> <p> >> <asp:Button ID="Button1" OnClick="Evaluate" runat="server" >> Text="Eval >> Expression" Visible="true" /> >> <asp:Button ID="Button2" OnClick="Clear" runat="server" >> Text="Clear >> Listbox " Visible="true" /> >> </p> >> <asp:TextBox ID="Script" visible="false" runat="server"> >> # mark the date >> $SessionProxy.DateLabel.text = "Evaluated at $(get-date)"; >> function out-listbox >> { >> $input | out-string -stream | %{ >> $SessionProxy.ResultListBox.Items.Add($_) >> } >> } >> $r = $SessionProxy.Expression.text + ' = '; >> $r += $(if ($SessionProxy.Expression.text) >> { >> invoke-expression $SessionProxy.Expression.Text >> } else { >> 'no expression: ' + $SessionProxy.Expression.text >> }; >> ); >> $SessionProxy.ResultListBox.Items.Add($r); >> </asp:TextBox> >> <p> >> <asp:ListBox ID="ResultListBox" Width="600" runat="server" >> Rows="12"></asp:ListBox> >> </p> >> <script runat="server" language="VB"> >> Sub Evaluate(ByVal s As Object, ByVal e As EventArgs) >> If Me.Session("runspace") Is Nothing Then >> MyRunspace = _ >> >> System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace() >> MyRunspace.Open() >> Me.Session.Add("runspace", MyRunspace) >> Me.SessionProxy = >> System.Collections.Hashtable.Synchronized(New Hashtable()) >> MyRunspace.SessionStateProxy.SetVariable("SessionProxy", >> Me.SessionProxy) >> Me.Session.Add("SessionProxy", SessionProxy) >> Else >> SessionProxy = Me.Session("SessionProxy") >> MyRunspace = Me.Session("runspace") >> End If >> SessionProxy("DateLabel") = DateLabel >> SessionProxy("Expression") = Expression >> SessionProxy("ResultListBox") = ResultListBox >> MyInvoke = New >> System.Management.Automation.RunspaceInvoke(MyRunspace) >> MyInvoke.Invoke(Script.Text) >> End Sub >> Sub Clear(ByVal s As Object, ByVal e As EventArgs) >> ResultListBox.Items.Clear() >> End Sub >> </script> >> </div> >> </form> >> </body> >> </html> >> --------------------------------------------------------------------- >> >> -bruce >> -- >> Bruce Payette [MSFT] >> Windows PowerShell Technical Lead >> Microsoft Corporation >> This posting is provided "AS IS" with no warranties, and confers no >> rights. >> >> "GC Postmaster" <gc_postmaster@newsgroups.nospam> wrote in message >> news:%23PiJnJDBHHA.4060@TK2MSFTNGP03.phx.gbl... >> > can someone post an example or point me to one where I can write an ASP >> > page that calls powershell commands and displays results of a query >> > like >> > "get-disks"? >> > >> > thanks. >> > >> >> >> |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Placing phone call using PowerShell | Marcel de Haas | PowerShell | 1 | 04-25-2008 07:08 PM |
| how to call a static method in a .net class from powershell? | YY | PowerShell | 1 | 09-11-2007 12:51 AM |
| function call from within powershell script | Anatoli | PowerShell | 2 | 08-08-2007 12:54 PM |
| Call powershell script via Batch-File | kr1999@gmx.de | PowerShell | 2 | 07-02-2007 10:50 AM |
| Call Powershell script from ASP.NET page? | Tim | PowerShell | 1 | 05-23-2007 07:55 AM |