powershell.exe -executionpolicy unrestricted
这将进入一个交互式PowerShell 会话,该会话需要用户以交互方式提交exit
才能退出会话,并且只有这样批处理文件才能继续执行。
-executionpolicy unrestricted
仅适用于该会话(进程)。
由于既没有使用 the-File
也没有使用-Command
参数(后者可能是隐含的,仅通过传递命令),PowerShell 发出一个“徽标”,即版权消息:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
最新版本的 Windows PowerShell会在此消息中附加一条消息,宣传跨平台按需安装后续版本PowerShell (Core) v6+,以便您看到以下内容:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
用于-NoLogo
抑制此输出;但是,如上所述,如果您通过将脚本文件路径 ( ) 传递到( ) 或(可能在位置上)将命令传递到( ) 来传递代码以作为调用的一部分执行,则这不是必需的。.ps1
-File
-f
-Command
-c
powershell python .\aTest.py
一般来说,执行 Python 脚本不需要涉及 PowerShell - 直接python .\aTest.py
从批处理文件中调用就可以了。
只有当对 Python 脚本的调用依赖于通过 PowerShell 的配置文件(特别是通过当前用户的文件)执行的初始化$PROFILE
时,才需要通过 PowerShell 进行调用。
- 顺便说一句:如果您想禁止加载任何配置文件,请使用
-NoProfile
CLI选项,这通常是正确的做法,以确保可预测的执行环境并避免不必要的处理。
如果确实需要通过 PowerShell 调用,则有效执行策略不适用于调用Python脚本 - 它仅适用于PowerShell脚本 ( );如果配置文件碰巧调用了 PowerShell 脚本,请使用以下命令:*.ps1
powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py
注意:Bypass
绕过有关脚本执行的所有.ps1
检查,而在执行从 web下载的脚本之前Restricted
仍会提示。
注意:在Windows PowerShell CLI中,明确使用-Command
( -c
) 参数名称不是绝对必要的;但是,PowerShell (Core) 6+ CLI 现在确实需要它。powershell.exe
pwsh.exe