Windows PowserShell能够很简洁 快速通过Script脚本方式获得我们想要执行效果. 如何在C#中任意执行PowerShell脚本.?类似目前我要在做一个进程管理工具. 通过PowerShell脚本方式获取当前系统进程调用的详细信息. C#如何执行Shell Script:
步骤如下:
<1>前提:安装PowerShell SDK. Selenium
要在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: publicclass ShellParameter
6: {
7: public string ShellKey { get; set; }
8: public string ShellValue { get; set; }
9: }
执行脚本方法:
/// <param name="getshellstrlist">Shell脚本集合</param> |
/// <param name="getshellparalist">脚本中涉及对应参数</param> |
/// <returns>执行结果返回值</returns> |
public static string ExecuteShellScript(List<string> getshellstrlist, List<ShellParameter> getshellparalist) |
Runspace newspace = RunspaceFactory.CreateRunspace(); |
Pipeline newline = newspace.CreatePipeline(); |
if (getshellstrlist.Count > 0) |
foreach (string getshellstr in getshellstrlist) |
//Add Command ShellString |
newline.Commands.AddScript(getshellstr); |
if (getshellparalist != null && getshellparalist.Count > 0) |
foreach (EntityModel.ShellParameter getshellpar in getshellparalist) |
//newspace.SessionStateProxy.SetVariable(getshellpar.ShellKey,getshellpar.ShellValue); |
CommandParameter cmdpara = new CommandParameter(getshellpar.ShellKey, getshellpar.ShellValue); |
newline.Commands[count].Parameters.Add(cmdpara); |
var getresult = newline.Invoke(); |
StringBuilder getbuilder = new StringBuilder(); |
foreach (var getresstr in getresult) |
getbuilder.AppendLine(getresstr.ToString()); |
getresutl = getbuilder.ToString(); |
Main方法中调用:
1: static void Main(string[] args)
2: {
3: Console.WriteLine("请输入你要执行的PowserShell命名:");
4: string gettakeresult=Console.ReadLine();
6: //Main Method Get All Process
7: List<string> getshellcmdlist = new List<string> ();
8: List<EntityModel.ShellParameter> getpatalist = new List<ShellParameter>
9: {
10: new ShellParameter{ ShellKey="Name",ShellValue="QQ*"}
11: };
14: if (!string.IsNullOrEmpty(gettakeresult))
15: {
16: getshellcmdlist.Add(gettakeresult);
17: }
18: //Execu Cmd
19: string getresult=Program.ExecuteShellScript(getshellcmdlist,getpatalist);
21: //Output ExecuteResult
22: Console.WriteLine("执行结果:");
23: Console.WriteLine(getresult);
24: }
执行结果: 获取以Ca作为开头名称系统进程信息 ShellScript :get-process Ca*
则利用PowerShell脚本轻松获取进程名称以Ca开头所有进程名称. 类似我们轻松在获取360在本地系统详细的版本信息: get-process 360* –fileversioninfo
- 【转】linux 定时执行shell脚本
在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...
- AngularJs中,如何在render完成之后,执行Js脚本
AngularJs是Google开源的前端JS框架.使用AngularJs, 我们能够容易地.健壮的开发出类似于Gmail一样的单页Web应用.AngularJs这个新兴的MVC前端框架,具有以下特点 ...
- crontab不执行perl脚本分析
在新装的Linux服务器上部署了一个作业监控磁盘空间并提前告警,在shell脚本里面调用了一个perl脚本发送告警邮件.结果出现了一个很奇怪的现象:如果手工执行该脚本/home/oracle/scri ...
- SQL Server自动化运维系列——批量执行SQL脚本(Power Shell)
需求描述 一般在生产环境中,在投产的情况下,需要批量的来执行SQL脚本文件,来完成整个投产,如果投产文件比较多的情况下,无疑这是一个比较痛苦的过程,所以本篇通过PowerShell脚本来批量完成. 监 ...
- jeecg安装——mysql数据库创建+手动执行初始化脚本
国产的开源项目官方文档写得那么详细,已经是很厚道了,可惜俺这种菜鸟还是会碰到各种"小"问题,做个笔记先! 1.新建数据库: CREATE DATABASE jeecg DEFAUL ...
- win7计划任务执行php脚本方法
第一步:编写bat文件 方法1:php方法 方法2:exploere浏览器 电脑上新建一个txt文本,把代码放进去.然后把他另存为xxx.bat explorer "http://网址/e/ ...
- 动态执行python脚本
前言 存在许多独立的python脚本,这些脚本可能会增加,也可能会减少,现在需要按照某种顺序调度这些程序.在python的standard library中,有一个模块imp可以实现动态的调用ptho ...
- shell中创建mysql库和执行sql脚本
以前执行oracle脚本都是放到plsql中执行 mysql 脚本执行: (1).先创建一个worlddb库 (2).导入sql脚本: 这就ok啦,哈哈.
- Linux中执行shell脚本的4种方法总结
bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...
随机推荐
- jQuery中的常用内容总结(三)
jQuery中的常用内容总结(三) 转载请注明地址:http://www.cnblogs.com/funnyzpc/p/7571998.html 内容提要 选择器(第一节) 选择器的扩展方法(第一节) ...
- MongoDB学习教程(3)-常用命令
1.MongoDB 条件操作符 描述 条件操作符用于比较两个表达式并从mongoDB集合中获取数据. 在本章节中,我们将讨论如何在MongoDB中使用条件操作符. MongoDB中条件操作符有: (& ...
- ZOJ2185 简单分块 找规律
初步找大概位置,然后找精确位置,算是简单化的分块吧! #include<cstdio> #include<cstdlib> #include<iostream> u ...
- epoll模型的使用
1. 创建epoll句柄 int epfd = epoll_create(int size); 该函数生成一个epoll专用的文件描述符.它其实是在内核申请一空间,用来存放你想关注的socket fd ...
- java基础--static关键字的使用
一.static关键字的作用 使类成员完全独立于该类的任何对象.通常情况下,类成员必须通过它的类的对象访问,但是被static修饰的成员,能够被自己访问,而不必引用特定的实例. 一旦一个成员被声明为s ...
- ASP.NET MVC 分页
概述:数据比较多的时候,常使用分页.这里使用bootpag.js和PagedList这两个插件实现. 准备JS的引用 1.这个是bootstrap 中pagination的库 2..NET后台ToPa ...
- jQuery图片轮播(一)轮播实现并封装
利用面向对象自己动手写了一个封装好的jquery轮播对象,可满足一般需求,需要使用时只需调用此对象的轮播方法即可. demo:https://github.com/zsqosos/shopweb ...
- mybatis xml中使用where 条件中的in方法
<select id="queryCount" resultType="cn.bnsr.edu_yun.view.FileView"> SELECT ...
- vue数据请求
我是vue菜鸟,第一次用vue做项目,写一些自己的理解,可能有些不正确,欢迎纠正. vue开发环境要配置本地代理服务.把config文件加下的index.js里的dev添加一些内容, dev: { e ...
- LeetCode 101. Symmetric Tree (对称树)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...