添加对system.management.automation.dll的引用

简介:

不知道为什么system.management.automation.dll存放在一个隐藏文件夹里面,入下图,我把他拷贝到项目的ThirdParty目录下。

 

image

在项目目录选择对该DLL的引用。

image

 

添加命名空间

 

 

 

using System.Management.Automation.Runspaces;
using System.Management.Automation;
using System.Collections.ObjectModel;

 

 

封装调用过程

private void RunPowershell(string filePath, string parameters)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(filePath);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (var parameter in parameters.Split(' '))
{
CommandParameter commandParm = new CommandParameter(null, parameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
Collection<PSObject> psObjects;
psObjects = pipeline.Invoke();
}

客户端代码

RunPowershell(@".\x.ps1", "");

需要注意还是需要使用.\来调用脚本.




    本文转自Jake Lin博客园博客,原文链接:http://www.cnblogs.com/procoder/archive/2010/08/30/WPF-invoke-powershell.html,如需转载请自行联系原作者

目录
打赏
0
0
0
0
52
分享
相关文章
【Azure Developer】记录一次使用Java Azure Key Vault Secret示例代码生成的Jar包,单独运行出现 no main manifest attribute, in target/demo-1.0-SNAPSHOT.jar 错误消息
【Azure Developer】记录一次使用Java Azure Key Vault Secret示例代码生成的Jar包,单独运行出现 no main manifest attribute, in target/demo-1.0-SNAPSHOT.jar 错误消息
153 0
|
8月前
【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
Java获取当前项目下的文件或目录物理地址System.getProperty(“user.dir“)
VS找不到System.Web.Extensions.dll的解决办法
VS找不到System.Web.Extensions.dll的解决办法
247 0
Eclipse中项目报Target runtime com.genuitec.runtime.generic.jee60 is not defined异常的解决方法
Eclipse中项目报Target runtime com.genuitec.runtime.generic.jee60 is not defined异常的解决
610 0
Eclipse中项目报Target runtime com.genuitec.runtime.generic.jee60 is not defined异常的解决方法

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等