Запустите скрипт/команду POWERSHELL из C#
Я попытался запустить код, но получил ошибку типа времени выполнения:
using System.Management.Automation;
using Microsoft.PowerShell;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (var ps = PowerShell.Create())
{
var results = ps.AddCommand().BeginInvoke();
label1.Text = "";
var R = ps.EndInvoke(results);
foreach (var item in R)
{
label1.Text = label1.Text + item.ToString();
}
}
}
}
private void button2_Click(object sender, EventArgs e)
{
using (var ps = PowerShell.Create())
{
var results = ps.AddCommand("Install-Module -Name PSFalcon -Scope CurrentUser").Invoke();
label1.Text = "";
foreach (var h in results)
{
foreach (var item in h.Properties)
{
label1.Text = label1.Text + item.MemberType;
}
}
}
}
}
}
System.Management.Automation.Runspaces.PSSnapInException: 'Cannot load PowerShell snap-in Microsoft.PowerShell.Diagnostics because of the following error: The PowerShell snap-in module U:\Downloads\WinFormsApp1\WinFormsApp1\bin\Debug\net6.0-windows\runtimes\win\lib\net6.0\Microsoft.PowerShell.Commands.Diagnostics.dll does not have the required PowerShell snap-in strong name Microsoft.PowerShell.Commands.Diagnostics, Version=7.2.5.500, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL.'