除了作为一种脚本语言外,Windows PowerShell被多种应用程序使用。这是因为Windows PowerShell引擎可以被托管在一个应用程序内部。这篇博文和下一篇博文将会处理在C#应用程序中托管Windows Powershell的多个API.

用来托管Windows Powershell最为重要的一个类型是System.Management.Automation.PowerShell类。这个类提供了用来创建命令管道和在运行空间中执行命令的方法。

添加命令(AddCommand)

让我们来看一个非常简单的例子。假设你想得到当前机器上运行的进程的一个列表。执行命令的方式如下。

步骤 1 – 创建一个 PowerShell 对象

1
PowerShell ps = [PowerShell]::Create();

步骤 2 – 添加你想执行的命令

1
ps.AddCommand(“Get-Process”);

步骤 3 –执行这些命令

1
ps.Invoke();

你也可以像这样一步到位执行这些步骤:

1
[PowerShell]::Create().AddCommand(“Get-Process”).Invoke();

添加参数(AddParameter)

上面执行单个命令的例子没有任何参数。比方说,我想得到运行在当前机器上的所有PowerShell进程。我就可以使用AddParamete方法来给命令添加参数了。

1
2
3
[PowerShell]::Create().AddCommand(“Get-Process”)
                   .AddParameter(“Name”, “PowerShell”)
                   .Invoke();

如果想添加更多参数,可以像这样继续调用:

1
2
3
4
[PowerShell]::Create().AddCommand(“Get-Process”)
                   .AddParameter(“Name”, “PowerShell”)
                   .AddParameter(“Id”, “12768”)
                   .Invoke();

或者,如果你有一个包含参数名和参数的词典,可以使用AddParameters ,来添加所有参数。

1
2
3
4
5
6
IDictionary parameters = new Dictionary<String, String>();
parameters.Add("Name""PowerShell");
parameters.Add("Id""12768");
[PowerShell]::Create().AddCommand(“Get-Process”)
           .AddParameters(parameters)
           .Invoke()

添加语句(AddStatement)

现在我们已经执行了一个单独的命令和它的参数,让我们再来执行一堆命令。PowerShell API给我们提供了一个模拟批处理的方式,这样就可以在管道的末尾追加额外的语句。获取所有正在运行的进程,然后再获取所有正在运行的服务,可以这样做:

1
2
3
4
PowerShell ps = [PowerShell]::Create();
ps.AddCommand(“Get-Process”).AddParameter(“Name”, “PowerShell”);
ps.AddStatement().AddCommand(“Get-Service”);
ps.Invoke();

添加脚本(AddScript)

AddCommand方法只能添加命令。如果我想运行一个脚本,我可以使用 AddScript 方法。假设我们有一个简单的脚本,a.ps1,可以获取机器上所有运行的PowerShell进程的的数量。

1
2
3
4
5
6
------------D:\PshTemp\a.ps1----------
$a Get-Process -Name PowerShell
$a.count
------------D:\PshTemp\a.ps1----------
PowerShell ps = [PowerShell]::Create();
ps.AddScript(“D:\PshTemp\a.ps1”).Invoke();

AddScript API也提供了一个选项来在本地作用域运行脚本。在下面的例子中,脚本会在本地作用域中来执行,因为我们将true传递给了参数useLocalScope 。

1
2
PowerShell ps = [PowerShell]::Create();
ps.AddScript(@“D:\PshTemp\a.ps1”, true).Invoke();

处理错误,详细消息,警告和进度信息(Handling Errors, Verbose, Warning, Debug and Progress Messages)

PowerShell API也能让我们访问命令运行时产生的错误和详细信息等。你可以使用PowerShell.Streams.Error属性获取错误信息,和PowerShell.Streams.Verbose 属性获取详细信息。下面的属性也可以获取进度,调试,和警告信息。

1
2
3
4
PowerShell ps = [PowerShell]::Create()
                          .AddCommand("Get-Process")
                          .AddParameter("Name""Non-ExistentProcess");
ps.Invoke();

ps.Streams.Error有一个命令运行时产生的错误列表,下面的情况,我们在查询一个不存在的进程时得到了一个错误

1
2
3
4
5
6
Get-Process : Cannot find a process with the name "Non-ExistentProcess".
Verify the process name and call the cmdlet again.
+ CategoryInfo : ObjectNotFound: (Non-ExistentProcess:String) [Get-Process],
ProcessCommandException
+ FullyQualifiedErrorId :
NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand

PowerShell API 非常强大。它能让我们在同步/异步模式下运行命令,创建一个只能运行有限命令集的受约束的运行空间,在嵌套的管道中执行命令,等等。

在本文中的所有例子中,我们创建了一个包含了所有Windows PowerShell 内置命令的默认运行空间,这在内存消耗方面并不高效。在更多的场景中,用户可能想创建一个只包含指定命令集/语言元素的运行空间。在下一篇博文中,我们会解释如何创建一个受限制但是效率更高的运行空间。

Original Author: Indhu Sivaramakrishnan [MSFT] (Windows PowerShell Developer)

From:http://www.pstips.net/paap-windows-powershell-as-a-platform-part-1.html

      http://blogs.msdn.com/b/powershell/archive/2013/10/01/paap-windows-powershell-as-a-platform-part-1.aspx

作为平台的Windows PowerShell(一)的更多相关文章

  1. 作为平台的Windows PowerShell(二)

    在此系列文章的前一篇,我们看到了怎样使用System.Management.Automation.PowerShell 类来在c#应用程序中运行PowerShell 命令.在那些例子中,我们创建的都是 ...

  2. SharePoint 2010 最佳实践学习总结------第2章 SharePoint Windows PowerShell指南

    第2章 SharePoint Windows PowerShell指南 SharePoint 2010是SharePoint系列产品中第一个开始支持Windows PowerShell的产品,在以前的 ...

  3. 解决VS2015启动时Package manager console崩溃的问题 - Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope

    安装VS2015,启动以后,Package manager console崩溃,错误信息如下: Windows PowerShell updated your execution policy suc ...

  4. 用Windows PowerShell 控制管理 Microsoft Office 365

    如果想要通过PowerShell控制管理Office365,首先要安装Microsoft Online Services Sign-In Assistant 7.0,链接如下 Microsoft On ...

  5. 【SharePoint学习笔记】第2章 SharePoint Windows PowerShell 指南

    快速了解Windows PowerShell     从SharePoint 2010开始支持PowerShell,仍支持stsadm.exe工具:     可以调用.NET对象.COM对象.exe文 ...

  6. Office 365 - SharePoint 2013 Online 中使用Windows PowerShell

    1.如果想要在SharePoint Online中使用Windows PowerShell,首先需要安装SharePoint Online Management Shell(下载地址附后),如下图: ...

  7. 检测访问网页的浏览器呈现引擎、平台、Windows操作系统、移动设备和游戏系统

    /** * Author: laixiangran. * Created by laixiangran on 2015/12/02. * 检测访问网页的浏览器呈现引擎.平台.Windows操作系统.移 ...

  8. 使用 Windows PowerShell 来管理和开发 windowsazure.cn 账户的特别注意事项

    6月6日,微软面向中国大陆用户开放了Microsoft Azure公众预览版的申请界面.大家可以申请免费的 beta 试用,收到内附邀请码的通知邮件后只需输入激活码即可开始免费试用.具体网址为: ht ...

  9. Windows PowerShell ISE

    Windows PowerShell 集成脚本环境 (ISE) 是 Windows PowerShell 的主机应用程序.在 Windows PowerShell ISE 中,可以在单一 Window ...

随机推荐

  1. chrome禁用某个网站js脚本的执行

      1 首先打开谷歌浏览器.如下 2 点击右上角,打开菜单进入[设置] 3 打开后,第一个界面是没有这个的,要滚动到最后点击[显示高级设置...] 4 展开第二页后,点击[隐私设置]->[内容设 ...

  2. mysql系列命令解释

    mysqld - the MySQL server mysql - the MySQL command-line tool mysqlaccess - client for checking acce ...

  3. poj 2965 The Pilots Brothers' refrigerator枚举(bfs+位运算)

    //题目:http://poj.org/problem?id=2965//题意:电冰箱有16个把手,每个把手两种状态(开‘-’或关‘+’),只有在所有把手都打开时,门才开,输入数据是个4*4的矩阵,因 ...

  4. poj1226,poj3080

    看来以后用pascal的函数要小心了: 简简单单pos其实时间复杂度是二次方级的…… 今天学习的是KMP——字符匹配算法: 这两道题也都很简单,都是为这个算法练手的, 最朴素的匹配显然是穷举起始位置然 ...

  5. PHP内置的Web Server的使用

    自PHP5.4之后 PHP内置了一个Web 服务器. 让我们来看看php Web Server的简单使用: 启动php Web Server php -S localhost:8080 通过 php ...

  6. Google 多源码管理工具 gclient

    google的chromium项目是用gclient来管理源码的checkout, update等. gclient是google专门为这种多源项目编写的脚本,它可以将多个源码管理系统中的代码放在一起 ...

  7. (5)jvm垃圾回收器相关垃圾回收算法

    引用计数法[原理]--->引用计数器是经典的也是最古老的垃圾收集防范.--->实现原理:对于对象A,只要有任何一个对象引用A,则计数器加1.当引用失效时,计数器减1.只要对象A的计数器值为 ...

  8. StrutsPrepareAndExecuteFilter的作用

    FilterDispatcher是早期struts2的过滤器,后期的都用StrutsPrepareAndExecuteFilter了,如 2.1.6.2.1.8.StrutsPrepareAndExe ...

  9. jQuery基础知识— 获得内容和属性

    jQuery拥有可操作HTML元素和属性的方法.   获得内容: text()--设置或返回所选元素的文本内容 html()--设置或返回所选元素的内容(包括HTML标记) val()--设置或返回表 ...

  10. 我的WCF之旅(3):在WCF中实现双工通信

    双工(Duplex)模式的消息交换方式体现在消息交换过程中,参与的双方均可以向对方发送消息.基于双工MEP消息交换可以看成是多个基本模式下(比如请求-回复模式和单项模式)消息交换的组合.双工MEP又具 ...