-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRDPs2RDG.ps1
282 lines (240 loc) · 10.3 KB
/
RDPs2RDG.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<#
.SYNOPSIS
This script loops through all valid RDP Files in a provided folder path, extracts machine and logon name for each of them and generates a Remote Desktop Connection Manager group file for your convenience.
.DESCRIPTION
This script loops through all valid RDP Files in a provided folder path, extracts machine and logon name for each of them and generates a Remote Desktop Connection Manager group file for your convenience.
Provide the default logon user name to connect to the remote machines with. If not provided, "Administrator" will be used.
Provide the default logon domain name to connect to the remote machines with. If not provided, "MyDomain" will be used.
Provide the path to the existing RDP files to generate the RDCMan file from. If not provided, the path where the script is located will be used.
Provide the name for the group node containing the remote servers to connect to. If not provided, "MyWorkspace" will be used.
Provide the comma or semi-colon delimited list of usernames for creating login profiles. If you do, please also provide the corresponding password for all of them.
.PARAMETER DefaultLogonName
(OPTIONAL)
Defines the logon user name to connect to the remote machines with. If not provided, "Administrator" will be used.
If there is already a defined username in the RDP file, this will be used and this parameter value will be ignored.
.PARAMETER DefaultLogonDomain
(OPTIONAL)
Defines the logon domain name to connect to the remote machines with. If not provided, "MyDomain" will be used.
.PARAMETER RdpFilesPath
(OPTIONAL)
Defines the path to the existing RDP files to generate the RDCMan file from. If not provided, the path where the script is located will be used.
.PARAMETER WorkspaceName
(OPTIONAL)
Defines the name for the group node containing the remote servers to connect to. Default is "MyWorkspace"
.PARAMETER Users
(OPTIONAL)
Comma or semi-colon delimited list of usernames for login profiles. Leave the domain off the username, it derives it from the DefaultLogonDomain parameter
.PARAMETER DefaultPassword
(OPTIONAL)
Default password used for all user accounts in test environments. This will store the password encrypted in the RDG file using the Windows ProtectedData API.
.PARAMETER debugging
(OPTIONAL)
Switch to turn on extra debug logging
.EXAMPLE
RDPs2RDG.ps1 -DefaultLogonName WsAdm -DefaultLogonDomain mydomain -RdpFilesPath C:\RDPs -WorkspaceName MyWorkspace -Users "jsmith,mroony,aguy" -DefaultPassword "testPassword"
.NOTES
File Name : RDPs2RDG.ps1
Author : https://github.com/rito2k/RDPs2RDG
Version : 3.1
Date : Dec 22nd, 2021
You can download the latest Remote Desktop Connection Manager version at https://docs.microsoft.com/en-us/sysinternals/downloads/rdcman
#>
Param (
[Parameter(Mandatory=$false)]
[string]$DefaultLogonName = "Administrator",
[Parameter(Mandatory=$false)]
[string]$DefaultLogonDomain = "MyDomain",
[Parameter(Mandatory=$false)]
[string]$RdpFilesPath,
[Parameter(Mandatory=$false)]
[string]$WorkspaceName = "MyWorkspace",
[Parameter(Mandatory=$false)]
[string]$Users,
[Parameter(Mandatory=$false)]
[string]$DefaultPassword,
[switch]$debugging
)
Add-Type -AssemblyName System.Security
function encryptPass($plaintext){
$bytetext = [System.Text.Encoding]::Unicode.GetBytes($plaintext)
#$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser = 0
$protectedPass = [System.Security.Cryptography.ProtectedData]::Protect($bytetext,$null,0)
return [System.Convert]::ToBase64String($protectedPass)
}
$Time1=Get-Date
if ($RdpFilesPath -eq ""){
$RdpFilesPath = $PSScriptRoot
}
else {
If (!(Test-Path $RdpFilesPath -PathType Container)){
Write-Host "Path '$RdpFilesPath' does not exist!" -ForegroundColor Red
Exit
}
}
try {
if (!($RdpFiles = Get-ChildItem $RdpFilesPath -Filter "*.rdp")){
Write-Host "No RDP files found in '$RdpFilesPath'!" -ForegroundColor Red
Exit
}
$NewRDCFile = $RdpFilesPath + "\$WorkspaceName.rdg"
If (Test-Path $NewRDCFile -PathType Leaf){
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$answer = $wshell.Popup("The file '$NewRDCFile' already exists! Do you want overwrite it?",0,"Attention!",32+4)
if ($answer -eq 6)
{Remove-Item $NewRDCFile -Force}
else
{
If ($debugging) {Write-Host "Exiting..." -ForegroundColor Gray}
Exit
}
}
}
catch {
}
If ($debugging) {Write-Host "Building base file '$NewRDCFile' . . ." -ForegroundColor Gray}
#Create base RDCMan file
$xmlsb = '<?xml version="1.0" encoding="utf-8"?>
<RDCMan programVersion="2.8.2" schemaVersion="3">
<file>
<credentialsProfiles />
<properties>
<expanded>True</expanded>
<name>$WorkspaceName</name>
</properties>
<remoteDesktop inherit="None">
<sameSizeAsClientArea>True</sameSizeAsClientArea>
<fullScreen>False</fullScreen>
<colorDepth>24</colorDepth>
</remoteDesktop>
<localResources inherit="None">
<audioRedirection>Remote</audioRedirection>
<audioRedirectionQuality>Dynamic</audioRedirectionQuality>
<audioCaptureRedirection>DoNotRecord</audioCaptureRedirection>
<keyboardHook>Remote</keyboardHook>
<redirectClipboard>True</redirectClipboard>
<redirectDrives>False</redirectDrives>
<redirectDrivesList />
<redirectPrinters>False</redirectPrinters>
<redirectPorts>False</redirectPorts>
<redirectSmartCards>False</redirectSmartCards>
<redirectPnpDevices>False</redirectPnpDevices>
</localResources>
<group>
<properties>
<expanded>True</expanded>
<name>$WorkspaceName</name>
</properties>
<logonCredentials inherit="None">
<profileName scope="Local">Custom</profileName>
<userName>$DefaultLogonName</userName>
<password />
<domain>$DefaultLogonDomain</domain>
</logonCredentials>
</group>
</file>
<connected />
<favorites />
<recentlyUsed />
</RDCMan>'
#Interpret & replace variable value
$xmlsb = $xmlsb.Replace('$WorkspaceName',$WorkspaceName)
$xmlsb = $xmlsb.Replace('$DefaultLogonName',$DefaultLogonName)
$xmlsb = $xmlsb.Replace('$DefaultLogonDomain',$DefaultLogonDomain)
#Transform string to XML structure
$xml = [xml]$xmlsb
#Save base RDC file
$xml.Save($NewRDCFile)
If ($debugging) {Write-Host "Adding remote machines to $NewRDCFile . . ." -ForegroundColor Gray}
#Read XML Object
[XML]$RDCConfig = Get-Content -Path $NewRDCFile
#Loop through RDP files and collect machine name as "connection strings"
$RdpFiles = Get-ChildItem $RdpFilesPath -Filter "*.rdp"
foreach ($RdpFile in $RdpFiles)
{
try{
#$RdpContent = Get-Content $RdpFile
if ($FullAdressString = Select-String -Path $RdpFile.FullName -Pattern "full address:s:" -SimpleMatch){
$MachineConnectionString = $FullAdressString.Line.Trim().Substring(15)
}
[string]$MachineName = $RdpFile.Name.Substring(0,$RdpFile.Name.LastIndexOf("."))
$MachineName=$MachineName.Replace('&','and')
$MachineName=$MachineName.Replace(">","")
$MachineName=$MachineName.Replace("<","")
if ($UserNameString = Select-String -Path $RdpFile.FullName -Pattern "username:s:" -SimpleMatch){
$UserName = $UserNameString.Line.Trim().Substring(11)
}
else {
$UserName = $DefaultLogonName
}
}
catch{
$MachineConnectionString = ""
}
if (($null -eq $MachineConnectionString) -or ($MachineConnectionString -eq ""))
{
Write-Host "Warning: Could not find full address in '$RdpFile', please check it." -ForegroundColor Yellow
}
else {
if ($UserName -ne $DefaultLogonName){
#Create server XML object with not inherited credentials
$xmlserver = "
<server>
<properties>
<displayName>$MachineName</displayName>
<name>$MachineConnectionString</name>
</properties>
<logonCredentials inherit=""None"">
<profileName scope=""Local"">Custom</profileName>
<userName>$UserName</userName>
</logonCredentials>
</server>"
#Interpret & replace variable values
$xmlserver = $xmlserver.Replace('$WorkspaceName',$WorkspaceName)
#Transform string to XML structure
$Server = [xml]$xmlserver
}
else{
[XML]$Server ="
<server>
<properties>
<displayName>$MachineName</displayName>
<name>$MachineConnectionString</name>
</properties>
</server>"
}
$New = $RDCConfig.ImportNode($Server.Server, $true)
try
{$RDCConfig.RDCMan.file.group.AppendChild($New) | out-null}
Finally
{
If ($debugging) {Write-Host "Added server '$MachineName' (Logon username: $UserName)" -ForegroundColor Cyan}
}
}
}#for-each RDP file
if($Users.Length -gt 0){
$userArray = $Users.Split($(",;".ToCharArray()))
foreach($user in $userArray){
$profileTemplate = @"
<credentialsProfile inherit="None">
<profileName scope="Local">{0}\{1}</profileName>
<userName>{1}</userName>
<password>{2}</password>
<domain>{0}</domain>
</credentialsProfile>
"@
$encPass = encryptPass -plaintext $DefaultPassword
[xml]$xmlTemplate = $profileTemplate -f $DefaultLogonDomain, $user, $encPass
$New = $RDCConfig.ImportNode($xmlTemplate.credentialsProfile, $true)
try
{$RDCConfig.RDCMan.file.GetElementsByTagName('credentialsProfiles').AppendChild($New)| out-null}
Finally
{
If ($debugging) {Write-Host "Added user credentials for: '$user'" -ForegroundColor Cyan}
}
}#for-each user
}#if Users specified
#Save final RDCMan file
$RDCConfig.Save($NewRDCFile)
Write-Host "$NewRDCFile has been successfully generated!" -ForegroundColor Green
$Time2=Get-Date
If ($debugging) {write-host "Elapsed time: "($Time2 - $Time1) -ForegroundColor Gray}