【问题标题】:How to call Powershell script and pass parameters from C# windows form如何从 C# windows 窗体调用 Powershell 脚本并传递参数
【发布时间】:2020-08-23 02:39:45
【问题描述】:

我想调用 powershell 脚本,它接受来自 c# windows 窗体的一些参数。

例如:- 我有一个接受 2 个参数的 powershell 脚本,我有一个带有两个文本框和一个提交按钮的 windows 窗体。我想从这两个文本框中将参数传递给 powershell 脚本。当我按下提交按钮时,它将调用 powershell 脚本并在 powershell 终端上运行它,并在 powershell 终端中显示结果。

【问题讨论】:

    标签: c# visual-studio powershell windows-forms-designer


    【解决方案1】:

    只要带参数启动powershell.exe

    要启动任何外部应用程序,您可能需要检查进程的工作方式。

    Process Class

    【讨论】:

      【解决方案2】:

      这是 powershell 脚本的工作示例,它为您提供 char 的 ASCII 代码。假设您有 richTextbox1 用于显示脚本的输出,并且您有 button1 可以触发它运行 - 这是代码:

              public void RunPowershell()
              {
                  var ps1File = @"C:\asc.ps1";
      
                  ProcessStartInfo startInfo = new ProcessStartInfo();
                  startInfo.FileName = @"powershell.exe";
                  startInfo.Arguments = $@"& '{ps1File}' f";
                  startInfo.RedirectStandardOutput = true;
                  startInfo.RedirectStandardError = true;
                  startInfo.UseShellExecute = false;
                  startInfo.CreateNoWindow = true;
                  Process process = new Process();
                  process.StartInfo = startInfo;
                  process.Start();
      
                  string output = process.StandardOutput.ReadToEnd();
                  string errors = process.StandardError.ReadToEnd();
      
                  richTextBox1.Text = $"Output: {output}{Environment.NewLine}Errors = {errors}";
              }
      
              private void button1_Click(object sender, EventArgs e)
              {
                  RunPowershell();
              }
      

      这将为 char 'f' 提供 ascii 代码。

      下面是附在示例中的 PS 脚本:

      function Syntax {
          Write-Host
          Write-Host "Asc.ps1,  Version 1.00"
          Write-Host "Returns the numeric ASCII value for the specified character"
          Write-Host
          Write-Host "Usage:   " -NoNewline
          Write-Host "ASC.PS1  char" -ForegroundColor White
          Write-Host
          Write-Host "Returns: Numeric ASCII value of " -NoNewline
          Write-Host "char" -ForegroundColor White
          Write-Host
          Write-Host "Notes:   The ASCII value is written to Standard Output (usually the screen)"
          Write-Host "         as well as returned as `"errorlevel`""
          Write-Host "         If more than 1 character is passed on the command line, only the first"
          Write-Host "         character will be used, the remainder of the string will be ignored."
          Write-Host "         The `"errorlevel`" will equal the ASCII value, or 0 if no parameter"
          Write-Host "         was passed, or -1 in case of errors or for this help"
          Write-Host
          Write-Host "Credits: Code to pass exit code as `"errorlevel`" to the calling batch file"
          Write-Host "         or PowerShell script by Serge van den Oever:"
          Write-Host "         weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script" -ForegroundColor DarkGray
          Write-Host
          Write-Host "Written by Rob van der Woude"
          Write-Host "http://www.robvanderwoude.com"
      
          $Host.SetShouldExit( -1 )
          Exit
      }
      
      if ( $args.Length -eq 1 ) {
          if ( $args[0] -eq "/?" ) {
              Syntax
          } else {
              $input = $args[0]
          }
      } else {
          Syntax
      }
      
      if ( $input -eq "" ) { $input = [string][char]0 }
      
      [char]$char = $input[0]
      
      Write-Host ([byte]$char) -NoNewline
      
      # Pass exit code to calling batch or PowerShell script, by Serge van den Oever
      # https://weblogs.asp.net/soever/returning-an-exit-code-from-a-powershell-script
      $Host.SetShouldExit( [byte]$char )
      Exit
      

      将其保存到磁盘C,将其命名为asc.ps1 --> 你就有了工作示例。

      【讨论】:

      • 感谢您的帮助。您的解决方案很好,但正如我在描述中所说,我希望在 powershell 终端中输出而不是在任何文本框中。
      • 我试过你上面的例子,它显示错误:System.ComponentModel.Win32Exception: '系统找不到指定的文件'
      • 这个路径下有文件吗:C:\asc.ps1?
      • 你安装了powershell吗?你有哪个 Windows 版本?
      • 看我有 windows 10 和 powershell 版本是 7.0.0
      猜你喜欢
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 2021-12-12
      • 2015-06-07
      • 2012-05-05
      • 2014-01-07
      • 2020-09-04
      • 1970-01-01
      相关资源
      最近更新 更多