1

我正在尝试使用 PsExec 远程执行脚本,并在我的 Tkinter 程序中使用 subprocess.Popen 返回实时输出。使用以下代码一切正常(无需使用 --windowed 参数编译我的应用程序)。

cmd = [Settings.psexec_loc, f"\\\\{target}", "-n", str(10), "-c", "-f", "-s", "-accepteula", "-nobanner", Settings.temp_cmd_loc]
process = subprocess.Popen(
    cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.STDOUT
)

while True:
    line = process.stdout.readline()
    if not line:
        break
    print(line.decode().rstrip())

当我使用 pyinstaller 将程序编译为 .exe 时,尝试使用 --windowed 或 --noconsole 选项删除 Tkinter 启动 cmd 控制台窗口时,不幸的是,事情开始崩溃。我只能读取 STDERR 和 STDOUT 的第一行,这取决于我使用的参数组合。

我已经为 subprocess.Popen 尝试了几个参数,包括 startup_info 和 creation_flags。它似乎不起作用。如果 stdin 设置为 subprocess.DEVNULL,并且我运行应用程序而不编译它,那么当我使用 stdin=None 的 --windowed 标志编译应用程序时,它会输出相同的内容。

cmd 脚本中的所有命令实际上都是由 PsExec 执行的。我msg * hello在脚本末尾对此进行了测试。它只是那个子进程只返回 STDOUT 的第一行。

我只想处理 STDOUT 和 STDERR,而忽略 STDIN。但我不希望在任何地方都能看到任何控制台窗口。谁能给我一些提示和技巧?提前致谢!

无论如何,我在空闲时间为我工作的公司创建了这个小程序。如果有人有兴趣并愿意查看源代码,这是 GitHub 存储库:https ://github.com/vlreinier/CmdDeployer/blob/main/Progression.py (第 235-290 行)

编辑:在运行时隐藏 CMD 窗口的临时修复是使用下面的函数,并在没有 --windowed 标志的情况下进行编译。但是,这不会在程序启动期间隐藏控制台窗口。

def cmd_visibility(show=True):
    hWnd = ctypes.WinDLL('kernel32').GetConsoleWindow()
    if hWnd:
        ctypes.WinDLL('user32').ShowWindow(hWnd, int(show))
4

0 回答 0