在 Windows 8 中启动/WAIT RDP 文件

在 Windows 8 中启动/WAIT RDP 文件

我不确定是否应该在这里或 StackOverflow 上发布这个。

我想要一个 BAT,它可以打开 RDP 连接文件,然后在关闭连接时注销机器。

start /WAIT "ConnectionFile.rdp"
logoff

但注销是即时发生的,它不会等待退出。我也尝试使用 C# 和 Process.WaitforExit()。

你应该怎么做?我认为当参数是 RDP 文件时,mstsc.exe 存在问题。

更新:我已经测试过,它在 Windows 7 中运行良好,但在 Windows 8 和 Windows 7 Thin PC 中却不起作用。

更新使用 powershell 我进步了一点。我的 bat 看起来像这样,在 Powershell 中启动进程就可以了。

powershell -version 2.0 -Sta -ExecutionPolicy UnRestricted Start-Process -Wait -FilePath mstsc -ArgumentList ConnectionFile.rdp; logoff

但是,如果 RDP 是有效文件,当 Windows 询问凭证时,它会失败并继续下一个过程。出于演示目的,我更改了 calc 的注销。 在此处输入图片描述

答案1

尝试这个:

START /WAIT !_MSTSC! !_FILE! !_CONSOLE!

在哪里

  • !_MSTSC! 是 MSTSC.EXE 的路径
  • !_FILE! 是保存的 RDP 文件的路径,包含主机名、登录名和密码,也可能是连接时要执行的文件的名称,等等
  • !_CONSOLE! 根据需要设置为“”(空)或“/ADMIN”

这明确地将 WAIT 取决于 MSTSC,而不仅仅是 RDP 文件的“成功打开”。因此,它应该 (!) 工作得更好一些。

并且...这是来自 START 帮助文本的一个有趣的警告:

    If Command Extensions are enabled, external command invocation
    through the command line or the START command changes as follows:

    When executing an application that is a 32-bit GUI application, CMD.EXE
does not wait for the application to terminate before returning to
the command prompt.  This new behavior does NOT occur if executing
within a command script.

所以……这里可能需要将 MSTSC 命令和参数包装到 CONNECT-REMOTE.CMD 文件中,并等待 THAT 终止。

答案2

对于找到本文并正在努力解决此问题的人,请在 mstsc.exe 命令和注销之间插入以下规则:

powershell wait-process -name mstsc

与本文结合:https://www.experts-exchange.com/articles/10032/MSTSC-as-a-Shell.htmlmstsc.cmd 包含:

C:\Windows\System32\mstsc.exe RDPLocation
powershell wait-process -name mstsc
logoff

相关内容