1

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

该脚本根据用户的输入进行操作,并根据它们进行输出。

我想获得 powershell 输出,将它们显示给我的 C# 客户端,然后根据他的选择输入输入。

这是我的脚本,下面的代码是我想出的,但它不起作用。(我只能在最后得到输出,并且只有当我的 powershell 脚本中没有 Read-Host 时)。希望它对如何有所帮助。

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();
  }

非常感谢您的回答!

4

1 回答 1

0

这是我编写的一个帮助程序类,用于帮助我创建一个允许人们运行 powershell 脚本的 ASP.NET Web 应用程序。

PSHelper.cs

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

namespace admin_scripts {
  public class PSHelper {
    // See the _ps_user_lookup method for demonstration of using this method
    public static Runspace new_runspace() {
      InitialSessionState init_state = InitialSessionState.CreateDefault();
      init_state.ThreadOptions = PSThreadOptions.UseCurrentThread;
      init_state.ImportPSModule(new[] { "ActiveDirectory", "C:\\ps_modules\\disable_user.psm1" });  
                                     // Custom  PS module containing functions related
                                     // to disabling AD accounts at work.
                                     // You would use your own module here, obviously.
      return RunspaceFactory.CreateRunspace(init_state);
    }

    // This method is for dead-simple scripts that you only want text output from
    // Not as flexible as the previous method, but it's good for the really simple cases
    public static string run_simple_script( string body ) {
      Runspace runspace = new_runspace();
      runspace.Open();

      Pipeline pipeline = runspace.CreatePipeline();
      pipeline.Commands.AddScript(body);
      pipeline.Commands.Add("Out-String");

      Collection<PSObject> output = pipeline.Invoke();
      runspace.Close();

      StringBuilder sb = new StringBuilder();
      foreach( PSObject line in output ) {
        sb.AppendLine(line.ToString());
      }
      return sb.ToString();
    }
  }
}

我会像这样从 ASP.NET 处理程序调用脚本:

private PSObject _ps_user_lookup( string id_param ) {
  Runspace runspace = PSHelper.new_runspace();
  runspace.Open();
  using( Pipeline pipe = runspace.CreatePipeline() ) {
    Command cmd = new Command("Get-ADUser");
    cmd.Parameters.Add(new CommandParameter("Identity", id_param));
    cmd.Parameters.Add(new CommandParameter("Properties", "*"));
    cmd.Parameters.Add(new CommandParameter("Server", "REDACTED"));
    pipe.Commands.Add(cmd);
    return pipe.Invoke().FirstOrDefault();
  }
}

这种安排的真正美妙之处在于,Web 应用程序将使用 Web 用户域帐户的凭据运行脚本。希望对你有帮助。

于 2013-05-22T14:45:09.473 回答