I am not sure if I should post this one here or in StackOverflow.
I want a BAT that opens a RDP connection file and then, when the connection is closed, logoff the machine.
start /WAIT "ConnectionFile.rdp"
logoff
But the logoff happens instantaly, it doesnt wait for exit. I also try this using C# and Process.WaitforExit().
What should you do? I think is a issue with mstsc.exe when the parameter is a RDP file.
Update: I was testing and this works well in Windows 7, but in Windows 8 and in Windows 7 Thin PC it doesnt work.
Update Using powershell I advance a little. I my bat looks like this, launchin the process in Powershell it works.
powershell -version 2.0 -Sta -ExecutionPolicy UnRestricted Start-Process -Wait -FilePath mstsc -ArgumentList ConnectionFile.rdp; logoff
But if the RDP is a valid file when windows asks the credentials it fails and continue with next process. For demo purposes I changed the logoff for a calc.
Try this:
START /WAIT !_MSTSC! !_FILE! !_CONSOLE!
where
This hinges WAIT on MSTSC explicitly, not just the "successful open" of your RDP file. As such, it should (!) work a little better.
And... here's an interesting caveat from the START helptext:
So... what may be needed here is to wrap the MSTSC command and parameters into a CONNECT-REMOTE.CMD file, and wait for THAT to terminate.
For anyone finding this article and fighting with this issue, insert the following rule between the mstsc.exe command and the logoff:
When combining with this article: https://www.experts-exchange.com/articles/10032/MSTSC-as-a-Shell.html the mstsc.cmd contains: