从进程中设置执行策略
Set-Execution Policy from process
我正在为 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.Invoke();
}
有人能给我指出正确的方向吗?我知道这行不通,就好像我将机器策略设置为限制其他真正令人兴奋的事情不会发生,但如果我将其设置为不受限制,那么一切正常。
我刚刚创建了一个新的控制台项目并将其添加到 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();
}
即使没有 运行 作为管理员的代码,这对我来说也是完美的。我在 Windows 10 中使用 Visual Studio 2015 和 Powershell 5。
根据 the Powershell 4.0 Set-ExecutionPolicy and the Powershell 5.0 Set-ExecutionPolicy.
,Set-ExecutionPolicy 在 Powershell 4 和 5 中的工作方式相同
正在尝试使用反射来做到这一点。
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();
请注意:PowerShell 中有 5 个级别 (scope
) 的 ExecutionPolicy(请参阅 about_execution_policies)。这将设置 Process
的 ExecutionPolicy。这意味着如果 ExecutionPolicy 是使用组策略或本地策略( UserPolicy 或 MachinePolicy )定义的,这将不会覆盖 ExecutionPolicy。
检查 Get-ExecutionPolicy -List
以查看在当前进程的不同范围内定义的 ExecutionPolicies 列表。
我正在为 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.Invoke();
}
有人能给我指出正确的方向吗?我知道这行不通,就好像我将机器策略设置为限制其他真正令人兴奋的事情不会发生,但如果我将其设置为不受限制,那么一切正常。
我刚刚创建了一个新的控制台项目并将其添加到 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();
}
即使没有 运行 作为管理员的代码,这对我来说也是完美的。我在 Windows 10 中使用 Visual Studio 2015 和 Powershell 5。
根据 the Powershell 4.0 Set-ExecutionPolicy and the Powershell 5.0 Set-ExecutionPolicy.
,Set-ExecutionPolicy 在 Powershell 4 和 5 中的工作方式相同正在尝试使用反射来做到这一点。
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();
请注意:PowerShell 中有 5 个级别 (scope
) 的 ExecutionPolicy(请参阅 about_execution_policies)。这将设置 Process
的 ExecutionPolicy。这意味着如果 ExecutionPolicy 是使用组策略或本地策略( UserPolicy 或 MachinePolicy )定义的,这将不会覆盖 ExecutionPolicy。
检查 Get-ExecutionPolicy -List
以查看在当前进程的不同范围内定义的 ExecutionPolicies 列表。