Running Powershell scripts - execution policy


I ran into something a little weird this morning and I was hoping someone could explain it to me.

I’m running Komodo IDE 10.1.1 on Windows 10 and I was trying to create a command in my toolbox to run a Powershell script. I’m able to run this script from the command line normally. However, when I ran the command in Komodo, I got an error from Powershell saying that it couldn’t run the script because of the execution policy.

This seems weird to me. I set the execution policy globally to “Unrestricted”. If my understanding is correct, that means that any user should be able to run any Powershell script. And I was working under the assumption that Komodo was running in the context of my user account anyway, so if I can run a script from the command-line, then Komodo should be able to run it too. But when I tried creating a Komodo command to print the execution policy (running powershell -NoProfile Get-ExecutionPolicy) and it printed out “Restricted”, which makes no sense to me.

I was able to fix the problem by creating a command to set the execution policy for the current scope (powershell -NoProfile Set-ExecutionPolicy Unrestricted -Scope CurrentUser), and that allowed me to run my original command as expected. But I’m really puzzled as to why that would even be necessary. Can anybody offer any insight into what’s going on here?

I think this might be a question for Stackoverflow, as I don’t believe any of us are powershell users.

Komodo just executes your command using the environment variables you provided, it would be no different from spawning a process in Python. So it seems the problem lies in the environment variables, sounds like powershell depends on something that perhaps defines the scope?

Google’d this a while ago and found this old blog. Maybe shed some light. Some good links in there as well:

Thanks for the replies, guys!

So after some additional investigation, it turns out that @nathanr was right - it is exactly like spawning a process from Python. By which I mean I get the same thing if I write a simply python script that spawns PowerShell via os.system(). So it’s definitely not a Komodo-specific thing.

I’m not sure exaclty why that would be the case. Looking at the output of Get-ExecutionPolicy -List, it seems that the LocalMachine scope is set to “Undefined” when I run Powershell from Python, which according to the documentation means the policy is set to the default value of “Restrict”. The odd part is that when I run the same command from the command line, the LocalMachine scope is set to the normal value. As I understand it, that should be stored in the registry under HKEY_LOCAL_MACHINE, so it’s not clear why it wouldn’t be able to read the policy. But that’s not a question for this forum.