现在的位置: 首页 > 综合 > 正文

程序中调用powershell 命令的基本步骤

2013年10月02日 ⁄ 综合 ⁄ 共 504字 ⁄ 字号 评论关闭

 1, 创建System.Management.Automation.Runspaces.InitialSeeeionState对象并导入 Snap-in or Cmdlets Entry 等信息用于定制运行环境.

 

  2,以 InitialSessionState对象为参数创建System.Management.Automation.Runspaces.Runspace对象,并打开该Runspace.

 

  3,创建一个System.Management.Automation.PowerShell对象 并 指定其 Runspace.

 

  4, 根据要调用的命令, 创建一个或多个 System.Management.Automation.Command对象, 并添加相应的参数;将这些命令按照所期望的Pipeline顺序添加到 PowerShell.Commands.

 

  5, 调用Powershell  对象的 invoke 函数而执行Powershell 命令,

 

  6,处理以上调用所返回的结果集, 以及Error, Warning, Verbose, Progress.

 

  7,Dispose  所使用的Powershell 对象,关闭Runspace.

抱歉!评论已关闭.