xxxxxxxxxx
// Install the NuGet package "System.Management.Automation" for PowerShell integration
using System.Management.Automation;
public void RunPowerShellScript(string scriptPath)
{
// Create an instance of the PowerShell engine
var powerShell = PowerShell.Create();
// Add the script path to the PowerShell instance
powerShell.AddScript(scriptPath);
// Execute the script
var results = powerShell.Invoke();
// Check for any errors
if (powerShell.HadErrors)
{
// Handle or log the errors
}
// Handle the output (if required)
foreach (var result in results)
{
// Process the output
}
}