简体   繁体   中英

How to configure ExecutionPolicy as "RemoteSigned" in Powershell 4.0 api

I need to configure ExecutionPolicy as 'RemoteSigned' at initial session state or somehow before execution of script. I don't want to execute script to set policy. This would alter the policy at client machine which i don't want.

In Powershell 5.0 reference assemblies one can do easily,

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

but how can I achieve the same while remaining with Powershell 4.0 as referenced assemblies.

C# code to execute script

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);
}

Do you mean when you launch PowerShell? if so can you do this:

powershell -ExecutionPolicy RemoteSigned ... rest of parameters here

I know we have several scripts that start out this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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