C#调用PowerShell脚本
2024-10-18 10:39:37
今天通过一个小例子,学习了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;
}
最新文章
- CSS float 浮动属性
- 领域设计之模型充血、Repository对象注入
- PHP左、右、内连接
- mac下网页中文字体优化
- 重构4-Push Down Method(方法下移)
- Tomcat内存溢出详解【转载】
- OC基础15:内存管理和自动引用计数
- MVC打开电脑对话框
- Memcached在.net中的应用
- Go 自带的 http/server.go 的连接解析 与 如何结合 master-worker 并发模式,提高单机并发能力
- 【手记】走近科学之为什么明明实现了IEnumerable<;T>;的类型却不能调用LINQ扩展方法
- docker报错:Failed to restart docker.service: Unit not found.
- leetcode26: 删除排序数组中的重复项
- NSFileManager和NSFileHandler的作用, category的理解
- 一个servlet处理多个请求或者叫方法
- Universal-Image-Loader源码分析(一)——ImageLoaderConfiguration分析
- Prism5.0开发人员指南内容 Contents of the Developer&#39;s Guide to Prism Library 5.0 for WPF(英汉对照版)
- 复制IE缓存里多个文件的方法
- 二分算法的应用——最大化最小值 POJ2456 Aggressive cows
- linux 块设备-整理(一)
热门文章
- 零基础学习云计算及大数据DBA集群架构师【Linux Bash Shell编程及系统自动化2015年1月21日周四】
- html图像入门
- sql数据库之间数据的转录
- C#面向对象的一些笔记
- “The SQL Server license agreenment cannot be located for the selected edition.”MSSQL安装问题
- 关于.net 对excel操作的方法
- windows下安装apache笔记
- THINK PHP U的用法
- Android webview 应用
- Python----Tornado安装