利用ASP.NET結合PowerShell的command,來取得Server端系統的process
最近看了一些微軟的PowerShell的相關資料.....
看起來還滿好玩的..就寫了一支結合powershell的asp.net程式...
利用asp.net的process呼叫powershell執行command,並且產生一個文字檔
再透過讀取文字檔的方法..取得server端系統正在run的process
asp.net(c#)範例...
PowerShell.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PowerShell.aspx.cs" Inherits="PowerShell" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>PowerShell</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Process" />
12 <br />
13 <asp:Literal ID="Literal1" runat="server"></asp:Literal>
14 </div>
15 </form>
16 </body>
17 </html>
18
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>PowerShell</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Get Process" />
12 <br />
13 <asp:Literal ID="Literal1" runat="server"></asp:Literal>
14 </div>
15 </form>
16 </body>
17 </html>
18
PowerShell.aspx.cs
01
using System;
02
using System.Collections;
03
using System.Configuration;
04
using System.Data;
05
using System.Linq;
06
using System.Web;
07
using System.Web.Security;
08
using System.Web.UI;
09
using System.Web.UI.HtmlControls;
10
using System.Web.UI.WebControls;
11
using System.Web.UI.WebControls.WebParts;
12
using System.Xml.Linq;
13
using System.Diagnostics;
14
using System.IO;
15
16
public partial class PowerShell : System.Web.UI.Page
17
{
18
protected void Page_Load(object sender, EventArgs e)
19
{
20
21
}
22
protected void Button1_Click(object sender, EventArgs e)
23
{
24
Process proc = new Process();
25
26 //PowerShell.exe path
27
proc.StartInfo.FileName = @"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";
28
29 //取得window process,並且寫入process.txt文字檔的PowerShell command
30
proc.StartInfo.Arguments = @"Get-Process | Out-File c:\process.txt";
31
32 proc.Start();
33
34 //Wait for the process to end.
35
proc.WaitForExit();
36
37 //open process.txt
38
39 StreamReader sr;
40
41 sr = File.OpenText(@"c:\process.txt");
42
43 string ProcessInfo = sr.ReadToEnd();
44
45 sr.Close();
46
47 this.Literal1.Text = "<pre>" + ProcessInfo + "</pre>";
48
}
49
}
using System;02
using System.Collections;03
using System.Configuration;04
using System.Data;05
using System.Linq;06
using System.Web;07
using System.Web.Security;08
using System.Web.UI;09
using System.Web.UI.HtmlControls;10
using System.Web.UI.WebControls;11
using System.Web.UI.WebControls.WebParts;12
using System.Xml.Linq;13
using System.Diagnostics;14
using System.IO;15

16
public partial class PowerShell : System.Web.UI.Page17
{18
protected void Page_Load(object sender, EventArgs e)19
{20

21
}22
protected void Button1_Click(object sender, EventArgs e)23
{24
Process proc = new Process();25

26 //PowerShell.exe path
27
proc.StartInfo.FileName = @"c:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe";28

29 //取得window process,並且寫入process.txt文字檔的PowerShell command
30
proc.StartInfo.Arguments = @"Get-Process | Out-File c:\process.txt";31

32 proc.Start();
33

34 //Wait for the process to end.
35
proc.WaitForExit();36

37 //open process.txt
38

39 StreamReader sr;
40

41 sr = File.OpenText(@"c:\process.txt");
42

43 string ProcessInfo = sr.ReadToEnd();
44

45 sr.Close();
46

47 this.Literal1.Text = "<pre>" + ProcessInfo + "</pre>";
48
}49
}
執行結果:
更多PowerShell資料:
易學易用的 Windows PowerShell
using