I've done the following code to execute a powershell script:
public static void LaunchCommand(string command, IList<String> arguments)
{
Console.WriteLine($"Launching powershell command: {command} {String.Join(" ", arguments)}");
using (PowerShell ps = PowerShell.Create())
{
// specify the script code to run.
ps.AddScript(command);
ps.Streams.Debug.DataAdded += OnDebugAdded;
ps.Streams.Information.DataAdded += OnInfoAdded;
ps.Streams.Warning.DataAdded += OnWarningAdded;
ps.Streams.Error.DataAdded += OnErrorAdded;
//Transformation into a non-generic version
ArrayList argumentsList = new ArrayList();
foreach (string argument in arguments)
{
argumentsList.Add(argument);
}
// specify the parameters to pass into the script.
ps.AddParameters(argumentsList);
Collection<PSObject> pipelineObjects = ps.Invoke();
// print the resulting pipeline objects to the console.
foreach (var item in pipelineObjects)
{
Console.WriteLine(item.BaseObject.ToString());
}
Console.WriteLine(ps.HadErrors+" -"+ps.HistoryString);
}
Console.WriteLine("Commaned finished");
}
(the .DataAdded just write to console.writeline the new message)
But I hit this:
Launching powershell command: F:\Dev\AAA\scripts/start.ps1 F:\Dev\AAA\scenarios/BBB/config.json
System.Management.Automation.PSSecurityException: File F:\Dev\AAA\scripts\start.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
---> System.UnauthorizedAccessException: File F:\Dev\AAA\scripts\start.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https://go.microsoft.com/fwlink/?LinkID=135170.
--- End of inner exception stack trace ---
at System.Management.Automation.AuthorizationManager.ShouldRunInternal(CommandInfo commandInfo, CommandOrigin origin, PSHost host)
at System.Management.Automation.CommandDiscovery.ShouldRun(ExecutionContext context, PSHost host, CommandInfo commandInfo, CommandOrigin commandOrigin)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(CommandInfo commandInfo, CommandOrigin commandOrigin, Nullable`1 useLocalScope, SessionStateInternal sessionState)
at System.Management.Automation.CommandDiscovery.LookupCommandProcessor(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
at System.Management.Automation.ExecutionContext.CreateCommand(String command, Boolean dotSource)
at System.Management.Automation.PipelineOps.AddCommand(PipelineProcessor pipe, CommandParameterInternal[] commandElements, CommandBaseAst commandBaseAst, CommandRedirection[] redirections, ExecutionContext context)
at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
I've searched a lot, I've executed as admin this commands, both in x86 and x64 commands prompt:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
And if I execute this myself(copy paste in the command line) it works, it only fails when launching from visual studio.
Any idea what is going on?
I'm on windows 10 pro(latest updates) + VS2019(latest updates also). The console app that I'm doing is on .Net Core 3.1. I do run VS as my simple-normal user(as will the end program).