使用 os.system 调用 PowerShell 时出错

Error when using os.system to call PowerShell

当我尝试从 Python 函数调用 powershell 时收到以下错误:

Cannot load PSReadline module. Console is running without PSReadline.

我有一个名为 ps_cli() 的函数,它是:os.system("powershell -NoProfile")

当我调用它时,我得到以下 behavior/error:

这之前在 Windows 7 和 8 台机器(我目前无法访问)上工作,从我读到的一些东西可能与 [= 上的 PowerShell 5 有关31=] 10台机器。我正在编写的工具背后的想法是,它可以在具有适当权限的帐户上与 Windows 的任何现代版本一起使用。由于我正在编写更高级的脚本,这似乎会导致几秒钟的延迟,我想了解如何在不执行的情况下摆脱它:

Open Powershell using the parameter -PSConsoleFile and a PSConsoleFile that you previously exported with Export-Console.

这是在某个论坛上建议的 post 我看到了。任何想法将不胜感激!

PSReadline-模块是 Powershell 5.0(在 Win10 上)中的新内置模块,可提供增强的控制台体验。因为它是内置的,所以 executionpolicy 不应该有问题(即使它由于某种原因没有签名),但检查一下也无妨。这行得通吗?

os.system("powershell -NoProfile -ExecutionPolicy ByPass")

您使用的是第三方控制台吗?该模块可能与 conhost.exe 以外的控制台不兼容(cmd 和 powershell 使用的 windows 中的默认值)。

一个肮脏的解决方法是简单地清除屏幕:

os.system("powershell -NoProfile -NoExit -Command cls")