-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rdcman.psm1
317 lines (220 loc) · 9.29 KB
/
Rdcman.psm1
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<#
Create an "RDCMan ready" RDG file with all the running Windows VMs connected in PowerCLI.
- Only Windows OS is supported.
- The VM must have the VMware tools installed.
- RDCMan must be closed.
### CHANGE LOG
01/03/2019
! Bug found - The RDG file must be opened at least once before running the set command to update it
07/11/2018
- [Set-RDCManFile] Adds new VMs and new domains to an existing file.
- Added Function Set-RDCManFile.
17/08/2018
- Added 3389 TCP port check on VM IP(s) - Set the first one the responds as hostname and "NO-RDP-RESPONSE" if none does.
- Added TCPCheckTimeout parameter to Add-RDCManVM function to configure the time it takes for the TCP port check to time out.
#>
Function Add-RDCManVM {
param(
[System.IO.FileInfo]
$RdgFilePath,
[parameter(position=0)]
[VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine[]]
$VM,
[int]
$TCPCheckTimeout = 300
)
# RDCMan must be closed otherwise the changes won't persist
if (get-process RDCMan -ErrorAction SilentlyContinue) {Write-Warning "Close RDCMan before editing the file"; break}
# Grab rdg file
$rdgfile = Get-ChildItem $RdgFilePath
$RdgContent = $rdgfile | Get-Content
# Process VMs
$VM = $VM | where {$_.powerstate -eq "poweredon" -and $_.guest.OSFullName -match "windows"}
foreach ($V in $VM) {
if ($v.guest.OSFullName -match "windows" -and !($RdgContent -match "<displayName>$($v.guest.HostName)</displayName>") -and !($RdgContent -match "<name>$IP</name>")) {
if ($v.guest.State -eq "Running") { # Tools running
$Prog++
if (!$v.guest.ExtensionData.IpStack.dnsconfig.domainname) {$group = "No-Domain"}
else {$group = $v.guest.ExtensionData.IpStack.dnsconfig.domainname}
$group = $group.ToLower()
# Check if group exists
if (!($RdgContent -match "<name>$Group</name>")) {Write-Warning "Group $Group not found in specified rdg file; $($V.name) skipped"; break}
Write-Progress -Activity "Group $Group : VM #$Prog/$($VM.count)" -Status $V.name -PercentComplete ($Prog/$VM.count*100) -Id 1
$IP = $v.guest.IPAddress | where {$_ -like "*.*.*.*"}
while (!$3389 -and $i -lt $ip.count) {
foreach ($IPP in $IP) {
$PortPing = New-Object System.Net.Sockets.TCPClient
# Try TCP port and stop after the $TCPCheckTimeout timeout
$ExecFrame = (get-date).AddMilliseconds($TCPCheckTimeout)
$PortConnect = $PortPing.BeginConnect("$IPP",3389,$null,$null)
While (((get-date) -lt $ExecFrame) -and ($PortPing.Connected -ne "true")) {}
# Populate $3389 and break from loop if tcp open
if ($PortPing.Connected) {$3389 = $IPP; break}
$PortPing.Close()
$i++
}
}
if (!$3389) {$3389 = "NO-RDP-RESPONSE"}
$VMNotes = @()
$VMNotes += $v.Notes
$v.CustomFields | where value | ForEach-Object {$VMNotes += "$($_.key) = $($_.value)"}
$AddContent +=
@"
<server>
<properties>
<displayName>$($v.guest.HostName)</displayName>
<name>$($3389)</name>
<comment>$($v.guest.HostName)
VM : $($v.name)
vCenter: $($DefaultVIServer.name)
Cluster: $($v | get-cluster | select -ExpandProperty name)
IP(s) : $([string]$IP)
OS : $($v.guest.OSFullName)
VM Notes:
$($VMNotes -replace "@",' at ' -replace "<","" -replace ">","" -replace "&"," and ")
</comment>
</properties>
</server>
"@
Clear-Variable i,3389 -ErrorAction SilentlyContinue
} else {Write-Warning "Tools not running on $($v.name)"} # Tools running
} else {Write-warning "$($v.name): not a windows host or already in rdg file"}
}
if (!$AddContent) {break}
# Find the index of the record </properties> following the group name
$StringIndexToReplace = ($RdgContent | where {$_ -match "<name>$Group</name>"}).readcount
# If more than one record the script will fail as it is not expecting an array
if ($StringIndexToReplace.count -gt 1) {Write-Warning "More than one entity called $Group"; break}
# Append the new servers to the record </Properties>
$RdgContent[$StringIndexToReplace] = "</properties>$AddContent"
# Write the rdg file
$RdgContent | Out-File $rdgfile -Confirm:$false -Encoding utf8
"File written"
}
Function Start-RDCMan {
param(
[System.IO.FileInfo]
$RdgFile
)
"Opening $RdgFile in RDCMan (required before using Set-RDCManFile)"
Start-Process $RdgFile
While ( !(get-process RDCMan -ErrorAction SilentlyContinue) ) {sleep -Milliseconds 200}
}
Function New-RDCManFile {
param(
[System.IO.FileInfo]
$RdgFilePath,
[parameter(position=0)]
[VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine[]]
$VM
)
# RDCMan must be closed otherwise the changes won't persist
if (get-process RDCMan -ErrorAction SilentlyContinue) {Write-Warning "Close RDCMan before editing the file"; break}
Write-Progress -Activity "Gathering VM and Domain lists"
$VM = $VM | where {$_.powerstate -eq "poweredon" -and $_.guest.OSFullName -match "windows"}
$Domains = $VM | select @{l="domain";e={$_.guest.ExtensionData.IpStack.dnsconfig.domainname}} | where domain | select -ExpandProperty domain -Unique domain
Write-Progress -Activity "Creating base rdg file"
$RDGFile = New-Item -Path $RdgFilePath -ItemType File -Force
# Prepare the no-domain section
$DomTxt = @"
<group>
<properties>
<expanded>False</expanded>
<name>No-Domain</name>
</properties>
</group>
"@
# Prepare each domain's section
foreach ($dom in $Domains.tolower()) {
$DomTxt += @"
<group>
<properties>
<expanded>False</expanded>
<name>$dom</name>
</properties>
</group>
"@
}
# Fit the prepared section in the global rdg frame and write to file
@"
<?xml version="1.0" encoding="utf-8"?>
<RDCMan programVersion="2.7" schemaVersion="3">
<file>
<credentialsProfiles />
<properties>
<expanded>True</expanded>
<name>$($RDGFile.BaseName)</name>
</properties>
<remoteDesktop inherit="None">
<sameSizeAsClientArea>True</sameSizeAsClientArea>
<fullScreen>False</fullScreen>
<colorDepth>24</colorDepth>
</remoteDesktop>
$DomTxt
</file>
<connected />
<favorites />
<recentlyUsed />
</RDCMan>
"@ | Out-File $RdgFilePath
ForEach ($VMDomain in $Domains) {
$a++
Write-Progress -Activity "Domain #$a/$($Domains.count)" -Status $VMDomain -PercentComplete ($a/$Domains.count*100)
# Gather all VMs that are part of the domain being currently processed
$V = $VM | where {$_.guest.ExtensionData.IpStack.dnsconfig.domainname -eq $VMDomain}
# Add gathered VMs to the created RDG file
Add-RDCManVM -RdgFilePath $RdgFilePath -VM $V
}
Write-Progress -Activity "Domain No-Domain : Domain #$a/$($Domains.count)" -Status "No-Domain" -PercentComplete ($a++/$Domains.count*100) -Id 0
$VMNoDomain = $VM | where {!($_.guest.ExtensionData.IpStack.dnsconfig.domainname)}
Add-RDCManVM -RdgFilePath $RdgFilePath -VM $VMNoDomain
"-End-"
Start-RDCMan -RdgFilePath $RdgFile
}
Function Set-RDCManFile {
param(
[System.IO.FileInfo]
$RdgFilePath,
[parameter(position=0)]
[VMware.VimAutomation.ViCore.types.V1.Inventory.VirtualMachine[]]
$VM
)
if ( !($RDGFile = Get-ChildItem $RdgFilePath) ) { Break }
# RDCMan must be closed otherwise the changes won't persist
if (get-process RDCMan -ErrorAction SilentlyContinue) {Write-Warning "Close RDCMan before editing the file"; break}
Write-Progress -Activity "Updating base rdg file"
$VM = $VM | where {$_.powerstate -eq "poweredon" -and $_.guest.OSFullName -match "windows"}
$Domains = $VM | select @{l="domain";e={$_.guest.ExtensionData.IpStack.dnsconfig.domainname}} | where domain | select -ExpandProperty domain -Unique domain
[xml]$DataXML = Get-content $RdgFilePath
# Enter loop for each detected domain
foreach ($dom in $Domains.tolower()) {
# If current domain not in rdg file
if ($DataXML.rdcman.file.group.properties.name -notcontains $dom) {
$DomTxt += @"
<group>
<properties>
<expanded>False</expanded>
<name>$dom</name>
</properties>
</group>
"@
}
}
# Add sections for new domains in the rdg file
if ($DomTxt) {
(Get-content $RDGFile) -replace "</file>","$DomTxt
</file>" | Out-File $RdgFile
}
ForEach ($VMDomain in $Domains) {
$a++
Write-Progress -Activity "Domain #$a/$($Domains.count)" -Status $VMDomain -PercentComplete ($a/$Domains.count*100)
$V = $VM | where {$_.guest.ExtensionData.IpStack.dnsconfig.domainname -eq $VMDomain}
Add-RDCManVM -RdgFilePath $RdgFilePath -VM $V
}
Write-Progress -Activity "Domain No-Domain : Domain #$a/$($Domains.count)" -Status "No-Domain" -PercentComplete ($a++/$Domains.count*100) -Id 0
$VMNoDomain = $VM | where {!($_.guest.ExtensionData.IpStack.dnsconfig.domainname)}
Add-RDCManVM -RdgFilePath $RdgFilePath -VM $VMNoDomain
"-End-"
Start-RDCMan -RdgFile $RdgFilePath
}