How to get ExecutionPolicy with C #?

I used the following code to get the execution policy and get it successfully.

Code Block 1:

using (var powerShellInstance = PowerShell.Create())
{
     powerShellInstance.AddCommand("Get-ExecutionPolicy");
     Collection<PSObject> obj = powerShellInstance.Invoke();
}  

The problem is that I am changing the ExecutionPolicy from PowerShell, this does not affect C #.

But if I changed ExecutionPolicy from C #, then that was reflected. I used the following code to modify ExecutionPolicy.

Code 2:

using (var powerShellInstance = PowerShell.Create())
{
     powerShellInstance.AddCommand("Set-ExecutionPolicy").AddArgument("ByPass");
     powerShellInstance.Invoke();
}

Consider the following scenario, for example:

ExecutionPolicy # 1.
ExecutionPolicy PowerShell # 1, ExecutionPolicy .
ExecutionPolicy 2 # # 1, ExecutionPolicy .

+4
1

Set-ExecutionPolicy -Scope CurrentUser
Set-ExecutionPolicy -Scope LocalMachine
Set-ExecutionPolicy -Scope MachinePolicy
Set-ExecutionPolicy -Scope Process
Set-ExecutionPolicy -Scope UserPolicy

, Process (, )

+1

Source: https://habr.com/ru/post/1678384/


All Articles