Enabling Remote Desktop Remotely
I inherited a 50 node school network. The details behind that are not needed, but it was completely by accident. One of the first things I wanted to do was setup the environment so I could remotely login in to each Windows XP machine to do maintenance. The previous administrators had never done this, every time they wanted to do something they had to go into the school and sit behind the machine. As I am doing this in my spare time and voluntarily, I wanted to make it as easy as possible. Commercial options were out of the question and since I already configured VPN into the network, RDP made the most sense.
RDP can be enabled by setting the fDenyTSConnections registry key under the SYSTEM\CurrentControlSet\Control\Terminal Server tree on each Windows XP machine. Instead of remotely attaching each registry, I decided Powershell could and should be my friend. I exported the machines from the AD computer container into a text file and the parsed those machines, first making sure they were accessible. That is the next part of what I want to do, is clean up the 300 or so computer container, when there are only really 50 or so actual machines. Instead of deleting accounts, they just created new accounts everytime they rebuilt something.
Without further adieu, here is the script, hopefully someone else will find this useful.
# Name: setRDP.ps1# Author: Tom Determan# Date: 09.10.2010# Description: Enables Terminal Services on Windows Machines## servers.txt is an output from the AD Forest of machines.$servers = Get-Content 'c:\Users\tdeterman\Scripts\servers.txt'ForEach ($server in $servers){$line = @($server.Split("`t"))$MachineName = $line[0]$responses = Get-WMIObject -query "select StatusCode from Win32_PingStatus where Address = '$MachineName'"$response = $false# Parse through array of responses from Win32_PingStatus and check for any valid responsesForEach ($i in $responses) {if ($i.statuscode -eq 0) {$response = $truebreak}}# If a response from ping succeeds then go ahead and set the registry key to enable remote desktop.if ($response) {# Attach to the remote registry$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $MachineName)$regKey = $reg.OpenSubKey("SYSTEM\CurrentControlSet\Control\Terminal Server",$true)write-host ("Enabling Terminal Services on: $MachineName")$curval = $regKey.GetValue('fDenyTSConnections')write-host ("Current Value: $curval")# Set Value to 0 to Enable TS Connections$regKey.SetValue('fDenyTSConnections',0)$newval = $regKey.GetValue('fDenyTSConnections')write-host ("New Value: $newval")} else {Write-host ("$MachineName does not response")}}
Permalink Comments off