Powershell Get-ExecutionPolicy返回不同的值 [英] Powershell Get-ExecutionPolicy returns different values
问题描述
根据我用来获取PowerShell中的执行策略设置的方法,我得到两个不同的值。
depending on the method I used to get the execution policy setting for Powershell, I get two different values.
如果我跑的Get- ExecutionPolicy
在PowerShell提示符处,我得到无限制。
If I run Get-ExecutionPolicy
in a Powershell prompt, I get 'Unrestricted'.
如果我使用下面的代码,我得到'限制'。
If I use the following code, I get 'Restricted'.
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;
}
}
再次我得到'限制'用下面的代码
Again, I get 'Restricted' with the following code:
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
有它说无限制。
I also checked in the registry here: HKEY_Local_Machine\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.Powershell\ExecutionPolicy
and there it says Unrestricted.
任何想法,为什么我得到不同的结果?是我的代码不正确吧?
Any idea why I get different result? Is my code incorrect perhaps?
推荐答案
您实现一个自定义的主机?如果是这样,则默认执行策略会受到限制,并需要该主机(下ShellIds)进行设置。
Are you implementing a custom host? If so, the default execution policy would be restricted and would need to be set for that host (under ShellIds).
无论哪种方式,你应该能够在代码中首先执行以下命令重写设置:
Either way, you should be able to execute this command first in your code to override the setting:
Set-ExecutionPolicy RemoteSigned -Scope process
这篇关于Powershell Get-ExecutionPolicy返回不同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!