繁体   English   中英

如何在 Powershell 4.0 api 中将 ExecutionPolicy 配置为“RemoteSigned”

[英]How to configure ExecutionPolicy as "RemoteSigned" in Powershell 4.0 api

我需要在初始会话状态或在执行脚本之前以某种方式将 ExecutionPolicy 配置为“RemoteSigned”。 我不想执行脚本来设置策略。 这会改变我不想要的客户端机器上的策略。

在 Powershell 5.0 参考程序集中,您可以轻松完成,

var iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned

但是如何在保留 Powershell 4.0 作为引用程序集的同时实现相同的目标。

执行脚本的C#代码

var iss = InitialSessionState.CreateDefault();
iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned; --> this is OK for Powershell ReferenceAssemblies 5.0 but not 4.0

iss.ImportPSModule(new[] { typeof(Parameter).Assembly.Location });
using (var powerShell = PowerShell.Create(iss))
{
     var psScript = _inlineScript ?? File.ReadAllText(_path);
     powerShell.AddScript(psScript);
}

你的意思是什么时候启动 PowerShell? 如果可以,你可以这样做:

powershell -ExecutionPolicy RemoteSigned ... rest of parameters here

我知道我们有几个以这种方式开始的脚本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM