3

In my C# application, I try to create a RunSpace to invoke some Powershell scripts. However when it reaches the code to actually create it, it fails with the error below:

var implementedHost = new implementedPSHost();
using (var rs = RunspaceFactory.CreateRunspace(customPSHost))
{

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.At line:1 char:3+ . .\Scripts\loadFunctions.ps1+

As suggested elsewhere, I ran a Powershell window and executed Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted but this did not help. The error still happens. Does anyone have any ideas how to fix this?

3
  • Is the C# application running in the security context of the same user? Commented Jun 4, 2019 at 13:49
  • @RohinSidharth how can I confirm that? Commented Jun 4, 2019 at 14:06
  • Can you share the complete code you're using, the error isn't truly generating on the using statement, but rather when you attempt to launch a script, we need to see the full body to see what you're doing. Commented Jun 4, 2019 at 16:05

1 Answer 1

3

Try running your Set-ExecutionPolicy -Scope CurrentUser step within the runspace, like this.

First, with my ExecutionPolicy for this user to Restricted, and I see the following error when i run this code:

RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

            using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                //scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");
                Pipeline pipeline = runspace.CreatePipeline();
                Command scriptCommand = new Command("C:\\temp\\test.ps1");
                Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();

                pipeline.Commands.Add(scriptCommand);
                Collection<PSObject> psObjects;
                psObjects = pipeline.Invoke();
            }


 RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
 scriptInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted");

Gives me

System.Management.Automation.PSSecurityException: 'File C:\temp\test.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.'

Next, I rerun it, uncommenting this line:

scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");

And, no errors! However, this will change the users ExecutionPolicy, which I think is bad behavior, so I'm going to instead wrap my function with this and set the Users ExecutionPolicy back the way I found it.

using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
            {
                runspace.Open();
                RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
                scriptInvoker.Invoke("$ExecutionPolicy = Get-ExecutionPolicy -Scope CurrentUser");
                scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted -Scope CurrentUser ");
                Pipeline pipeline = runspace.CreatePipeline();
                Command scriptCommand = new Command("C:\\temp\\test.ps1");
                Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();

                pipeline.Commands.Add(scriptCommand);
                Collection<PSObject> psObjects;
                psObjects = pipeline.Invoke();
                scriptInvoker.Invoke("Set-ExecutionPolicy -Scope CurrentUser $ExecutionPolicy -Force");
            }

Now our function works and we don't tamper with the user's system settings.

Sign up to request clarification or add additional context in comments.

10 Comments

where do I get RunspaceInvoke from? Visual Studio is complaining that it doesn't exist
Add a reference to System.Management.Automation.dll which will be found here C:\Program Files (x86)\Reference Assemblies\Microsoft\WindowsPowerShell\3.0\System.Management.Automation.dll
Yes I already have using System.Management.Automation as I was already using Runspace etc
RunspaceInvoke is a member of System.Management.dll, if you've added the reference, be sure to also add it to your usings at the top of your .cs file too :) using System.Management.Automation; using System.Management.Automation.Runspaces; learn.microsoft.com/en-us/dotnet/api/…
I did. I'm using version 6.2.1.. Maybe that's the problem?
|

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.