我正在尝试从python这样启动PowerShell脚本:
psxmlgen = subprocess.Popen([r'C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe',
'./buildxml.ps1',
arg1, arg2, arg3], cwd=os.getcwd())
result = psxmlgen.wait()
问题是我得到以下错误:
File
C:\Users\sztomi\workspace\myproject\buildxml.ps1
cannot be loaded because the execution of scripts is disabled on this
system. Please see"get-help about_signing" for more details.
尽管事实上我很早以前确实在管理员运行的PS终端中键入Set-ExecutionPolicy Unrestriced来启用Powershell中的运行脚本(然后再做一次,只是为了确保)。 powershell可执行文件与开始菜单中的快捷方式所指向的可执行文件相同。 不管我是否以管理员身份运行PowerShell,Get-ExecutionPolicy都会正确报告Unrestricted。
如何从Python正确执行PS脚本?
首先,Set-ExecutionPolicy Unrestriced基于每个用户,并且基于每个位数(32位与64位不同)。
其次,您可以从命令行覆盖执行策略。
psxmlgen = subprocess.Popen([r'C:\WINDOWS\system