C#调用powershell的问题。

[复制链接]
查看11 | 回复3 | 2010-10-19 10:47:26 | 显示全部楼层 |阅读模式
有个文件ps1.ps1内容是:
get-windowsfeature web-server
在powershell命令行里能直接.\ps1.ps1运行出结果。
但在程序里:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
namespace ConsoleApplication
{

class Program

{

static void Main(string[] args)

{

string result = RunScript(@"D:\ps1.ps1");//get-windowsfeature web-server
Console.Write(result);

Console.Read();

}

private static string RunScript(string scriptText)

{

Runspace runspace = RunspaceFactory.CreateRunspace();

runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();

pipeline.Commands.AddScript(scriptText);

pipeline.Commands.Add("Out-String");

Collection results = pipeline.Invoke();

runspace.Close();

StringBuilder stringBuilder = new StringBuilder();

foreach (PSObject obj in results)

{

stringBuilder.AppendLine(obj.ToString());

}

return stringBuilder.ToString();

}

}
}
一直报错,说get-windowsfeature不是可是别的cmdlet什么什么的。。。。。
55555555555555555555555555555555555555555555555555555

回复

使用道具 举报

千问 | 2010-10-19 10:47:26 | 显示全部楼层
不懂哎,挺高深的。powershell是什么?自己多琢磨琢磨吧,祝你好运。
回复

使用道具 举报

千问 | 2010-10-19 10:47:26 | 显示全部楼层
先把module import
回复

使用道具 举报

千问 | 2010-10-19 10:47:26 | 显示全部楼层
RunScript(@"D:\ps1.ps1");//引用错误
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行