I´m trying to run a PowerShell script with C# on .netCore.
I have tried many different solutions for now, but none of them seem to work. I just want to execute a PowerShell script and set ExecutionPolicies and Scope to make it work. But I always got the exception that the ExecutionPolicies don´t allow me to run the script that way.
Despite that with the actual configuration and code, you find below, I don´t get any feedback from the Debugger after reaching the point where .Invoke(); is executed. And waiting for response and letting the software doing its stuff in background leads always to a stackoverflow exception.
commandParameters is a simple Dictionary<string, string>
Any thoughts on this?
Cheers.
var iss = InitialSessionState.CreateDefault2();
// Set its script-file execution policy.
iss.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Unrestricted;
iss.ExecutionPolicy = (ExecutionPolicy)Microsoft.PowerShell.ExecutionPolicyScope.CurrentUser;
// Create a PowerShell instance with a runspace based on the
// initial session state.
PowerShell ps = PowerShell.Create(iss);
ps.AddCommand(path + "\\" + fileName);
ps.AddParameters(commandParameters);
var results = ps.InvokeAsync().GetAwaiter().GetResult();