9

I'm doing a VSTO add in for Outlook in C# that calls PowerShell scripts to interact with the Exchange Online of Office 365.

It all works perfectly on my windows 10 machine with a machine level unrestricted PowerShell execution policy. However, I can't get this to run on the client's Windows 7 machine.

I think there are two issues. One that possibly his windows 7 PowerShell needs to be updated to work with my code, and second that I'm not properly setting the process execution policy. Here was my best effort to get the execution policy set to unrestricted (would bypass be better?).

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    StringBuilder OSScript = new StringBuilder("Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted;");
    OSScript.Append(@"other really exciting stuff");
    PowerShellInstance.AddScript(OSScript.ToString());
    PowerShellInstance.Invoke();
} 

Could someone point me the right direction? I know this doesn't work, as if I set the machine policy to restricted the other really exciting stuff doesn't happen, but if I set it to unrestricted then everything works.

Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
Pug
  • 103
  • 1
  • 1
  • 8
  • 1
    Are you running this as administrator? – Camilo Terevinto Dec 07 '15 at 18:25
  • Yes this is being run as an office 365 admin. The code executes fine on my Windows 10 PowerShell 5 unrestricted machine. I've also upgraded a windows 7 machine to PowerShell 5 and it runs the code fine in unrestricted as well. It seems that for some reason my "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted;" command isn't working on machines set to restricted. I was hoping the command would allow the process to run even on machines set to restricted. 44 – Pug Dec 07 '15 at 20:49
  • I looked at my code block, and I noticed that things were different then when I posted it. The invoke has been removed from the last line. Is this standard to delete the invoke? I'm just trying to understand the edits. Beyond the invoke, it seems that most of the edits were just cleanup on apparently poor English skills. – Pug Dec 08 '15 at 03:15
  • The removal of the Invoke call was probably a mistake that got wrongly approved. – Camilo Terevinto Dec 08 '15 at 03:31

2 Answers2

9

I just created a new Console project and added this to Main:

using (PowerShell PowerShellInstance = PowerShell.Create())
{
    string script = "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted; Get-ExecutionPolicy"; // the second command to know the ExecutionPolicy level
    PowerShellInstance.AddScript(script);
    var someResult = PowerShellInstance.Invoke();
    someResult.ToList().ForEach(c => Console.WriteLine(c.ToString()));
    Console.ReadLine();
}   

This works perfectly for me, even without running the code as administrator. I'm using Visual Studio 2015 in Windows 10 with Powershell 5.

Set-ExecutionPolicy works in the same way in Powershell 4 and 5, according to the Powershell 4.0 Set-ExecutionPolicy and the Powershell 5.0 Set-ExecutionPolicy.

Code Doggo
  • 2,146
  • 6
  • 33
  • 58
Camilo Terevinto
  • 31,141
  • 6
  • 88
  • 120
  • 2
    So I tested the code above. And it did in fact work properly. This was the push I needed thanks. I changed to bypass, and I think that what the issue was, is that I needed to do it in two steps. The first step toggled on my bypass and my second invoke call was able to successfully complete, but if I tried to call it all in a single script, all the other items would fail. Thanks for your time and help gentlemen. – Pug Dec 08 '15 at 03:08
0

Trying to do this using reflection.

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Reflection;
using Microsoft.PowerShell;
...
InitialSessionState iss = InitialSessionState.CreateDefault();
// Override ExecutionPolicy
PropertyInfo execPolProp = iss.GetType().GetProperty(@"ExecutionPolicy");
if (execPolProp != null && execPolProp.CanWrite)
{
    execPolProp.SetValue(iss, ExecutionPolicy.Bypass, null);
}
Runspace rs = RunspaceFactory.CreateRunspace(iss);
rs.Open();

Please note: There are 5 levels (scope) of ExecutionPolicy in PowerShell (see about_execution_policies). This will set Process's ExecutionPolicy. This means if that ExecutionPolicy was defined with Group policy or Local policy ( UserPolicy or MachinePolicy ), this will not override ExecutionPolicy.

Check Get-ExecutionPolicy -List to see list of ExecutionPolicies defined in different scopes for your current process.

filimonic
  • 3,988
  • 2
  • 19
  • 26