【问题标题】:How to execute a PowerShell script using c#如何使用 C# 执行 PowerShell 脚本
【发布时间】:2020-08-12 14:55:55
【问题描述】:

我需要使用 c# 执行一个 PowerShell 脚本

ProcessStartInfo startInfo = new ProcessStartInfo();
            startInfo.FileName = @"cmd.exe";
            startInfo.Arguments = @"powershell -File ""C:\Users\user1\Desktop\power.ps1""";
            startInfo.Verb = "runas";
            startInfo.RedirectStandardOutput = true;
            startInfo.RedirectStandardError = true;
            startInfo.UseShellExecute = false;
            startInfo.CreateNoWindow = true;
            Process process = new Process();
            process.StartInfo = startInfo;
            process.Start();
            process.WaitForExit();

这不起作用。有人能帮我吗? 我有一个 PowerShell 脚本文件,有时它会有要传递的参数

【问题讨论】:

  • 你遇到了什么错误?
  • @LinkedListT 没有任何回报。进程卡在 waitforexit() 代码中
  • 尝试这样的事情:stackoverflow.com/q/43855697/9936356
  • 您正在编写无效代码。要使用runas 的动词,必须将UseShellExecute 设置为true。那么你究竟想以管理员身份运行什么?
  • @LexLi 如果我删除它,我可以像这样运行 .ps1 文件吗?我不知道如何执行 .ps1 文件

标签: c# asp.net .net windows powershell


【解决方案1】:

这里的答案都没有帮助我,但这个答案确实帮助了我,并且取自这篇博文,因此感谢作者。

https://duanenewman.net/blog/post/running-powershell-scripts-from-csharp/

以下是您在 Visual Studio 中创建一个可以绕过任何限制的控制台应用程序时运行 PowerShell 脚本的完整代码。

只需在下面将 ps1File 变量更改为您的需要。

using System;
using System.Diagnostics;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {

            InstallViaPowerShell();
        }

       public static void InstallViaPowerShell()
        {

            var ps1File = @"C:\Users\stevehero\source\repos\RGB Animated Cursors PowerShell\RGB Animated Cursors\install-scheme.ps1";

            var startInfo = new ProcessStartInfo()
            {
                FileName = "powershell.exe",
                Arguments = $"-NoProfile -ExecutionPolicy ByPass -File \"{ps1File}\"",
                UseShellExecute = false
            };
            Process.Start(startInfo);

        }
    }
}

【讨论】:

    【解决方案2】:

    从 C# 运行 .ps1 是很常见的事情,with many examples all over the web 和 Youtube 上的视频展示了如何/带有示例。

    Call a Powershell script from c# code

    public static int RunPowershellScript(string ps)
    {
    int errorLevel;
    ProcessStartInfo processInfo;
    Process process;
    
    processInfo = new ProcessStartInfo("powershell.exe", "-File " + ps);
    processInfo.CreateNoWindow = true;
    processInfo.UseShellExecute = false;
    
    process = Process.Start(processInfo);
    process.WaitForExit();
    
    errorLevel = process.ExitCode;
    process.Close();
    
    return errorLevel;
    }
    

    以及查看这些 stackoverflowthreads。

    To call a PowerShell script file (example.ps1) from C#

    When do we need to set UseShellExecute to True?

    【讨论】:

    • 我试过了,但没有得到预期的输出。我的 shell 脚本包含用于创建文件夹的代码。进程执行,退出代码为 1。但未创建文件夹。
    • @ljas,您需要提升访问权限。尝试 processInfo.Verb = "runas"。此外,还有一个问题,您运行的脚本有环境行 \n\r 在 powershell 中读取方式不正确......关于如何使 powershell 将它们作为新参数读取的任何想法?
    • @postanote,只有当您不需要提升访问权限时,网络上许多示例的链接才有效。我已经看了一遍,到目前为止,以管理员身份有效运行 powershell 的唯一方法是运行上面的代码,并使用动词“runas”。唯一的问题是这样只会采用一个参数。
    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2019-07-28
    • 1970-01-01
    • 2018-11-09
    • 2014-04-06
    • 1970-01-01
    相关资源
    最近更新 更多