Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

VBS Script to validate RDP Connection

$
0
0
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.




Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>