簡體   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