VB Script to check on Remote Desktop

Greetings,
I have a list of IP addresses and i would like to check if any of those IPs have the remote desktop service enabled. Currently i'm doing the check manually by inserting one by one into the remote desktop connection windows. could anyone help me to Script this process.

Thanks
HAKSAAsked:
Who is Participating?
 
jostranderConnect With a Mentor Commented:
Otherwise, if you're not opposed to using a free MS tool in addition to the script...

Download:
http://www.microsoft.com/downloads/details.aspx?FamilyID=89811747-C74B-4638-A2D5-AC828BDC6983&displaylang=en

Save/Extract PortQry.exe into:
c:\PortQryV2\PortQry.exe


ON ERROR RESUME NEXT
'Joe Ostrander
'03.31.2010
'Requires Free MS Tool:  PortQry
'http://www.microsoft.com/downloads/details.aspx?FamilyID=89811747-C74B-4638-A2D5-AC828BDC6983&displaylang=en


'Force CSCRIPT
Set WshShell=CreateObject("Wscript.Shell")
If instr(lcase(wscript.fullname),"wscript") then
	wshshell.run "cmd /k cscript //nologo " & chr(34) & wscript.scriptfullname & Chr(34),1,false
	wscript.quit
End If

Dim strPortQryEXE
Dim strFileName
Dim strPort
Dim strDelimiter

'------------------------------------
'	User Variables
'------------------------------------
strWindowTitle="Port Check"
strPortQryEXE="c:\PortQryV2\PortQry.exe"
strFilename="c:\servers.txt"
strLogFileName="c:\ports.txt"
strPort = 3389
strDelimiter = vbTab
'------------------------------------

Set fso=CreateObject("Scripting.FileSystemObject")

'Verify server list file exists
If NOT fso.FileExists(strFilename) then
	msgbox "Could not find file:  " & strFilename,vbExclamation,strWindowTitle
	wscript.quit
End If

'Verify portqry exists
If NOT fso.FileExists(strPortQryEXE) then
	msgbox "Could not find file:  " & strPortQryEXE,vbExclamation,strWindowTitle
	wscript.quit
End If

'create the logfile
Set LogFile=fso.OpenTextFile(strLogFileName,2,true)

'Read the server file
Set oFile=fso.OpenTextFile(strFilename,1)
text = oFile.ReadAll
oFile.Close

arrComputers = split(text,vbCrLf)

For Each strComputer in arrComputers
	
	CheckPort strComputer,strPort,"TCP"

Next


'Close the logfile
LogFile.close


Sub CheckPort(myHost,myPort,myProtocol)
	ON ERROR RESUME NEXT

	strCMD=Chr(34) & strPortQryEXE & Chr(34) & " -n " & myHost & " -e " & myPort & " -p " & myProtocol
	
	Set oExec=WshShell.Exec(strCMD)
	
	status="UNKNOWN"
	resolvedTo=""
	
	Do While Not oExec.StdOut.AtEndOfStream
		count=count+1
		line=oExec.StdOut.ReadLine
		
		posA=instr(line,"IP address resolved to")
		If posA>0 then resolvedTo=mid(line,posA+1)
		
		posB=instr(line,"Name resolved to")
		If posB>0 then resolvedTo=mid(line,posB+1)		
		
		If instr(line,"NOT LISTENING") then
			status="DISABLED"
		End If
		
		If instr(line,": LISTENING") then
			status="ENABLED"
		End If

	Loop

	wscript.echo myHost & strDelimiter & myPort & strDelimiter & myProtocol & strDelimiter & status
	LogFile.WriteLine myHost & strDelimiter & myPort & strDelimiter & myProtocol & strDelimiter & status

End Sub

Open in new window

0
 
rajvjaCommented:
0
 
Malli BoppeCommented:
telnet servername 3389  You should get a black screen.You can test that for each and every server.
0
Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

 
HAKSAAuthor Commented:
Thanks mboppe for the advice. How can i incorporate your idea into a VBS where i provide a list of IP addresses and i get back a list of the IPs along with thier RDC status weather enable or not.
0
 
Malli BoppeCommented:
I don't know if its can be scripted.This what i tried but just stops at the 1st server.Servers.txt should have a list of serversnames

for /f %%a in   (c:\servers.txt) do (

telnet %%a 3389


)
0
 
jostranderCommented:
Do you have admin privileges on all the hosts?  If so, read on...

You could use a script to check Win32_TerminalServiceSetting.  This method doesn't seem to work too well on Win7/Vista though.

Another method is to check the registry on the hosts.  Here's a script that I made for checking the registry on hosts specified in C:\servers.txt.  Tested on Win2K,XP,Vista and Win7.
ON ERROR RESUME NEXT

'Force CSCRIPT
Set WshShell=CreateObject("Wscript.Shell")
If instr(lcase(wscript.fullname),"wscript") then
	wshshell.run "cmd /k cscript //nologo " & chr(34) & wscript.scriptfullname & Chr(34),1,false
	wscript.quit
End If


Const HKEY_LOCAL_MACHINE 	= &H80000002

strFilename="c:\servers.txt"

Set fso=CreateObject("Scripting.FileSystemObject")

'Verify server list file exists
If NOT fso.FileExists(strFilename) then
	msgbox "Could not find file:  " & strFilename,vbExclamation,"Remote Desktop Check"
	wscript.quit
End If

'Read the server file
Set oFile=fso.OpenTextFile(strFilename,1)
text = oFile.ReadAll
oFile.Close

arrComputers = split(text,vbCrLf)

For Each strComputer in arrComputers
	
	status=""

	status=GetVal(strComputer,"fDenyTSConnections")
	
	If status="UNKNOWN" then
		status=GetVal(strComputer,"TSEnabled")
	End If
	
	wscript.echo strComputer & "=" & status

Next


Function GetVal(strComputer,strValueName)
	ON ERROR RESUME NEXT

	strKeyPath = "SYSTEM\CurrentControlSet\Control\Terminal Server"
	
	Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" &_ 
	strComputer & "\root\default:StdRegProv")

	If err.number <> 0 then 
		strValue=err.number
		err.clear
	Else
		oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
		If err.number <> 0 then 
			strValue=err.number
			err.clear
		End If
	End If
	
	IF strValueName="fDenyTSConnections" then
		If strValue="0" then
			strValue="1"
		ElseIf strValue="1" then 
			strValue="0"
		End If
	End If
	
	
	Select Case strValue
		Case "1"
			GetVal="Enabled"
		Case "0"
			GetVal="Disabled"
		Case "462"
			GetVal="UNAVAILABLE"
		Case "-2147217405"
			GetVal="ACCESS DENIED"
		Case Else
			GetVal="UNKNOWN"
	End Select
	
End Function

Open in new window

0
 
HAKSAAuthor Commented:
This is what i was looking for. Thanks
0
All Courses

From novice to tech pro — start learning today.