gpt4 book ai didi

powershell - 如何在Powershell 4.0 API中将ExecutionPolicy配置为 “RemoteSigned”

转载 作者:行者123 更新时间:2023-12-02 23:53:27 24 4
gpt4 key购买 nike

我需要在初始 session 状态或执行脚本之前以某种方式将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

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

关于powershell - 如何在Powershell 4.0 API中将ExecutionPolicy配置为 “RemoteSigned”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52063083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com