繁体   English   中英

从C#包装Powershell脚本

[英]wraping powershell script from C#

我正在尝试使用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();
  }

预先感谢您的回答!

这是我编写的帮助程序类,旨在帮助我创建一个允许人们运行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用户的域帐户的凭据来运行脚本。 希望对你有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM