C#调用powershell的公共方法

折叠 C# 代码
  1. using System.Management;   
  2. using System.Management.Automation;   
  3. using System.Management.Automation.Runspaces;  
执行脚本代码如下:
 
折叠 C# 代码
  1. ///    
  2.     /// PowerShell脚本基础   
  3.     ///    
  4.     public static class PowerShell   
  5.     {   
  6.         ///    
  7.         /// 运行脚本信息,返回脚本输出   
  8.         ///    
  9.         /// 需要运行的脚本   
  10.         /// output of the script   
  11.         public static string RunScript(List<string> scripts,List 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.     ///    
  45.     /// 脚本参数   
  46.     ///    
  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 { getset; }   
  4.         public int y { getset; }   
  5.         public int z { getset; }   
  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() { 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,,就可以执行程 序看结果:
 
 
此结果由powershell脚本返回

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/8183550/viewspace-697284/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/8183550/viewspace-697284/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值