如何将ExecutionPolicy配置为;“远程签名”;在Powershell 4.0 api中

如何将ExecutionPolicy配置为;“远程签名”;在Powershell 4.0 api中,powershell,powershell-4.0,executionpolicy,Powershell,Powershell 4.0,Executionpolicy,我需要将ExecutionPolicy配置为初始会话状态下的“RemoteSigned”,或者在执行脚本之前的某种方式。我不想执行脚本来设置策略。这将改变客户端机器上的策略,这是我不想要的 在Powershell 5.0参考程序集中,您可以轻松地执行以下操作: var iss = InitialSessionState.CreateDefault(); iss.ExecutionPolicy = ExecutionPolicy.RemoteSigned 但是,如何在使用Powershell 4

我需要将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

我知道我们有几个脚本是这样开始的。

不,不,我根本不想运行Windows Powershell。在使用PowerShell api执行脚本之前,我想从C代码设置它。。查看上面的编辑如果您询问如何调用不可用的API,您可能已经回答了自己的问题。你不能。我一定不明白你的问题。此答案不会更改用户配置文件设置,它仅对调用PowerShell有效。我能否问一个问题,运行以下代码时会发生什么。使用(var powerShell=powerShell.Create(iss)){string script=“Set ExecutionPolicy Unrestricted”powerShell.AddScript(script);var putput=powerShell.Invoke();}