none
[Help] Logging off using terminal services RRS feed

  • Question

  • Hello,

    I'm trying to make a RDP checker tool (beginner C#-er, script kiddie before) and the most difficult part of it is automating the interaction with the remote computer. After various attempts and searches (to find a way to logoff the remote machine) I found that this could be done in two ways.

    The first one would be sending all the keys to do it in a "user fashion" (Win+R, then type shutdown.exe /l /f). For some reason this works:

    System.Windows.Forms.SendKeys.Send("^+{ENTER}");

    but anything else just won't, and I do rdp.Focus() each time.

    I also tried to use the SendKeys method from here : https://msdn.microsoft.com/en-us/library/aa381294(v=vs.85).aspx and tried something like this https://social.msdn.microsoft.com/Forums/en-US/dce2cfc3-6952-459b-8edf-04749f7eab05/mstsclibimsrdpclientnonscriptablesendkeys-pains?forum=netfxnetcom

    But I was not able to make it and sense of it. 

    I bailed out of this dark hole of trial and error and tried something new. I found that some people used a feature of the library to start a remote application on the machine:

    https://msdn.microsoft.com/en-us/library/aa382952(VS.85).aspx

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/76fff61c-a224-40bc-a108-2d3f2f616cd7/microsoft-terminal-services-library-programmatically-execute-a-batch-file-program-on-a-remote?forum=csharpgeneral

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/2a6c79cc-3890-4899-850c-a938320a4b6e/remoteapp-and-mstsclib?forum=winserverTS

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/1b3f1f70-9eea-45d8-a6c4-5c5a71e6fa1a/launch-ts-remote-app-from-terminal-services-api?forum=winserverTS

    Still i could not get this to work. Some references: 

    ...
    this.rdp = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
    ...
    this.rdp.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("rdp.OcxState")));
    ...
    private AxMSTSCLib.AxMsRdpClient9NotSafeForScripting rdp;
    ...
    ...
                rdp.Name = "RDP";
                rdp.Enabled = true;
                rdp.AdvancedSettings.allowBackgroundInput = 1;
                rdp.AdvancedSettings2.DisplayConnectionBar = true;
                rdp.AdvancedSettings2.SmartSizing = true;
                rdp.AdvancedSettings2.RDPPort = 3389;
                rdp.TransportSettings.GatewayUsageMethod = 2;
                rdp.TransportSettings.GatewayCredsSource = 0;
                rdp.TransportSettings.GatewayProfileUsageMethod = 1;
                rdp.TransportSettings.GatewayHostname = gateway;
                rdp.TransportSettings2.GatewayUsername = username;
                rdp.TransportSettings2.GatewayPassword = password;
                rdp.DesktopHeight = 768;
                rdp.DesktopWidth = 1366;
                rdp.Server = server;
                rdp.UserName = username;
                MSTSCLib.IMsTscNonScriptable secured = (MSTSCLib.IMsTscNonScriptable)rdp.GetOcx();
                secured.ClearTextPassword = password;
                rdp.ConnectingText = "Connecting...";
                rdp.DisconnectedText = "Disconnected";
                var ocx = (MSTSCLib.IMsRdpClientNonScriptable5)rdp.GetOcx();
    
                ocx.EnableCredSspSupport = true;
                ocx.AllowCredentialSaving = false;
                ocx.PromptForCredentials = false;
                ocx.PromptForCredsOnClient = false;
                ocx.WarnAboutSendingCredentials = false;
                ocx.AllowPromptingForCredentials = false;
                ocx.MarkRdpSettingsSecure = true;
                
                //rdp.AdvancedSettings7.DisableRdpdr = 0;
                //rdp.CreateVirtualChannels("CH001,CH002");
    
                var settings = (MSTSCLib.IMsRdpClientAdvancedSettings8)rdp.AdvancedSettings;
                settings.allowBackgroundInput = 1;
                settings.ClientProtocolSpec = MSTSCLib.ClientSpec.FullMode;
                settings.ConnectToServerConsole = true;
                settings.EnableCredSspSupport = true;
                settings.EncryptionEnabled = 1;
    
                rdp.Connect();
    ...
    

    Hopefully some can make some sense of this :/

    Thanks a lot in advance if you managed to get to reading so far !

    Monday, October 16, 2017 8:04 AM

All replies

  • Hello CosminF,

    Do you mean you want to automatically close a session or start a rdp session? The reference code is the code that you used to connect a remote computer? Have you tried the following windows api yet: https://msdn.microsoft.com/en-us/library/aa383830.aspx

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 17, 2017 9:58 AM
  • Hy Barry,

    Thank you for the answer, i've been looking at this for a while but i can't seem to connect to the remote computer with that api since it doesn't have any gateway options. I might be wrong but this appears to only work with direct connections.

    Does this sound right or should I keep trying?

    All the best,

    Cosmin

    Thursday, October 19, 2017 12:08 PM
  • @Cosmin,

    The usage of the API I mentioned is not for connection, it is used for log off when a user already logged on a remote machine by the specific session. Actually may I be able to know why you want to automating the interaction with the remote computer? To be honest I don't think this is a good requirement.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 20, 2017 6:40 AM
  • @Barry,

    I need a way to verify the connection to a remote windows cluster as close to what a normal user would experience.
    The windows machines are inaccessible via powershell remoting and have no internet access, the only way in is RDP through a windows gateway. I'm open to any other way to check this feature if you have any better options? :)

    Friday, October 20, 2017 8:25 AM
  • @CosminF,

    It seems it really confused us about what you have done. We can see you shared a code snippet which is using rdp to connect. And actually if you are using this code, just use rdp.disconnect() you will disconnect from remote side. 

    So can we ask some questions:

    1. What's the code that you used to connect to remote machine. The code you provided?

    2. What's the error you get when you use your code. And error messages or exception info?

    3. How you write the code when you use windows api?

    If you can provide us more details maybe we can understand your case better. As for now since we have nothing we are not able to test/reproduce this issue.

    Best regards,

    Barry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 24, 2017 3:07 AM