为什么删除 powershell.exe -executionpolicy unrestricted 突然工作但不是以前
Why removing powershell.exe -executionpolicy unrestricted suddenly working but wasn't before
我有一个批处理文件 run.bat,它调用 python 脚本(如果它是 ps1 而不是 py 脚本,也会出现同样的问题)
run.bat
的内容
powershell.exe -executionpolicy unrestricted
powershell python .\aTest.py
直到今天批处理文件没有调用 python 脚本时,它一直运行良好。
命令 window 显示以下消息:
“尝试新的跨平台 PowerShell https://aka/ms/pscore6”
我从网上发现我可以使用 -nologon 来抑制这条消息,但除了删除消息之外没有帮助。
我删除了以下行 powershell.exe -executionpolicy unrestricted 并且脚本有效。
从上次成功到今天,没有用户权限更改或对系统进行任何操作。
为什么会发生这种情况让我感到困惑,最初添加了 -executionPolicy,因为没有它,脚本就不会 运行。
现在恰恰相反,我怎么能弄清楚为什么会这样呢?是什么原因造成的?
如果用户是本地管理员组,那么有额外的 PS 标志和没有区别吗?
系统是 Windows10 并且有一个本地管理员用户。
powershell.exe -executionpolicy unrestricted
这将进入一个 交互式 PowerShell 会话,该会话需要用户以交互方式提交 exit
才能退出会话,并且它只是 然后批处理文件继续执行。
-executionpolicy unrestricted
仅应用于那个会话(进程)。
因为既没有使用 -File
也没有使用 -Command
参数(后者可能 隐含地 ,通过传递命令(s )),PowerShell 会发出一个“徽标”,即 版权信息:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
WindowsPowerShell 的最新版本附加一条消息,推广跨平台、按需安装的后继版本,PowerShell (Core) v6+, 到此消息,以便您将看到以下内容:
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
Try the new cross-platform PowerShell https://aka.ms/pscore6
使用-NoLogo
抑制这个输出;但是,如上所述,如果您将 code 作为调用的一部分传递给 execute,那么这 不是 必需的,或者通过传递脚本文件路径 ( .ps1
) 到 -File
(-f
),或者通过(可能按位置)将命令传递给 -Command
(-c
)。但是, 需要 -NoLogo
和 -File
如果你将它与 -NoExit
.
powershell python .\aTest.py
一般来说,不需要 PowerShell 来执行 Python 脚本 - 从批处理文件直接调用 python .\aTest.py
应该可以.
仅当对 Python 脚本的调用依赖于 通过 PowerShell 的 profiles 执行的初始化(特别是通过当前用户的 $PROFILE
文件) 将需要通过 PowerShell 调用。
- 顺便说一句:如果您想抑制加载任何配置文件,请使用
-NoProfile
CLI选项,即通常做正确的事,这样才能确保可预测的执行环境,避免不必要的处理。
如果您确实需要通过 PowerShell 调用,有效的 execution policy not 适用于调用 Python 脚本 - 它仅适用于 PowerShell 脚本 (*.ps1
);如果 配置文件 碰巧调用 PowerShell 脚本,请使用以下内容:
powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py
注意:Bypass
绕过所有关于执行.ps1
脚本的检查,而Restricted
仍然在执行从网络下载的脚本之前提示。
注意:使用 -Command
(-c
) 参数名称对于 powershell.exe
来说并不是绝对必要的,Windows PowerShell命令行界面;但是,pwsh.exe
, the PowerShell (Core) 6+ CLI 现在确实需要它。
我有一个批处理文件 run.bat,它调用 python 脚本(如果它是 ps1 而不是 py 脚本,也会出现同样的问题)
run.bat
的内容powershell.exe -executionpolicy unrestricted
powershell python .\aTest.py
直到今天批处理文件没有调用 python 脚本时,它一直运行良好。 命令 window 显示以下消息: “尝试新的跨平台 PowerShell https://aka/ms/pscore6”
我从网上发现我可以使用 -nologon 来抑制这条消息,但除了删除消息之外没有帮助。 我删除了以下行 powershell.exe -executionpolicy unrestricted 并且脚本有效。 从上次成功到今天,没有用户权限更改或对系统进行任何操作。
为什么会发生这种情况让我感到困惑,最初添加了 -executionPolicy,因为没有它,脚本就不会 运行。 现在恰恰相反,我怎么能弄清楚为什么会这样呢?是什么原因造成的? 如果用户是本地管理员组,那么有额外的 PS 标志和没有区别吗?
系统是 Windows10 并且有一个本地管理员用户。
powershell.exe -executionpolicy unrestricted
这将进入一个 交互式 PowerShell 会话,该会话需要用户以交互方式提交
exit
才能退出会话,并且它只是 然后批处理文件继续执行。-executionpolicy unrestricted
仅应用于那个会话(进程)。因为既没有使用
-File
也没有使用-Command
参数(后者可能 隐含地 ,通过传递命令(s )),PowerShell 会发出一个“徽标”,即 版权信息:Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.
WindowsPowerShell 的最新版本附加一条消息,推广跨平台、按需安装的后继版本,PowerShell (Core) v6+, 到此消息,以便您将看到以下内容:
Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6
使用
-NoLogo
抑制这个输出;但是,如上所述,如果您将 code 作为调用的一部分传递给 execute,那么这 不是 必需的,或者通过传递脚本文件路径 (.ps1
) 到-File
(-f
),或者通过(可能按位置)将命令传递给-Command
(-c
)。但是, 需要-NoLogo
和-File
如果你将它与-NoExit
.
powershell python .\aTest.py
一般来说,不需要 PowerShell 来执行 Python 脚本 - 从批处理文件直接调用 python .\aTest.py
应该可以.
仅当对 Python 脚本的调用依赖于 通过 PowerShell 的 profiles 执行的初始化(特别是通过当前用户的 $PROFILE
文件) 将需要通过 PowerShell 调用。
- 顺便说一句:如果您想抑制加载任何配置文件,请使用
-NoProfile
CLI选项,即通常做正确的事,这样才能确保可预测的执行环境,避免不必要的处理。
如果您确实需要通过 PowerShell 调用,有效的 execution policy not 适用于调用 Python 脚本 - 它仅适用于 PowerShell 脚本 (*.ps1
);如果 配置文件 碰巧调用 PowerShell 脚本,请使用以下内容:
powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py
注意:Bypass
绕过所有关于执行.ps1
脚本的检查,而Restricted
仍然在执行从网络下载的脚本之前提示。
注意:使用 -Command
(-c
) 参数名称对于 powershell.exe
来说并不是绝对必要的,Windows PowerShell命令行界面;但是,pwsh.exe
, the PowerShell (Core) 6+ CLI 现在确实需要它。