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 .