Submitted By: Martijn Haverhoek

Enables Terminal Services on a group of computers. The file Computers.txt should contain the names of the computers on which to enable remote desktop connections.

Visual Basic
Edit|Remove
On Error Resume Next

Const ENABLE_CONNECTIONS = 1
Const FOR_WRITING = 2
Const FOR_READING = 1

Set objFso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")

Set oList = objFso.OpenTextFile("computers.txt",FOR_READING)
Do While Not oList.AtEndOfStream
strComputer = oList.ReadLine

If CheckIfClientExists(strComputer) = True Then
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_TerminalServiceSetting")

For Each objItem in colItems
    errResult = objItem.SetAllowTSConnections(ENABLE_CONNECTIONS)
Next
End If
Loop


Function CheckIfClientExists(strComputer)
Const TEMPFOLDER = 2

Dim oTS, oFile
Dim sTempFile, sTempFolder, sReturn

CheckIfClientExists = False
Set sTempFolder = objFso.GetSpecialFolder(TEMPFOLDER)
sTempFile = objFso.GetTempName
sTempFile = sTempFolder & "\" & sTempFile

objShell.Run "cmd /c ping -n 2 -l 8 " & strComputer & ">" & sTempFile,0,True

Set oFile = objFso.GetFile(sTempFile)
set oTS = oFile.OpenAsTextStream(FOR_READING)
do while oTS.AtEndOfStream <> True
sReturn = oTS.ReadLine
if instr(sReturn, "Reply")>0 then
CheckIfClientExists = True
Exit Do
End If
Loop

ots.Close
oFile.delete
End Function