0

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?

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • There are some strange interactions between Group Policy and `-ExecutionPolicy Bypass`; you may want to try `-ExecutionPolicy Unrestricted -Scope Process` instead. – Jeff Zeitlin Mar 08 '18 at 14:00
  • Sadly @JeffZeitlin the same issue persists – MyDaftQuestions Mar 08 '18 at 14:02
  • Try running with elevated permission – sramalingam24 Mar 08 '18 at 14:09
  • @sramalingam24 do you mean adding `-verb runas` ? If so, same issue :( – MyDaftQuestions Mar 08 '18 at 14:23
  • @MyDaftQuestions Is your app `32bit` or `64bit`? Launch `Windows PowerShell (x86)` and check `ExecutionPolicy` is also fine there. – G42 Mar 08 '18 at 14:26
  • _. - but it has them and bypass_ Sory this part of your title is - for me - not easy to understand. – Clijsters Mar 08 '18 at 14:27
  • 1
    @gms0ulman that is interesting. Yes, when I run it from a 32big (86) then it is still restricted... The app runs with AnyCPU. But wouldn't `ExecutionPolicy Unrestricted -Scope Process` have solved this? – MyDaftQuestions Mar 08 '18 at 14:29
  • 1
    Execution policy is architecture specific. – EBGreen Mar 08 '18 at 14:42
  • So, I have to change the policy for the system then? @EBGreen If I make the policy for 32bit bypass, it works fine, but there are obvious security concerns, as per the comments on https://stackoverflow.com/a/9167524/1221410 – MyDaftQuestions Mar 08 '18 at 14:43
  • Your code is not setting the execution policy for the machine. It is simply running the script and ignoring whatever the execution policy happens to be. – EBGreen Mar 08 '18 at 14:59
  • Understood @EBGreen but why is it then if I do `set-executionpolicy -executionpolicy bypass` with the 32 bit powershell, my script runs fine... It's almost as if the script in my code (where I put -executionpoliy unrestricted -scope process) is ignored – MyDaftQuestions Mar 08 '18 at 15:30

0 Answers0