首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 调用powershell 相互操作,该怎么处理

2012-03-26 
C# 调用powershell 相互操作首先我们来写一个C#调用powershell的公共方法需要引用:System.Management.Auto

C# 调用powershell 相互操作
首先我们来写一个C#调用powershell的公共方法
 
需要引用:System.Management.Automation.dll
 
此dll一般装上powershell 的sdk就可以找到
 
我这里直接放到源码里了。。
 
折叠展开C# 代码

  1. using System.Management;  
  2. using System.Management.Automation;  
  3. using System.Management.Automation.Runspaces;  

执行脚本代码如下:
 
折叠展开C# 代码

  1. /// <summary>  
  2. /// PowerShell脚本基础  
  3. /// </summary>  
  4. public static class PowerShell  
  5. {  
  6. /// <summary>  
  7. /// 运行脚本信息,返回脚本输出  
  8. /// </summary>  
  9. /// <param name="scriptText"& gt;需要运行的脚本</param>  
  10. /// <returns>output of the script</returns>  
  11. public static string RunScript(List<string> scripts,List<PSParam> pars)  
  12. {  
  13. Runspace runspace = RunspaceFactory.CreateRunspace();  
  14.  
  15. runspace.Open();  
  16.  
  17. Pipeline pipeline = runspace.CreatePipeline();  
  18. foreach (var scr in scripts)  
  19. {  
  20. pipeline.Commands.AddScript(scr);  
  21. }  
  22.  
  23. //注入参数  
  24. if (pars != null)  
  25. {  
  26. foreach (var par in pars)  
  27. {  
  28. runspace.SessionStateProxy.SetVariable(par.Key,par.Value);  
  29. }  
  30. }  
  31.  
  32. //返回结果  
  33. var results = pipeline.Invoke();  
  34. runspace.Close();  
  35. StringBuilder stringBuilder = new StringBuilder();  
  36. foreach (PSObject obj in results)  
  37. {  
  38. stringBuilder.AppendLine(obj.ToString());  
  39. }  
  40. return stringBuilder.ToString();  
  41. }  
  42. }  
  43.  
  44. /// <summary>  
  45. /// 脚本参数  
  46. /// </summary>  
  47. public class PSParam  
  48. {  
  49. public string Key  
  50. {  
  51. get;  
  52. set;  
  53. }  
  54.  
  55. public object Value  
  56. {  
  57. get;  
  58. set;  
  59. }  
  60. }  

 
这句为注入脚本一个.net对象:runspace.SessionStateProxy.SetVariable(par.Key,par.Value);  
 
这样在powershell脚本中就可以直接通过$key访问和操作这个对象
 
下面来看调用实例:
 
定义一个.net对象,以便powershell中调用:
折叠展开C# 代码

  1. class info  
  2. {  
  3. public int x { get; set; }  
  4. public int y { get; set; }  
  5. public int z { get; set; }  
  6. }  



实例化一个对象psobj。。就是上面定义的info
 
给x,y赋值
然后把此对象传给powershell脚本,,参数标识写做arg
 
折叠展开C# 代码

  1. static void Main(string[] args)  
  2. {  
  3. List<string> ps = new List<string>();  
  4. ps.Add("Set-ExecutionPolicy RemoteSigned");//先执行启动安全策略,,使系统可以执行powershell脚本文件  
  5.  
  6. string pspath = System.IO.Path.Combine(Environment.CurrentDirectory,"ps.ps1");  
  7.  
  8. ps.Add(pspath);// 执行脚本文件  
  9.  
  10. info psobj = new info();  
  11. psobj.x = 20;  
  12. psobj.y = 10;  
  13.  
  14. BaseCommon.PSParam par=new BaseCommon.PSParam();  
  15. par.Key="arg";  
  16. par.Value=psobj;  
  17.  
  18. BaseCommon.PowerShell.RunScript(ps, new List<BaseCommon.PSParam>() { par });  
  19.  
  20. Console.WriteLine(psobj.x + " + " + psobj.y + " = " + psobj.z);  
  21.  
  22. Console.ReadLine();  
  23. }  

 
接下来就看写一个简单的powershell脚本来操作.net实例了
 

  1. $a=$arg.x  
  2. $b=$arg.y  
  3. $arg.z=$a+$b  

 
其中$arg就表示上面注入的psobj对象。。标识为arg
 
把此脚本存在此程序的bin\debug目录下。文件名为:ps.ps1,,就可以执行程 序看结果:
 
10+20=30
 
此结果由powershell脚本返回

源码下载:http://www.jiamaocode.com/Cts/1193.shtml


[解决办法]
帮顶了 没有看懂

热点排行