RDP aktivieren – remote per Powershell
4. Oktober 2019
RDP (Remote Desktop Protocol) ist bereits standardmäßig auf aktuellen Windows-Clients deaktiviert – aus Sicherheitsgründen . Nutzen Unternehmen allerdings intern die RDP-Dienste, um sich auf entfernten Systemen anzumelden, müssen die benötigten Funktionen erst auf den betroffenen Systemen eingeschaltet werden. Manuell kann dies sehr viel Zeit in Anspruch nehmen, aber findige Administratoren nutzen für solche Aufgaben passende Cmdlets. Für diesen Zweck ist beispielsweise das Skript „Enable-RDP “ von „MichalGajda“ auf Technet maßgeschneidert:
5
Remotly
enable
RDP on domain machines or workgroup.
8
Use Enable-RDP to
enable
RDP on all domain machines or workgroup.
10
.PARAMETER ComputerName
11
Specific Computer Name or Ldap path to object or
set
of object like computer, OU or whole domain.
14
Get-ADComputer PC1 | Enable-RDP
16
RDP is enabled
in
Remote Registry on machine: PC1
19
Enable-RDP -ComputerName
"CN=Computers,DC=your,DC=domain,DC=com"
21
RDP is enabled
in
Remote Registry on machine: PC1
22
RDP is enabled
in
Remote Registry on machine: PC2
23
WARNING: Machine: PC3 is unavailable
24
RDP is enabled
in
Remote Registry on machine: PC4
27
"PC1"
,
"PC2"
| Enable-RDP
29
RDP is enabled
in
Remote Registry on machine: PC1
30
RDP is enabled
in
Remote Registry on machine: PC2
37
SupportsShouldProcess=$True,
42
[Parameter(ValueFromPipeline=$True)]
43
[Array]$ComputerName =
"LocalHost"
50
if
($ComputerName -match
"="
)
52
Write-Verbose
"Searching LDAP Objects in path: $ComputerName"
53
$Searcher=[adsisearcher]
"(&(objectCategory=computer)(objectClass=computer))"
55
$ComputerName = ([String]$ComputerName).replace(
"LDAP:// "
,
""
)
56
$Searcher.SearchRoot=
"LDAP:// $ComputerName"
57
$Results=$Searcher.FindAll()
62
Write-Verbose
"Direct access to specific machine: $ComputerName"
63
$Results = $ComputerName
67
Foreach($result
in
$results)
71
$ComputerName = $result
75
$ComputerName = $result.Properties.Item(
"Name"
)
79
if
($pscmdlet.ShouldProcess($ComputerName,
"Enable RDP"
))
84
Write-Verbose
"Checking Remote Registry status via WinRM on machine: $ComputerName"
85
$RRStatusIC = Invoke-Command -ComputerName $ComputerName -ScriptBlock {C:\Windows\System32\sc query RemoteRegistry} -ErrorAction SilentlyContinue
87
if
([string]$RRStatusIC -
eq
""
)
90
Write-Verbose
"Checking Remote Registry status via WMI on machine: $ComputerName"
91
$RRStatusGWMI = Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='RemoteRegistry'"
-ErrorAction SilentlyContinue
93
if
($RRStatusGWMI -notlike $null)
96
Write-Verbose
"Checking Remote Registry status via Get-Service on machine: $ComputerName"
97
$RRStatusGS = Get-Service -ComputerName $ComputerName RemoteRegistry -ErrorAction SilentlyContinue
99
if
($RRStatusGS -notlike $null)
103
Write-Warning
"Machine: $ComputerName is unavailable"
111
Write-Verbose
"Starting Remote Registry via WMI on machine: $ComputerName"
112
(Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='RemoteRegistry'"
-ErrorAction SilentlyContinue ).InvokeMethod(
"StartService"
,$null) | Out-Null
117
Write-Warning
"Can't start Remote Registry on machine: $ComputerName"
124
if
($RRStatusIC -match
"STOPPED"
)
127
Write-Verbose
"Starting Remote Registry via WinRM on machine: $ComputerName"
128
Invoke-Command -ComputerName $ComputerName -ScriptBlock {net start RemoteRegistry} -ErrorAction SilentlyContinue | Out-Null
132
Write-Verbose
"Remote Registry is Running on machine: $ComputerName"
137
While($EnableFlag -
eq
$null)
144
Write-Verbose
"Modifying Remote Registry on machine: $ComputerName"
145
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
'LocalMachine'
, $ComputerName)
146
$regkey = $reg.OpenSubKey(
"SYSTEM\\CurrentControlSet\\Control\\Terminal Server"
,$
true
)
147
$regkey.SetValue(
'fDenyTSConnections'
,
'0'
,
'DWord'
)
149
Write-Host
"RDP is enabled in Remote Registry on machine: $ComputerName"
154
[string]$HostIP = ([System.Net.Dns]::GetHostByName($ComputerName)).AddressList
157
Write-Verbose
"Modifying Remote Registry by IP on machine: $ComputerName"
158
$reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(
'LocalMachine'
, $HostIP)
159
$regkey = $reg.OpenSubKey(
"SYSTEM\\CurrentControlSet\\Control\\Terminal Server"
,$
true
)
160
$regkey.SetValue(
'fDenyTSConnections'
,
'0'
,
'DWord'
)
163
Write-Host
"RDP is enabled in Remote Registry on machine: $ComputerName"
167
Write-Warning
"You havent access to Remote Registry on machine: $ComputerName"
172
Write-Verbose
"Enable firewall rules on machine: $ComputerName"
173
$fw = Invoke-Command -ComputerName $ComputerName -ScriptBlock {netsh firewall
set
service remoteadmin
enable
} -ErrorAction SilentlyContinue
174
$fw = Invoke-Command -ComputerName $ComputerName -ScriptBlock {netsh firewall
set
service remotedesktop
enable
} -ErrorAction SilentlyContinue
175
if
(!($fw -match
"Ok."
))
177
Write-Warning
"Can't enable firewall rules on machine: $ComputerName. Try use maunaly winrm quickconfig on remote machine."
180
if
([string]$RRStatusIC -
ne
""
)
183
Write-Verbose
"Restart Terminal Service service via WinRM on machine: $ComputerName"
184
Invoke-Command -ComputerName $ComputerName -ScriptBlock {net stop UmRdpService} -ErrorAction SilentlyContinue | Out-Null
185
Invoke-Command -ComputerName $ComputerName -ScriptBlock {net stop TermService} -ErrorAction SilentlyContinue | Out-Null
186
Invoke-Command -ComputerName $ComputerName -ScriptBlock {net start TermService} -ErrorAction SilentlyContinue | Out-Null
187
Invoke-Command -ComputerName $ComputerName -ScriptBlock {net start UmRdpService} -ErrorAction SilentlyContinue | Out-Null
194
Write-Verbose
"Restart Terminal Service service via WMI on machine: $ComputerName"
195
(Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='UmRdpService'"
-ErrorAction SilentlyContinue ).InvokeMethod(
"StopService"
,$null) | Out-Null
196
(Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='TermService'"
-ErrorAction SilentlyContinue ).InvokeMethod(
"StopService"
,$null) | Out-Null
197
(Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='TermService'"
-ErrorAction SilentlyContinue ).InvokeMethod(
"StartService"
,$null) | Out-Null
198
(Get-WmiObject -computer $ComputerName Win32_Service -Filter
"Name='UmRdpService'"
-ErrorAction SilentlyContinue ).InvokeMethod(
"StartService"
,$null) | Out-Null
202
Write-Warning
"Can't restart Terminal Service on machine: $ComputerName. Try Reboot this machine manualy."
Florian Huttenloher