Windows PowserShell能够很简洁 快速通过Script脚本方式获得我们想要执行效果. 如何在C#中任意执行PowerShell脚本.?类似目前我要在做一个进程管理工具. 通过PowerShell脚本方式获取当前系统进程调用的详细信息. C#如何执行Shell Script:
步骤如下:
<1>前提:安装PowerShell SDK.
<2>新建Console Application项目 命名:CallPowerShellDemo .添加引用:System.Management.Automation 这个命名空间需要引用PowerShell SDK中System.Management.Automation.dll. 如果已经PowerShell SDK可以在目录:C:\Program Files\Reference Assemblies\Microsoft\WindowsPowerShell\v1.0 下找到:
<a href="http://blog.51cto.com/attachment/201201/230957910.jpg" target="_blank"></a>
添加相关引用:
//导入引入
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Management;
using CallPowerShellDemo.EntityModel;
封装参数实体:
/// <summary>
/// 定义一个封装Shell脚本命令参数实体类
/// Author:chenkai Date:2010年11月9日10:27:55
/// </summary>
public class ShellParameter
{
public string ShellKey { get; set; }
public string ShellValue { get; set; }
}
执行脚本方法:
/// <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>
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*
<a href="http://blog.51cto.com/attachment/201201/231005607.jpg" target="_blank"></a>
则利用PowerShell脚本轻松获取进程名称以Ca开头所有进程名称. 类似我们轻松在获取360在本地系统详细的版本信息:get-process 360* –fileversioninfo
<a href="http://blog.51cto.com/attachment/201201/231013705.jpg" target="_blank"></a>
<3>Windows PowerShell 与C#之间交互.
PowerShell对.NET对象是无缝支持的, 在Windows PowerShell脚本编程中也提出面向对象的概念. 对于Shell脚本中对象和C#语言对象是否具有相互可操作性. ?答案是肯定的. 类似我们现在要做一个运算, 运算逻辑定义以及执行则有Shell脚本. 只需在C#中以对象的方式传给Shell脚本中对象相关的参数.值. 自动计算.
先定义一个计算操作对象:
/// 计算操作的对象
/// Author:chenkai Date:2010年11月9日13:54:49
public class ConvertPatameter
{
public int FirstNum { get; set; }
public int SecondNum { get; set; }
public int Sum { get; set; }
}
当我们顶一个操作对象. 初始化FirstNum和SecondNum,然后把值传入到Shell脚本中执行加法运算并把结果赋给Sum参数.Shell Script中定义加法执行规则:
$a=$Result.FirstNum
$b=$Result.SecondNum
$Result.Sum=$a+$b
C#调用对象处理Shell脚本执行方法:
try
{
//zaiShell 执行方法参数
List<string> getlist = new List<string>();
getlist.Add("Set-ExecutionPolicy RemoteSigned");//先执行启动安全策略,,使系统可以执行powershell脚本文件
string pspath = System.IO.Path.Combine(Environment.CurrentDirectory, "PSDoc1.ps1");
getlist.Add(pspath);
//定义一个操作的实体对象
ConvertPatameter newconvert = new ConvertPatameter
{
FirstNum=200,
SecondNum=80,
Sum=0 16: };
if (File.Exists(pspath))
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace newspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
newspace.Open();
Pipeline newline = newspace.CreatePipeline();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(newspace);
Command getcmd = new Command(pspath);
newline.Commands.Add(getcmd);
//注入脚本一个.NEt对象 这样在powershell脚本中就可以直接通过$key访问和操作这个对象
//newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue);
newspace.SessionStateProxy.SetVariable("Result", newconvert);
//执行
var gettakeres=newline.Invoke();
foreach (var getstr in gettakeres)
{
Console.WriteLine(getstr.ToString());
}
Console.WriteLine("计算结果:" + newconvert.FirstNum.ToString() + "加上"
+ newconvert.SecondNum.ToString() + "等于" + newconvert.Sum.ToString());
Console.WriteLine("对象的值已经修改:"+newconvert.Sum.ToString());
}
}
catch (Exception se)
执行结果:
<a href="http://blog.51cto.com/attachment/201201/231021191.jpg" target="_blank"></a>
当把C#中实体对象ConvertPatameter赋给Shell脚本中对象$Result. 注意核心注册语句:
//注入脚本一个.NEt对象 这样在powershell脚本中就可以直接通过$key访问和操作这个对象
//newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue);
newspace.SessionStateProxy.SetVariable("Result", newconvert);
在脚本注册一个.NET对象并赋给Shell对象$Result,则在ShellScript脚本中利用加法运算 修改原来对象的Sum的属性.并返回给.NET对象中.这是一次关于.NEt对象和Shell Script中对象交互一次简单操作.
源代码下载见附件。
<a href="http://down.51cto.com/data/2359673" target="_blank">附件:http://down.51cto.com/data/2359673</a>
本文转自chenkaiunion 51CTO博客,原文链接:http://blog.51cto.com/chenkai/764150