C# 从C包装powershell脚本#

C# 从C包装powershell脚本#,c#,powershell,C#,Powershell,我正在尝试用C#GUI客户端包装一个已经制作好的powershell脚本 该脚本根据用户的输入进行操作,并根据这些输入进行输出 我想获取powershell输出,将其显示给我的C#客户端并 然后根据他的选择输入 这是我的脚本,下面的代码是我编写的,但它不起作用。 (只有在powershell脚本中没有读取主机的情况下,我才能在最后获得输出)。希望对你有所帮助 Write-Host Write-Host 'Hello World!' Write-Host "Good-bye World! `n"

我正在尝试用C#GUI客户端包装一个已经制作好的powershell脚本

该脚本根据用户的输入进行操作,并根据这些输入进行输出

我想获取powershell输出,将其显示给我的C#客户端并 然后根据他的选择输入

这是我的脚本,下面的代码是我编写的,但它不起作用。 (只有在powershell脚本中没有读取主机的情况下,我才能在最后获得输出)。希望对你有所帮助

Write-Host
Write-Host 'Hello World!'
Write-Host "Good-bye World! `n"

$option = Read-Host "Please enter your number"

switch ($option){
  "1"
  {
    Write-Host "jack"
  }
  "2"
  {
    Write-Host "john"
  }
  "3"
  {
    Write-Host "joe"
  }
}

public void WrapPowerShell()
{
        string directory = Directory.GetCurrentDirectory();

        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = @"powershell.exe";
        startInfo.Arguments = string.Format(@"& '{0}'", directory + @"\hello.ps1");
        startInfo.RedirectStandardOutput = true;
        startInfo.RedirectStandardError = true;
        startInfo.UseShellExecute = false;
        startInfo.CreateNoWindow = false;
        Process process = new Process();
        process.StartInfo = startInfo;
        process.Start();



        string output = process.StandardOutput.ReadToEnd();
        // Assert.IsTrue(output.Contains("StringToBeVerifiedInAUnitTest"));

        string errors = process.StandardError.ReadToEnd();
  }

非常感谢您的回答

这是一个帮助器类,我编写该类是为了帮助我创建一个ASP.NET web应用程序,允许人们运行powershell脚本

PSHelper.cs

使用System.Collections.ObjectModel;
使用系统、管理、自动化;
使用System.Management.Automation.Runspaces;
使用系统文本;
命名空间管理脚本{
公共类PSHelper{
//有关使用此方法的演示,请参阅_ps_user_lookup方法
公共静态运行空间新建_运行空间(){
InitialSessionState init_state=InitialSessionState.CreateDefault();
init_state.ThreadOptions=PSThreadOptions.UseCurrentThread;
init_state.ImportPSModule(新[]{“ActiveDirectory”,“C:\\ps_modules\\disable_user.psm1”});
//包含相关函数的自定义PS模块
//在工作中禁用AD帐户。
//显然,你会在这里使用你自己的模块。
返回RunspaceFactory.CreateRunspace(init_状态);
}
//此方法适用于只希望从中输出文本的死简单脚本
//不像前面的方法那样灵活,但它适用于非常简单的情况
公共静态字符串运行\简单\脚本(字符串体){
运行空间运行空间=新建运行空间();
Open();
Pipeline Pipeline=runspace.CreatePipeline();
pipeline.Commands.AddScript(body);
pipeline.Commands.Add(“输出字符串”);
集合输出=pipeline.Invoke();
runspace.Close();
StringBuilder sb=新的StringBuilder();
foreach(输出中的PSObject行){
sb.AppendLine(line.ToString());
}
使某人返回字符串();
}
}
}
我将从ASP.NET处理程序调用脚本,如下所示:

private PSObject\u ps\u user\u查找(字符串id\u参数){
Runspace Runspace=PSHelper.new_Runspace();
Open();
使用(Pipeline pipe=runspace.CreatePipeline()){
Command cmd=新命令(“Get ADUser”);
添加(新的CommandParameter(“Identity”,id_param));
cmd.Parameters.Add(新的CommandParameter(“属性”,“*”));
cmd.Parameters.Add(新的CommandParameter(“Server”,“redact”));
pipe.Commands.Add(cmd);
返回管道.Invoke().FirstOrDefault();
}
}

这种安排的真正好处在于,web应用程序将使用web用户域帐户的凭据运行脚本。我希望您会觉得它很有用。

我没有尝试过您的代码,只是尝试给出Powershell.exe的完整显式路径,即
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\Powershell.exe
(在64位系统上)当我尝试运行简单脚本时,我得到了处理CmdletInvocationExecOption的错误-“无法调用此函数,因为当前主机未实现它。“您知道是什么导致它吗?除非我知道您尝试运行的cmdlet。我在一台有.NET 4.5,也许还有PowerShell 3.0的机器上设置了这个。有可能其中一些需要任何或所有这些东西。如果对你有利,别忘了投票并接受:D