I'm trying to execute some powershell. It works fine from the command line or within Powershell.
My question has been partly answered in a few posts here and on SU. Such as .ps1 cannot be loaded because the execution of scripts is disabled on this system? and more importantly https://stackoverflow.com/a/9167524/1221410
The issue only occurs when I execute this from C# code
This it the code
var command = $"\"{_myPath}\" -ExecutionPolicy ByPass -imagePath \"{ _imagePath }\" -url \"{_fullUrl}\"";
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "powershell.exe";
psi.Arguments = command;
Process process = new Process();
process.StartInfo = psi;
process.Start();
And the powershell script I'm loading is simply
Param(
[string]$imagePath,
[string]$url
)
$chrome = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
start-process $chrome $url
The error message is
Execution of scripts is disabled on this system
If I do: get-executionpolicy
I can see it is set to bypass
If I change the following to
psi.Arguments = "start-process chrome.exe www.google.co.uk";
then it works fine. So it appears the issue is when I try to execute an actual script (as per the error message)
Why does this only not work from within my C# application?