VBScript for RDP with exception

Hi,

I looking for a vbscript for RDP which will work in this way:

1) double click on the script and it will ask for the hostname to connect and press ok.
2) once entered the hostname or IP it will check whether this hostname exist in this text file (this text file will be on the network with all the exceptional hostnames)
3) if it exists dont RDP with a msg
4) if does not exist connect using RDP.

Please help.
LVL 14
Dhiraj MuthaLevel DAsked:
Who is Participating?
 
RobSampsonCommented:
Oops, please change
Set objServers = objFSO.OpenTextFile(strHostName, ForReading, False)

to this
Set objServers = objFSO.OpenTextFile(strMaintenanceServers, ForReading, False)

Rob.
0
 
gwickertCommented:
Hmmm can I first of all ask why you want this? I assume you want to do this so that people can't access certain servers/workstations via RDP?

If you want to only allow RDP access for users to certain computers, and exclude them access on others, it would be best to:
-Make a 'Restricted RDP Group' in Active Directory (assuming your using AD here..)
-Create a 'Local Users and Groups' Group Policy Preference that adds the previously created AD group to the local 'Remote Desktop Users' group on all PCs that have this policy applied.
-Scope this GPO to the computers that you want the restricted users to access.
-Done.

Then whenever you want a user to be able to access the whitelist machines, simply add them to the 'Restricted RDP Group' in AD, then they will in turn be a member of the 'Remote Desktop Users' group on all of the whitelist PCs which have the above GPO scoped to them.  The users will not be able to access any computers/servers that do not have this group in their local 'Remote Desktop Users' group via RDP.

Does that sound a little something like what you want to achieve?
0
 
Dhiraj MuthaLevel DAuthor Commented:
I dont want them to restrict the users in login, I just want them to know that they are not suppose to perform the task (mentioned in the comment) on that server due to some maintainance happening on the server.

I miss typed it...

3) if it exists RDP with a msg.
4) if does not exist connect using RDP.

and the msg will be in the same text file... In the below format


HOSTNAME|COMMENT
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
gwickertCommented:
Hm OK...Well I'll step oiut of this one then, VBS is not one of my strong points, sorry I couldn't help.

Glen
0
 
RobSampsonCommented:
Hi, is something like this what you're after?

Regards,

Rob.
strMaintenanceServers = "\\server\share\ServersUnderMaintenance.txt"

strHostName = InputBox("Enter host name:", "Host name")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objServers = objFSO.OpenTextFile(strHostName, ForReading, False)
Set objMsgs = CreateObject("Scripting.Dictionary")
objMsgs.CompareMode = vbTextCompare
While Not objServers.AtEndOfStream
	strLine = Trim(objServers.ReadLine)
	If strLine <> "" Then
		If InStr(strLine, "|") > 0 Then
			strServer = Split(strLine, "|")(0)
			strMsg = Split(strLine, "|")(1)
			If objMsgs.Exists(strServer) = False Then objMsgs.Add strServer, strMsg
		End If
	End If
Wend
strServers = ";" & Replace(objServers.ReadAll, VbCrLf, ";") & ";"
objServers.Close

If objMsgs.Exists(strHostName) = True Then
	MsgBox "You cannot connect to " & strHostName & " at this time." & VbCrLf & objMsgs(strHostName)
Else
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "mstsc -v " & strHostName, 1, False
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
I am getting an error on Line 7 Char 1

Error: File not found.

I have a proper file under the the mentioed drive. The file is in this format.

Hostname|Comment
0
 
Dhiraj MuthaLevel DAuthor Commented:
Now getting an error on Line 20

Error: Input past end of file
0
 
RobSampsonCommented:
Sorry again....try this.

Rob.
strMaintenanceServers = "\\server\share\ServersUnderMaintenance.txt"

strHostName = InputBox("Enter host name:", "Host name")

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Set objServers = objFSO.OpenTextFile(strMaintenanceServers, ForReading, False)
Set objMsgs = CreateObject("Scripting.Dictionary")
objMsgs.CompareMode = vbTextCompare
While Not objServers.AtEndOfStream
	strLine = Trim(objServers.ReadLine)
	If strLine <> "" Then
		If InStr(strLine, "|") > 0 Then
			strServer = Split(strLine, "|")(0)
			strMsg = Split(strLine, "|")(1)
			If objMsgs.Exists(strServer) = False Then objMsgs.Add strServer, strMsg
		End If
	End If
Wend
objServers.Close

If objMsgs.Exists(strHostName) = True Then
	MsgBox "You cannot connect to " & strHostName & " at this time." & VbCrLf & objMsgs(strHostName)
Else
	Set objShell = CreateObject("WScript.Shell")
	objShell.Run "mstsc -v " & strHostName, 1, False
End If

Open in new window

0
 
Dhiraj MuthaLevel DAuthor Commented:
Rob you are Superb... as usual... a genius.
0
 
RobSampsonCommented:
Thanks.

Thanks for the grade.

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.