Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with Powershell Executionpolicy #5

Closed
ducke opened this issue Nov 30, 2015 · 4 comments
Closed

Problem with Powershell Executionpolicy #5

ducke opened this issue Nov 30, 2015 · 4 comments
Labels

Comments

@ducke
Copy link

ducke commented Nov 30, 2015

I have a problem with the execution policy on my Windows 10 machine.

This is my Testcode:

image

It fails with this error:

System.Management.Automation.PSSecurityException was unhandled by user code
  HResult=-2146233087
  Message=File C:\Alchemy\Scripts\TestMe.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
  Source=System.Management.Automation
  WasThrownFromThrowStatement=false
  StackTrace:
       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.CommandFactory._CreateCommand(String commandName, CommandOrigin commandOrigin, Nullable`1 useLocalScope)
       at System.Management.Automation.Runspaces.Command.CreateCommandProcessor(ExecutionContext executionContext, CommandFactory commandFactory, Boolean addToHistory, CommandOrigin origin)
       at System.Management.Automation.Runspaces.LocalPipeline.CreatePipelineProcessor()
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeHelper()
       at System.Management.Automation.Runspaces.LocalPipeline.InvokeThreadProc()
  InnerException: 
       HResult=-2147024891
       Message=File C:\Alchemy\Scripts\TestMe.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
       InnerException: 

My executionpolicy:
image

I'm running your project from VS Professional 2015 with IIS Express.

I found a solution for this problem in this blog http://www.nivot.org/blog/post/2012/02/10/Bypassing-Restricted-Execution-Policy-in-Code-or-in-Script

            // Executionpolicy Bypass
            // 
            InitialSessionState initial = InitialSessionState.CreateDefault();

            // Replace PSAuthorizationManager with a null manager
            // which ignores execution policy 
            initial.AuthorizationManager = new
                  System.Management.Automation.AuthorizationManager("Microsoft.PowerShell");

            //Create Runspace
            Runspace runspace = RunspaceFactory.CreateRunspace(initial);
@michaelburns
Copy link
Owner

Nice find, testing now. Also - $Test param should be [string]$Test. Right now only strings can be parameters but will need to expand this.

@michaelburns
Copy link
Owner

Updated the code - can you test on Windows 10? d266676

@ducke
Copy link
Author

ducke commented Dec 1, 2015

👍

@michaelburns
Copy link
Owner

Thanks @ducke - closing issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants