Many customers are not inclined to deviate from a policy of limited execution, because they really do not understand this. This is not a safety frontier - it's just an extra hoop to jump so you don't shoot in the leg. If you want to run ps1 scripts in your own application, just use your own working environment and use a basic authorization administrator who does not pay attention to the system execution policy:
InitialSessionState initial = InitialSessionState.CreateDefault(); // Replace PSAuthorizationManager with a null manager which ignores execution policy initial.AuthorizationManager = new System.Management.Automation.AuthorizationManager("MyShellId"); // Extract psm1 from resource, save locally // ... // load my extracted module with my commands initial.ImportPSModule(new[] { <path_to_psm1> }); // open runspace Runspace runspace = RunspaceFactory.CreateRunspace(initial); runspace.Open(); RunspaceInvoke invoker = new RunspaceInvoke(runspace); // execute a command from my module Collection<PSObject> results = invoker.Invoke("my-command"); // or run a ps1 script Collection<PSObject> results = invoker.Invoke("c:\temp\extracted\my.ps1");
Using the zero authorization manager, the execution policy is completed, ignored. Remember, this is not a βhack,β because the execution policy is something to protect users from themselves. This is not to protect against malicious third parties.
http://www.nivot.org/nivot2/post/2012/02/10/Bypassing-Restricted-Execution-Policy-in-Code-or-in-Script.aspx
source share