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:      }

执行脚本方法:

/// <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
                //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();
        
    
    catch (Exception se)
    
        //catch Excetption 
    
    return getresutl;
}

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

C#执行PowserShell 脚本的更多相关文章

  1. 【转】linux 定时执行shell脚本

    在oracle 中可以利用dbms_job包定时执行pl/sql.sql过程,在像备份等需要在操作系统级定时任务只能采用crontab来完成 本文讲述crontab具体用法,以供备忘. 在oracle ...

  2. AngularJs中,如何在render完成之后,执行Js脚本

    AngularJs是Google开源的前端JS框架.使用AngularJs, 我们能够容易地.健壮的开发出类似于Gmail一样的单页Web应用.AngularJs这个新兴的MVC前端框架,具有以下特点 ...

  3. crontab不执行perl脚本分析

    在新装的Linux服务器上部署了一个作业监控磁盘空间并提前告警,在shell脚本里面调用了一个perl脚本发送告警邮件.结果出现了一个很奇怪的现象:如果手工执行该脚本/home/oracle/scri ...

  4. SQL Server自动化运维系列——批量执行SQL脚本(Power Shell)

    需求描述 一般在生产环境中,在投产的情况下,需要批量的来执行SQL脚本文件,来完成整个投产,如果投产文件比较多的情况下,无疑这是一个比较痛苦的过程,所以本篇通过PowerShell脚本来批量完成. 监 ...

  5. jeecg安装——mysql数据库创建+手动执行初始化脚本

    国产的开源项目官方文档写得那么详细,已经是很厚道了,可惜俺这种菜鸟还是会碰到各种"小"问题,做个笔记先! 1.新建数据库: CREATE DATABASE jeecg DEFAUL ...

  6. win7计划任务执行php脚本方法

    第一步:编写bat文件 方法1:php方法 方法2:exploere浏览器 电脑上新建一个txt文本,把代码放进去.然后把他另存为xxx.bat explorer "http://网址/e/ ...

  7. 动态执行python脚本

    前言 存在许多独立的python脚本,这些脚本可能会增加,也可能会减少,现在需要按照某种顺序调度这些程序.在python的standard library中,有一个模块imp可以实现动态的调用ptho ...

  8. shell中创建mysql库和执行sql脚本

    以前执行oracle脚本都是放到plsql中执行 mysql 脚本执行: (1).先创建一个worlddb库 (2).导入sql脚本: 这就ok啦,哈哈.

  9. Linux中执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,现在作个小结.假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在 ...

随机推荐

  1. 定制自己的vue模版

    前言 使用vue这个框架已经陆陆续续做了好几个项目了,不知不觉也已经在公司呆了4个月,转正了.时间如水...(省略一万字)./咳-不瞎扯了,公司是直接用的官方脚手架生成项目,官方模版没有vuex,ax ...

  2. asp.net中利用Jquery+Ajax+Json实现无刷新分页(二)

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="PageTest.aspx.cs ...

  3. 【疑问】用python写登录验证遇到的问题

    最近开始断断续续学习python,今天加入博客园,作为新人,和各位老师们讨教了,以后多多照顾!为了大家能看清楚所以就截图了,文末尾附源码,说不定会有那位老师给我指教一番.############### ...

  4. 轻松几句搞定【Javascript中的this指向】问题

    this关键字在JavaScript中扮演了至关重要的角色,每次它的出现都伴随着它的指向问题,这也是很多初学者容易出错的地方. 不过,这篇文章将会带你一次性搞定this指向的问题,望能给大家提供帮助! ...

  5. ES6的变量解构赋值

      前  言 ES6 解构赋值: ES6允许按照一定模式从数组和对象中提取值,然后对变量进行赋值,这被称为解构. 1.1 数组的结构赋值 1.1.1基本用法 JS中,为变量赋值直接指定.例如下面代码: ...

  6. Extjs6(四)——侧边栏导航根据路由跳转页面

    本文基于ext-6.0.0 之前做的时候这个侧边栏导航是通过tab切换来切换页面的,但是总感觉不太对劲,现在终于发现怎么通过路由跳转了,分享给大家,可能有些不完善的地方,望大家读后可以给些指点.欢迎留 ...

  7. win10 uwp 打电话

    UWP可以使用打电话功能,在PC是用Skype,在手机是直接使用电话功能. UWP可以通过Skype打电话,那么如何通过应用间通讯,很简单使用Launcher. Skype电话使用Skype:(电话号 ...

  8. CMD(SA400 Command)

    一.CMD模糊查询: 命令行键入:CRT,WRK,ADD,CPY,DSP,CHG,CLR,FND,RTV*等. 二.CMD分类查询: 命令行键入:GO CMD xxx eg:GO CMD FILE,G ...

  9. 查漏补缺系列之dapper初体验

    什么是dapper 在维护一些较老的项目的时候,往往我们会用很多sql那么这个时候我们要考虑优化这些项目的时候,我们就可以使用dapper dapper 是一款轻量级的ORM框架,它的优势很多轻量级, ...

  10. 长话短说 之 js的原型和闭包

    原型链:undefined, number, string, boolean 属于简单的值类型,函数.数组.对象.null.new obj()都是引用类型.检测值类型用typeof x 即可,检测引用 ...