根据我用来获取 PowerShell 执行策略设置的方法,我得到了两个不同的值。
如果我在 PowerShell 提示符下运行 Get-ExecutionPolicy
,我会得到“Unrestricted”。
如果我使用以下代码,我会得到“受限”。
using (var runspace = RunspaceFactory.CreateRunspace())
{
runspace.Open();
var pipeline = runspace.CreatePipeline();
pipeline.Commands.AddScript("Get-ExecutionPolicy");
foreach (var result in pipeline.Invoke())
{
var restriction = ((ExecutionPolicy)result.BaseObject);
break;
}
}
再次,我使用以下代码获得“受限”:
using (var invoker = new RunspaceInvoke())
{
foreach (var result in invoker.Invoke("Get-ExecutionPolicy"))
{
var restriction = ((ExecutionPolicy)result.BaseObject);
break;
}
}
我还在此处检查了注册表:HKEY_Local_Machine\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.Powershell\ExecutionPolicy
上面写着 Unrestricted。
为什么我得到不同的结果?也许我的代码不正确?
最佳答案
您要实现自定义主机吗?如果是这样,默认执行策略将受到限制,需要为该主机设置(在 ShellIds 下)。
无论哪种方式,您应该能够首先在代码中执行此命令以覆盖设置:
Set-ExecutionPolicy RemoteSigned -Scope process
关于c# - PowerShell 的 Get-ExecutionPolicy 返回不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4339093/