程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#執行PowserShell 腳本

C#執行PowserShell 腳本

編輯:C#入門知識

Windows PowserShell能夠很簡潔 快速通過Script腳本方式獲得我們想要執行效果. 如何在C#中任意執行PowerShell腳本.?類似目前我要在做一個進程管理工具. 通過PowerShell腳本方式獲取當前系統進程調用的詳細信息. C#如何執行Shell Script:

步驟如下:

<1>前提:安裝PowerShell SDK.

要在C#執行Power Shell 腳本.需要在PowerShell的SDK添加相關引用. Windows 7系統自動集成Windows PowerShell 2.0版本.如果尚未請點擊下載安裝

<2>新建Console Application項目 命名:CallPowerShellDemo .添加引用:System.Management.Automation 這個命名空間需要引用PowerShell SDK中System.Management.Automation.dll. 如果已經PowerShell SDK可以在目錄:C:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0 下找到:

2010-11-09_113358

 

 

 

 

 

 

 

 

添加相關引用:

   1:  //導入引入
   2:  using System.Management.Automation;
   3:  using System.Management.Automation.Runspaces;
   4:  using System.Management;
   5:  using CallPowerShellDemo.EntityModel;

封裝參數實體:

   1:   /// <summary>
   2:      /// 定義一個封裝Shell腳本命令參數實體類
   3:      /// Author:chenkai Date:2010年11月9日10:27:55
   4:      /// </summary>
   5:    public  class ShellParameter
   6:      {
   7:        public string ShellKey { get; set; }
   8:        public string ShellValue { get; set; }
   9:      }

執行腳本方法:

/// <summary> /// 執行PowserShell 腳本核心方法 /// </summary> /// <param name="getshellstrlist">Shell腳本集合</param> /// <param name="getshellparalist">腳本中涉及對應參數</param> /// <returns>執行結果返回值</returns> public static string ExecuteShellScript(List<string> getshellstrlist, List<ShellParameter> getshellparalist)
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved