今天通过一个小例子,学习了C#如何调用PowerShell脚本文件的Function以及传参。

       private bool CallPowershell(string outputFile)
{
string ddcHost = "test";
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open(); bool result = false;
try
{
PowerShell ps = PowerShell.Create();
ps.Runspace = runspace;
ps.AddScript("param($paramList)");
ps.AddArgument(m_paramList);
ps.AddScript(string.Format("Import-Module -Name {0}", "testPath"));
ps.AddScript(string.Format("Get-MachineList {0} {1} $paramList", ddcHost, outputFile)); ps.Invoke();
if (File.Exists(outputFile))
{
result = true;
}
}
catch (System.Exception ex)
{
Trace.WriteLine("[Error] Failed to execute command, {0}", ex.Message);
}
runspace.Close();
runspace.Dispose(); return result;
}

最新文章

  1. CSS float 浮动属性
  2. 领域设计之模型充血、Repository对象注入
  3. PHP左、右、内连接
  4. mac下网页中文字体优化
  5. 重构4-Push Down Method(方法下移)
  6. Tomcat内存溢出详解【转载】
  7. OC基础15:内存管理和自动引用计数
  8. MVC打开电脑对话框
  9. Memcached在.net中的应用
  10. Go 自带的 http/server.go 的连接解析 与 如何结合 master-worker 并发模式,提高单机并发能力
  11. 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法
  12. docker报错:Failed to restart docker.service: Unit not found.
  13. leetcode26: 删除排序数组中的重复项
  14. NSFileManager和NSFileHandler的作用, category的理解
  15. 一个servlet处理多个请求或者叫方法
  16. Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析
  17. Prism5.0开发人员指南内容 Contents of the Developer's Guide to Prism Library 5.0 for WPF(英汉对照版)
  18. 复制IE缓存里多个文件的方法
  19. 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
  20. linux 块设备-整理(一)

热门文章

  1. 零基础学习云计算及大数据DBA集群架构师【Linux Bash Shell编程及系统自动化2015年1月21日周四】
  2. html图像入门
  3. sql数据库之间数据的转录
  4. C#面向对象的一些笔记
  5. “The SQL Server license agreenment cannot be located for the selected edition.”MSSQL安装问题
  6. 关于.net 对excel操作的方法
  7. windows下安装apache笔记
  8. THINK PHP U的用法
  9. Android webview 应用
  10. Python----Tornado安装