C#调用PowerShell脚本
今天通过一个小例子,学习了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;
}
C#调用PowerShell脚本的更多相关文章
- 在Bat批处理中调用Powershell脚本
##如何在BAT中调用powershell,把下面代码另存为bat格式pushd %~dp0powershell.exe -command ^ "& {set-executionp ...
- 通过cmd调用Powershell脚本
一共需要3个文件,把这3个文件放在一个路径下 UTF8NoBOM.bat 这个文件是为了调用ps1 pwsh -file "%cd%\UTF8NoBOM.ps1" UTF8No ...
- cmd命令调用powershell脚本方法
cmd方法: powershell -command ". ('ps1脚本路径'); WriteInfo -param 'param参数值'" ps1脚本代码: function ...
- 怎样从 bat 批处理文件调用 PowerShell 脚本
https://stackoverflow.com/questions/19335004/how-to-run-a-powershell-script-from-a-batch-file https: ...
- 【黑客基础】Windows PowerShell 脚本学习(上)
视频地址:[黑客基础]Windows PowerShell 脚本学习 2019.12.05 学习笔记 1.$PSVersionTable :查看PowerShell的版本信息. 2.PowerShel ...
- 如何在windows计划中调用备份sharepoint2010网站集的powershell脚本
最近有个项目需要在在windows计划中使用powershell脚本备份sharepoint2010网站集,打开sharepoint的powershell执行命令管理界面的属性 查看: C:\Wind ...
- 【Azure Developer】调用SDK的runPowerShellScript方法,在Azure VM中执行PowerShell脚本示例
当需要通过代码的方式执行PowerShell脚本时,可以参考以下的示例. Azure SDK中提供了两个方法来执行PowerShell脚本 (SDK Source Code: https://gith ...
- 有关PowerShell脚本你必须知道的十个基本概念
1.PS1文件 一个PowerShell脚本其实就是一个简单的文本文件,这个文件包含了一系列PowerShell命令,每个命令显示为独立的一行,对于被视为PowerShell脚本的文本文件,它的文件名 ...
- 今天在写powershell脚本中犯的两个错误
可能是因为牙痛没睡好,今天老是犯错,还是脚本写错,特别难调. 第一个错误: powershell脚本里面,函数与函数互相调用的传参.其实就像调用普通的cmdlet一样的写法,应该这么写: Add-Sc ...
随机推荐
- SQL语句添加删除修改字段及一些表与字段的基本操作
用SQL语句添加删除修改字段 1.增加字段 alter table docdsp add dspcode char(200)2.删除字段 ALTER TABLE table_NA ...
- javascript 中寻找性能瓶颈
1.如果一个段代码很耗时的话你可以注释掉一部分你认为是很耗时的,或者干脆全部注释掉,然后再一点一点的解开. 2.js优化中最主要的还是对dom操作的优化,单纯的js执行时间是很短的,而js和dom之间 ...
- Windbg简单介绍
1.1 使用帮助 Windbg中的命令分为三种:基本命令.元命令和扩展命令.基本命令和元命令都是调试器自带的,元命令以" ."开头. 扩展命令是外部加入的,以"!&quo ...
- 《CSS网站布局实录》学习笔记(一)
今天开始,认真学习前端技术,哈哈哈~~~加油~~~ 推荐这本<CSS网站布局实录>(第2版)给初级入门选手,虽然这本书年代有点久远,不过很经典. 注明一下:这里讲述的CSS均为CSS 2. ...
- iOS9之后对于NSURL的编码转换方法变化说明
在iOS9之后,官方推荐使用下面的方法对NSString进行转换 - (nullable NSString *)stringByAddingPercentEncodingWithAllowedChar ...
- iOS判断当前控制器是否正在显示
+(BOOL)isCurrentViewControllerVisible:(UIViewController *)viewController { return (viewController.is ...
- Struts2 模型驱动及页面回显
* 要从页面中获取表单元素的值,需要在动作类中声明与页面元素同名的属性.导致动作类中既有javabean又有业务方法. * 将javabean和业务方法进行分离: * 将重 ...
- telnet简单操作 模拟请求
telnet简单操作 模拟请求 一: 二: 三: 按照以上操作即可!
- addChildViewController 用法
// // SCMyOrderViewController.m // SmartCommunity // // Created by chenhuan on 15/9/7. // Copyright ...
- app.config动态修改及读取
1.添加应用程序配置文件 右键点击项目,选择“添加”→“添加新建项”→“添加应用程序配置文件”将其添加到项目中. 2.设置配置文件 <?xmlversion="1.0"enc ...