I have a Powershell script being executed from a C# program. Both use a C# dll with static memory. When the Powershell script is executed, it has access to the same data that the C# program set. Additionally, anything the Powershell script writes to the dll is available the next time the script is called.
I want them to be completely separate so that the Powershell script runs in its own environment and memory space.
Here is my code:
using ( var _powerShell = PowerShell.Create() )
{
try
{
_powerShell.Runspace = null;
_powerShell.RunspacePool = null;
_powerShell.AddScript($"{scriptFile} {args}");
_powerShell.Invoke();
}
catch ( Exception ex )
{
Console.WriteLine(ex.ToString());
}
finally
{
_powerShell.Dispose();
}
}
I am guessing I need to create a new Powershell session or something? I'm stuck.
Process.Start.finallyblock is redundant_powerShell.Runspace = RunspaceFactory.CreateOutOfProcessRunspace(null);