michaelburns / LaunchPad

PowerShell Web Portal
MIT License
90 stars 13 forks source link

Problem with Powershell Executionpolicy #5

Closed ducke closed 8 years ago

ducke commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

Updated the code - can you test on Windows 10? https://github.com/michaelburns/LaunchPad/commit/d2666766fdfe8247b683465496f75992c8bfe84d

ducke commented 8 years ago

:+1:

michaelburns commented 8 years ago

Thanks @ducke - closing issue.