Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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. enter image description here

share|improve this question
    
Very odd.. Removing my incorrect answer... Could you test with PowerShell on Windows 8? Start-Process -Wait -FilePath mstsc -ArgumentList ConnectionFile.rdp –  jscott Jan 30 '13 at 19:47
    
With Powershell works. I am creating a bat that launches Powershell. I'll comment soon. –  Ricardo Polo Jan 30 '13 at 19:53
    
With powershell works but not well when windows asks for credentials –  Ricardo Polo Jan 30 '13 at 20:39
add comment

1 Answer

Try this:

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

where

  • !_MSTSC! is the path to MSTSC.EXE
  • !_FILE! is a path to a saved RDP file, containing hostname, login and pwd, perhaps the name of a file to execute on connection, blah blah blah
  • !_CONSOLE! is set to either "" (null) or "/ADMIN" as needed

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:

    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.

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.

share|improve this answer
    
Thanks @George but It does not work. Look at this image with the result of testing it dl.dropbox.com/u/300838/error2.png –  Ricardo Polo Jan 30 '13 at 17:51
    
Interesting. The "verify the cert is OK" prompt is interrupting MSTSC, obviously, but didn't think that'd cause WAIT to trigger... do you get the same results if you check the checkbox "Don't Ask me again" and then retry? The only other way I can think of is to kludge a "flag" variable/file that would get set before running MSTSC, and then deleted by the remote session using \\tsclient\<path to flag file> ... and a test loop that would trigger your next step in your batch when the flag file no longer existed. –  George Erhard Jan 30 '13 at 18:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.