【问题标题】:Run a PowerShell script from C#从 C# 运行 PowerShell 脚本
【发布时间】:2019-09-30 07:11:39
【问题描述】:

我正在尝试使用 Visual Studio 构建图形平台。而且我不是开发人员,我想在单击按钮时运行 PowerShell 或批处理文件。事情是当我尝试 C# 语法时,即使我安装了 PowerShell 扩展,它也不起作用。

我尝试了一些我在 Internet 上找到的代码,使用 process.start 或尝试在所有情况下创建命令,但未定义命令名称并且它不起作用。

private void Button1_Click(object sender, EventArgs e)
{
    Process.Start("path\to\Powershell.exe",@"""ScriptwithArguments.ps1"" ""arg1"" ""arg2""");
}

我想启动我的 .ps1 脚本,但出现错误

名称进程未定义

【问题讨论】:

  • use 'System.Diagnostics.Process.Start(...)' 或者你添加 'using System.Diagnostics;'到您的 cs 文件的顶部
  • “名称进程未定义”是编译错误还是运行时错误?
  • System.Diagnostics.Process.Start("path\to\Powershell.exe");可以,但是当我定义脚本的路径时,它不会运行它。
  • 例如:System.Diagnostics.Process.Start("powershell.exe", "U:\\folder1\\folder2\\Test.ps1");不工作
  • 我认为stackoverflow.com/questions/527513/… 可能会有所帮助

标签: c# powershell


【解决方案1】:

Calling C# code in Powershell and vice versa

Powershell 中的 C#

$MyCode = @"
public class Calc
{
    public int Add(int a,int b)
    {
        return a+b;
    }
    
    public int Mul(int a,int b)
    {
        return a*b;
    }
    public static float Divide(int a,int b)
    {
        return a/b;
    }
}
"@

Add-Type -TypeDefinition $CalcInstance
$CalcInstance = New-Object -TypeName Calc
$CalcInstance.Add(20,30)

C# 中的 Powershell

所有与 Powershell 相关的功能都位于 System.Management.Automation 命名空间,...在您的项目中引用它

 static void Main(string[] args)
        {
            var script = "Get-Process | select -Property @{N='Name';E={$_.Name}},@{N='CPU';E={$_.CPU}}";

            var powerShell = PowerShell.Create().AddScript(script);

            foreach (dynamic item in powerShell.Invoke().ToList())
            {
                //check if the CPU usage is greater than 10
                if (item.CPU > 10)
                {
                    Console.WriteLine("The process greater than 10 CPU counts is : " + item.Name);
                }
            }

            Console.Read();
        }

因此,您的查询实际上也是 stackoverflow 上许多类似帖子的副本。

Powershell Command in C#

【讨论】:

    【解决方案2】:

    这对我有用,包括参数包含空格的情况:

    using (PowerShell PowerShellInst = PowerShell.Create())
            {
    
                PowerShell ps = PowerShell.Create();
                
                string param1= "my param";
                string param2= "another param";
                string scriptPath = <path to script>;
    
                ps.AddScript(File.ReadAllText(scriptPath));
    
                ps.AddArgument(param1);
                ps.AddArgument(param2);
    
                ps.Invoke();
             
            }
    

    .ps1 文件是这样的(确保在 .ps1 脚本中声明参数):

    Param($param1, $param2)
    
    $message = $param1 + " & " + $param2"
    [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
    [System.Windows.Forms.MessageBox]::Show($message)
    

    我觉得这种方法很容易理解,也很清楚。

    【讨论】:

      猜你喜欢
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-07-23
      • 2014-09-12
      • 1970-01-01
      • 2021-08-16
      • 2017-08-17
      • 1970-01-01
      相关资源
      最近更新 更多