locked
VBS Script to validate RDP Connection RRS feed

  • Question

  • I am working on a VBS script to confirm if RDP Connection is working on remote servers. I know some server administrators check it via Telnet on default RDP port 3389. But, it is not completely reliable. I need to open an RDP Connection to a server, capture the result, if the connection is working or not, document the results on a text file and close the window just opened. My code follows below:
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("servers.txt", ForReading)
    Set oFSO = CreateObject("Scripting.FilesyStemObject")
    Outputfile="RDP.txt"
    Set ofile = ofso.createTextFile(OutputFile, True)
    ofile.writeline "computer" &vbtab& "Status"
    Dim arrFileLines(), computer, pingable
    i = 0
    Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
    Loop
    objFile.Close
    
    For Each computer in arrFileLines
            
     Select Case RDPTest(computer)
      Case "RDP is working"
       wscript.echo computer &""& " RDP is working"
       ofile.writeline computer &""& vbtab & " RDP is working"
      Case "RDP is NOT working"
       wscript.echo computer & " RDP is not working"
       ofile.writeline computer &""& vbtab & " RDP is NOT working"
      
     End Select
     
     'WScript.Echo strLine
    Next
    Private Function RDPTest(ByVal strComputer)
     Dim objShell, objExecObject, strText
     Set objShell = CreateObject("Wscript.Shell")
     Set wshShell = CreateObject("WScript.Shell") 
     
     Set objExecObject = wshShell.Exec("%comspec% /c mstsc /v:" & strComputer) 'Calls RDP Connection to the target server as per servers.txt list
     ret = wshShell.AppActivate(strComputer & " - Remote Desktop") 'Check out if the RDP window is opened for the target server
         If ret = True Then 
             RDPTest = "RDP is working"	'Confirms RDP is working
             wshShell.SendKeys "%{F4}"	'Close up the RDP window
        
     Else  
         RDPTest = "RDP is NOT working"	'Confirms RDP is not working
         wshShell.SendKeys "%{F4}"		'Close up the RDP-error window
     End If 
      
      Do While Not objExecObject.StdOut.AtEndOfStream
      strText = strText & objExecObject.StdOut.ReadLine()
     Loop
       
    End Function

    The part that calls a remote server and open RDP by the servername provided on the servers.txt list is working. The problem is with the part that confirms the RDP window opened and close it posting the results. I need help to fix it. As it is the code is not recognizing the RDP window just opened by the script, it is not closing the window and not reporting the connections as working. I tested it by opening RDP manually to a server, then ran the script, and the window I opened before running the script was recongnized by the script, but it was not closed.

    I thank in advance any help on this question.



    • Edited by Jess Lopes Wednesday, June 25, 2014 2:48 PM
    Wednesday, June 25, 2014 2:40 PM

Answers

  • Tis comes up all of the time. RDP sessions are disallowed from keyboard interaction in that way for security reasons.

    You can attempt to build a custom client using the RDP API.

    If automation of RDP connections were possible don't you think examples would be easy to find?


    ¯\_(ツ)_/¯

    • Marked as answer by Bill_Stewart Tuesday, July 22, 2014 6:23 PM
    Wednesday, June 25, 2014 11:01 PM

All replies

  • You cannot do it like that.

    To test RPP use PortQry and check the returned string.  Use PowerShell.

    foreach($computers in $computers){
        $results=PortQryV2 -n $computer -p tcp -e 3389
        # process results
    }

    That is all it takes.

    http://www.microsoft.com/en-us/download/details.aspx?id=17148


    ¯\_(ツ)_/¯

    Wednesday, June 25, 2014 2:55 PM
  • Wednesday, June 25, 2014 2:56 PM
  • Thanks for the answer, I appreciate it but this is not what I am looking for.
    Testing RDP default port 3389 it not reliable enough. Powershell is not an option for me too.
    I need to make sure the connection can be established not only by confirming if RDP port is listening, but also starting the connection.
    My script is half-way gone and I really want help to get it tottaly working.

    Does anyone have on light on how to fix my script?
    Thanks

    Wednesday, June 25, 2014 10:49 PM
  • Tis comes up all of the time. RDP sessions are disallowed from keyboard interaction in that way for security reasons.

    You can attempt to build a custom client using the RDP API.

    If automation of RDP connections were possible don't you think examples would be easy to find?


    ¯\_(ツ)_/¯

    • Marked as answer by Bill_Stewart Tuesday, July 22, 2014 6:23 PM
    Wednesday, June 25, 2014 11:01 PM