首页 文章

来自流程的设置执行策略

提问于
浏览
4

我正在为C#中的Outlook做VSTO添加,调用PowerShell脚本与Office 365的Exchange Online进行交互 .

它完全适用于我的Windows 10机器,具有机器级无限制的PowerShell执行策略 . 但是,我不能让它在客户端的Windows 7机器上运行 .

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

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.
}

有人能指出正确的方向吗?我知道这不起作用,好像我将机器策略设置为限制其他真正令人兴奋的东西不会发生,但如果我将其设置为不受限制,那么一切正常 .

2 回答

  • 2

    我刚刚创建了一个新的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();
        something.ToList().ForEach(c => Console.WriteLine(c.ToString()));
        Console.ReadLine();
    }
    

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

    根据the Powershell 4.0 Set-ExecutionPolicythe Powershell 5.0 Set-ExecutionPolicy,Set-ExecutionPolicy在Powershell 4和5中以相同的方式工作 .

  • 0

    你在Windows 7上拥有的powershell版本是什么?如果它不是V.4,你可以在那里得到它:https://support.microsoft.com/en-us/kb/2819745 . 然后,如果您使用的不是V.4,请使用不受限制的策略再次尝试 .

    另外,当其他真正令人兴奋的事情没有发生时,你有错误信息吗?如果是这样,请将其添加到您的问题中,以便我们可以找到更多关于您的问题的线索 .

相关问题