Friday, January 11, 2008

VB Script to Enable/Disable Remote Desktop through Group Policy

This script will enable or disable Remote Desktop on multiple computers on an IP segment. The first input box asks for the subnet (i.e. 192.168.1). The second input box asks for the lowest number on the segment that you would like to start with. The third box asks for the highest number on the segment that you want to end with. I would like to add more to this script that will allow it to ping an IP address first. If there is no response it will move on to the next address instead of trying to enable RD on a non-existent PC. I will add that when I have time.

DIM strComputer, strIPInput, strLow, strHigh
'On Error Resume Next
Set WshShell = WScript.CreateObject("WScript.Shell")

'Set to 0 to disable Remote Desktop, set to 1 to enable Remote Desktop
Const ENABLE_CONNECTIONS = 1

strIPInput = InputBox("What is the subnet? Format as WW.XX.YY.")
strLow = InputBox("What is the low address in the range?")
strHigh = InputBox("What is the highest address in the range?")

If Right(strIPInput,1) <> "." Then
strIPInput = strIPInput & "."
End If

If strIPInput = "" OR strLow = "" OR strHigh = "" OR strHigh > 254 Then
Wscript.Echo "Invalid input - Script Cancelled"
wscript.Quit
End If

For i = strLow to strHigh

strComputer = strIPInput & i

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
Next

WshShell.Popup "Complete", 10, "Remote Desktop Status", 64

No comments: