从流程设置执行策略 [英] Set-Execution Policy from process

查看:54
本文介绍了从流程设置执行策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 C# 为 Outlook 做一个 VSTO 插件,它调用 PowerShell 脚本与 Office 365 的 Exchange Online 进行交互.

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

这一切都在我的 Windows 10 机器上完美运行,并且具有机器级不受限制的 PowerShell 执行策略.但是,我无法让它在客户端的 Windows 7 机器上运行.

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.

我认为有两个问题.一个可能需要更新他的 Windows 7 PowerShell 才能使用我的代码,其次我没有正确设置流程执行策略.这是我将执行策略设置为不受限制的最大努力(绕过会更好吗?).

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.

推荐答案

我刚刚创建了一个新的 Console 项目并将其添加到 Main:

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();
}   

这对我来说非常有效,即使没有以管理员身份运行代码.我在带有 Powershell 5 的 Windows 10 中使用 Visual Studio 2015.

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 4.0 Set-ExecutionPolicyPowershell 5.0 Set-ExecutionPolicy.

这篇关于从流程设置执行策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆