简体   繁体   English

为什么删除 powershell.exe -executionpolicy unrestricted 突然起作用但以前没有

[英]Why removing powershell.exe -executionpolicy unrestricted suddenly working but wasn't before

I've a batch file run.bat which calls a python script (same issue if it's a ps1 instead of py script)我有一个调用 python 脚本的批处理文件 run.bat(如果它是 ps1 而不是 py 脚本,则同样的问题)

Contents of run.bat run.bat 的内容

powershell.exe -executionpolicy unrestricted
powershell python .\aTest.py

This was working fine until today where batch file is not invoking the python script.直到今天批处理文件没有调用 python 脚本,这一直运行良好。 The command window shows the following message: "Try the new cross-platform PowerShell https://aka/ms/pscore6"命令窗口显示以下消息:“尝试新的跨平台 PowerShell https://aka/ms/pscore6”

I found from online that I can suppress this message with -nologon but that did not help other than removing the message.我从网上发现我可以使用 -nologon 禁止显示此消息,但除了删除该消息之外没有其他帮助。 I removed the following line powershell.exe -executionpolicy unrestricted and script worked.我删除了以下行 powershell.exe -executionpolicy unrestricted 并且脚本有效。 There was no user permissions change or anything made to the system between the last time it was successful and today.从上次成功到今天,没有用户权限更改或对系统进行任何更改。

Why this is happening is puzzling me and initially the -executionPolicy was added because without it, the script wasn't running.为什么会发生这种情况让我感到困惑,最初添加了 -executionPolicy 因为没有它,脚本就无法运行。 Now it's the opposite, how can I figure out why this happened?现在情况正好相反,我怎样才能弄清楚为什么会发生这种情况? What caused it?是什么原因造成的? Are there any difference having the extra PS flags and not if the user is a local admin group?如果用户是本地管理员组,是否有额外的 PS 标志有什么区别?

System is a Windows 10 and has one local admin user.系统是 Windows 10 并且有一个本地管理员用户。

powershell.exe -executionpolicy unrestricted

  • This enters an interactive PowerShell session that requires a user to interactively submit exit in order to exit the session, and it is only then that the batch file continues executing.这将进入一个交互式PowerShell 会话,该会话要求用户以交互方式提交exit以退出会话,并且只有这样批处理文件才能继续执行。

  • -executionpolicy unrestricted applies only to that session (process). -executionpolicy unrestricted适用于该会话(进程)。

  • Since neither the -File nor the -Command parameter are being used (the latter possibly implicitly , by passing command(s) only), PowerShell emits a "logo", ie a copyright message :由于既没有使用-File也没有使用-Command参数(后者可能是隐含的,仅通过传递命令),PowerShell 会发出一个“徽标”,即版权消息

     Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved.
    • Recent versions of Windows PowerShell append a message promoting the cross-platform, install-on-demand successor edition, PowerShell (Core) v6+ , to this message, so that you'll see the following:最新版本的 Windows PowerShell 会在此消息中附加一条消息,宣传跨平台按需安装后续版本PowerShell (Core) v6+ ,以便您看到以下内容:

       Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Try the new cross-platform PowerShell https://aka.ms/pscore6
    • Use -NoLogo to suppress this output;使用-NoLogo抑制此输出; however, as implied above, this is not necessary if you pass code to execute as part of the call, either by passing a script file path ( .ps1 ) to -File ( -f ), or by (possibly positionally) passing command(s) to -Command ( -c ).但是,如上所述,如果您通过将脚本文件路径 ( .ps1 ) 传递给-File ( -f ) 或通过(可能在位置上)传递 command( s) 到-Command ( -c )。 However, you do need -NoLogo with -File if you combine it with -NoExit .但是,如果将 -NoLogo 与 -File 与-NoExit结合使用-NoLogo -File确实需要它。

powershell python .\aTest.py

Generally speaking, there is no need to involve PowerShell in order to execute a Python script - directly invoking python .\aTest.py from a batch file should do.一般来说,执行 Python 脚本不需要涉及 PowerShell - 直接从批处理文件中调用python .\aTest.py应该可以。

Only if the call to the Python script relies on initializations performed via PowerShell's profiles (notably via the current user's $PROFILE file) would invocation via PowerShell be required.只有当对 Python 脚本的调用依赖于通过 PowerShell 的配置文件(特别是通过当前用户的$PROFILE文件) 执行的初始化时,才需要通过 PowerShell 进行调用。

  • As an aside: use the -NoProfile CLI option in case you want to suppress loading of any profile files, which is usually the right thing to do, so as to ensure a predictable execution environment and avoid unnecessary processing.顺便说一句:如果您想禁止加载任何配置文件,请使用-NoProfile CLI选项,这通常是正确的做法,以确保可预测的执行环境并避免不必要的处理。

If you do need to call via PowerShell, the effective execution policy does not apply to calling a Python script - it only applies to PowerShell scripts ( *.ps1 );如果确实需要通过 PowerShell 调用,则有效执行策略不适用于调用Python脚本 - 它仅适用于PowerShell脚本 ( *.ps1 ); if the profile files happen to call PowerShell scripts, use the following:如果配置文件碰巧调用了 PowerShell 脚本,请使用以下命令:

powershell.exe -ExecutionPolicy Bypass -Command python .\aTest.py

Note: Bypass bypasses all checks regarding execution of .ps1 scripts, whereas Restricted would still prompt before executing scripts downloaded from the web .注意: Bypass会绕过有关.ps1脚本执行的所有检查,而Restricted在执行从 web下载的脚本之前仍会提示

Note: Using the -Command ( -c ) parameter name explicitly isn't strictly necessary with powershell.exe , the Windows PowerShell CLI;注意:在powershell.exeWindows PowerShell CLI 中,明确使用-Command ( -c ) 参数名称不是必需的; however, pwsh.exe , the PowerShell (Core) 6+ CLI, now does require it.但是, PowerShell (Core) 6+ CLI pwsh.exe现在确实需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 PowerShell.exe -ExecutionPolicy 绕过 - 脚本中的 Header - PowerShell.exe -ExecutionPolicy Bypass - Header in Script Powershell 脚本在没有“powershell.exe -File”的情况下无法正常工作,并且在“powershell.exe -File”的情况下也无法正常工作 - Powershell script not working without "powershell.exe -File" and it's also not working properly with "powershell.exe -File" Powershell.exe没有输出 - Powershell.exe No Output 如何在启动powershell.exe之前设置首选项`$ ErrorView =“ CategoryView”` - How set preference `$ErrorView = “CategoryView”` before start powershell.exe Scapy为什么打开powershell.exe实例? - why does Scapy opens a powershell.exe instance? 为什么 PowerShell.exe 没有办法点源脚本? - Why PowerShell.exe There is no way to dot source a script? Powershell代码在Powershell.exe和Powershell ISE上运行良好,但在VS Code上无法运行 - Powershell Code is working well on Powershell.exe and Powershell ISE but not working on VS Code 比PowerShell.exe更好的PowerShell主机? - Better PowerShell hosts than powershell.exe? Powershell.exe和Powershell ISE之间的代码工作方式略有不同 - 排序对象行为 - Code working a bit differently between Powershell.exe and Powershell ISE - Sort-Object behavior Powershell.exe (v 2.0) 的路径 - Path to Powershell.exe (v 2.0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM