C#执⾏PowserShell脚本
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 Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0 下到:
添加相关引⽤:
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)
{
string getresutl = null;
try
{
//Create Runspace
Runspace newspace = RunspaceFactory.CreateRunspace();
Pipeline newline = newspace.CreatePipeline();
//open runspace
newspace.Open();
if (getshellstrlist.Count > 0)
{
foreach (string getshellstr in getshellstrlist)
{
//Add Command ShellString
newline.Commands.AddScript(getshellstr);
}
}
//Check Parameter
if (getshellparalist != null && getshellparalist.Count > 0)
{
int count = 0;
foreach (EntityModel.ShellParameter getshellpar in getshellparalist)
{
//Set parameter
//注⼊脚本⼀个.NEt对象这样在powershell脚本中就可以直接通过$key访问和操作这个对象
/
/newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue);
CommandParameter cmdpara = new CommandParameter(getshellpar.ShellKey, getshellpar.ShellValue);
newline.Commands[count].Parameters.Add(cmdpara);
}
}
//Exec Restult
var getresult = newline.Invoke();
if (getresult != null)
{
StringBuilder getbuilder = new StringBuilder();
foreach (var getresstr in getresult)
{
getbuilder.AppendLine(getresstr.ToString());
}
getresutl = getbuilder.ToString();
}
//close
newspace.Close();
}
catch (Exception se)
{
//catch Excetption
}
return getresutl;
}
Main⽅法中调⽤:
static void Main(string[] args)
{
Console.WriteLine("请输⼊你要执⾏的PowserShell命名:");
string gettakeresult = Console.ReadLine();
//Main Method Get All Process
List<string> getshellcmdlist = new List<string>();
List<EntityModel.ShellParameter> getpatalist = new List<ShellParameter>
writeline方法的作用
{
new ShellParameter{ ShellKey="Name",ShellValue="QQ*"}
};
if (!string.IsNullOrEmpty(gettakeresult))
{
getshellcmdlist.Add(gettakeresult);
}
//Execu Cmd
string getresult = Program.ExecuteShellScript(getshellcmdlist, getpatalist);
//Output ExecuteResult
Console.WriteLine("执⾏结果:");
Console.WriteLine(getresult);
Program.OperatorObjectShell();
}
执⾏结果: 获取以Ca作为开头名称系统进程信息 ShellScript :get-process Ca*
则利⽤PowerShell脚本轻松获取进程名称以Ca开头所有进程名称. 类似我们轻松在获取360在本地系统详细的版本信息: get-process 360* –fileversioninfo
对于C#执⾏Shell脚本⽅式. 是通过 ,Runspace主要作⽤是分配⼀块独⽴空间,在空间之上建⽴⼀个独⽴为执⾏Shell命令唯⼀通信通
道,Runspace同时也为创建通道环境参数进⾏配置. Runspace主要⽤来构建对象和配置通信环境相关参数. Shell脚本命令通过管道内的运⾏空间主机应⽤程序执⾏,但RunSpace并没有提供对执⾏结果输出. 如果需要输出则需要pipeline.类⽀持
<3>Windows PowerShell 与C#之间交互.
PowerShell对.NET对象是⽆缝⽀持的, 在Windows PowerShell脚本编程中也提出⾯向对象的概念. 对于Shell脚本中对象和C#语⾔对象是否具有相互可操作性. ?答案是肯定的. 类似我们现在要做⼀个运算, 运算逻辑定义以及执⾏则有Shell脚本. 只需在C#中以对象的⽅式传给Shell 脚本中对象相关的参数.值. ⾃动计算.
先定义⼀个计算操作对象:
1:  /// <summary>
2:  /// 计算操作的对象
3:  /// Author:chenkai Date:2010年11⽉9⽇13:54:49
4:  /// </summary>
5:  public class ConvertPatameter
6:      {
7:  public int FirstNum { get; set; }
8:  public int SecondNum { get; set; }
9:  public int Sum { get; set; }
10:      }
当我们顶⼀个操作对象. 初始化FirstNum和SecondNum,然后把值传⼊到Shell脚本中执⾏加法运算并把结果赋给Sum参数.Shell Script中定义加法执⾏规则:
1:  $a=$Result.FirstNum
2:  $b=$Result.SecondNum
3:  $Result.Sum=$a+$b
4:
C#调⽤对象处理Shell脚本执⾏⽅法:
1:  try
2:              {
3:  //zaiShell 执⾏⽅法参数
4:                  List<string> getlist = new List<string>();
5:                  getlist.Add("Set-ExecutionPolicy RemoteSigned");//先执⾏启动安全策略,,使系统可以执⾏powershell脚本⽂件
6:
7:  string pspath = System.IO.Path.Combine(Environment.CurrentDirectory, "PSDoc1.ps1");
8:                  getlist.Add(pspath);
9:
10:  //定义⼀个操作的实体对象
11:                  ConvertPatameter newconvert = new ConvertPatameter
12:                  {
13:                      FirstNum=200,
14:                      SecondNum=80,
15:                      Sum=0
16:                  };
17:
18:  if (File.Exists(pspath))
19:                  {
20:                      RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
21:                      Runspace newspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
22:
23:                      newspace.Open();
24:                      Pipeline newline = newspace.CreatePipeline();
25:                      RunspaceInvoke scriptInvoker = new RunspaceInvoke(newspace);
28:                      Command getcmd = new Command(pspath);
29:                      newline.Commands.Add(getcmd);
30:
31:  //注⼊脚本⼀个.NEt对象这样在powershell脚本中就可以直接通过$key访问和操作这个对象
32:  //newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue);
33:                      newspace.SessionStateProxy.SetVariable("Result", newconvert);
34:
35:  //执⾏
36:                    var gettakeres=newline.Invoke();
38:  foreach (var getstr in gettakeres)
39:                    {
40:                        Console.WriteLine(getstr.ToString());
41:                    }
42:
43:                    Console.WriteLine("计算结果:" + newconvert.FirstNum.ToString() + "加上"
44:                        + newconvert.SecondNum.ToString() + "等于" + newconvert.Sum.ToString());
45:                    Console.WriteLine("对象的值已经修改:"+newconvert.Sum.ToString());
46:                  }
48:
49:              }
50:  catch (Exception se)
执⾏结果:
当把C#中实体对象ConvertPatameter赋给Shell脚本中对象$Result. 注意核⼼注册语句:
1:  //注⼊脚本⼀个.NEt对象这样在powershell脚本中就可以直接通过$key访问和操作这个对象
2:  //newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue);
3:  newspace.SessionStateProxy.SetVariable("Result", newconvert);
在脚本注册⼀个.NET对象并赋给Shell对象$Result,则在ShellScript脚本中利⽤加法运算修改原来对象的Sum的属性.并返回给.NET对象中.这是⼀次关于.NEt对象和Shell Script中对象交互⼀次简单操作.
本次操作源码下载: