简体   繁体   English

从流程设置执行策略

[英]Set-Execution Policy from process

I'm doing a VSTO add in for Outlook in C# that calls PowerShell scripts to interact with the Exchange Online of Office 365.我正在用 C# 为 Outlook 做一个 VSTO 插件,它调用 PowerShell 脚本与 Office 365 的 Exchange Online 进行交互。

It all works perfectly on my windows 10 machine with a machine level unrestricted PowerShell execution policy.这一切都在我的 Windows 10 机器上完美运行,具有机器级不受限制的 PowerShell 执行策略。 However, I can't get this to run on the client's Windows 7 machine.但是,我无法让它在客户端的 Windows 7 机器上运行。

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.一个可能需要更新他的 Windows 7 PowerShell 才能使用我的代码,其次我没有正确设置流程执行策略。 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.我知道这行不通,好像我将机器策略设置为限制其他真正令人兴奋的事情不会发生,但是如果我将其设置为不受限制,那么一切正常。

I just created a new Console project and added this to Main:我刚刚创建了一个新的 Console 项目并将其添加到 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.我在带有 Powershell 5 的 Windows 10 中使用 Visual Studio 2015。

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 .根据Powershell 4.0 Set-ExecutionPolicyPowershell 5.0 Set-ExecutionPolicy,Set-ExecutionPolicy在 Powershell 4 和 5 中的工作方式相同。

what is the version of powershell that you have on your Windows 7 ? 你在Windows 7上拥有的powershell版本是什么? If its not V.4, you can get it there: https://support.microsoft.com/en-us/kb/2819745 . 如果它不是V.4,你可以在那里得到它: https//support.microsoft.com/en-us/kb/2819745 Then if that was not V.4 that you where using, try again using unrestricted Policy. 然后,如果您使用的不是V.4,请使用不受限制的策略再次尝试。

Also, do you have an error message when the other really exciting stuff doesn't happen? 另外,当其他真正令人兴奋的事情没有发生时,你有错误信息吗? If so add it in you question so we can found more clues as to what is your problem. 如果是这样,请将其添加到您的问题中,以便我们找到更多关于您的问题的线索。

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 ).请注意:PowerShell 中有 5 个级别( scope )的 ExecutionPolicy(请参阅about_execution_policies )。 This will set Process 's ExecutionPolicy.这将设置Process的 ExecutionPolicy。 This means if that ExecutionPolicy was defined with Group policy or Local policy ( UserPolicy or MachinePolicy ), this will not override ExecutionPolicy.这意味着如果 ExecutionPolicy 是使用组策略或本地策略( UserPolicy 或 MachinePolicy )定义的,则不会覆盖 ExecutionPolicy。

Check Get-ExecutionPolicy -List to see list of ExecutionPolicies defined in different scopes for your current process.检查Get-ExecutionPolicy -List以查看在当前进程的不同范围中定义的 ExecutionPolicies 列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM