1

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).

3
  • 1
    What about sending the command to set the execution policy. Check this. Commented Dec 1, 2020 at 7:10
  • @user1672994 oh? It works! Seems weird security, that an user cannot execute script but can execute the commande to allow it? Anyway, could you add this as answer? Commented Dec 1, 2020 at 7:26
  • @JAN - Thanks, Adding as answer. Commented Dec 1, 2020 at 7:30

1 Answer 1

1

You can set the execution policy as command in power-shell object.

 powershell.AddCommand("Set-ExecutionPolicy").AddArgument("Unrestricted")
    .AddParameter("Scope","CurrentUser");
Sign up to request clarification or add additional context in comments.

1 Comment

Well, apparently in the other system we have, this isn't enough at least :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.