forked from jagilber/powershellScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdv-vdi-query.ps1
415 lines (358 loc) · 13.2 KB
/
rdv-vdi-query.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
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<#
.SYNOPSIS
powershell script to query rds/rdv deployment for virtualization hosts and vdi machines
.DESCRIPTION
powershell script to query rds/rdv deployment for virtualization hosts and vdi machines
machines that are running will be queried for rdp port, process list, qwinsta, network and share connectivity
.NOTES
Author : jagilber
Version : 161209
History :
161208.5 added background jobs to speed up vm querying
.EXAMPLE
.\rdv-vdi-query.ps1
query for connection broker, hosts, and desktops, and run script. slow
.EXAMPLE
.\rdv-vdi-query.ps1 -activeBroker broker01
query specific connection broker, query for hosts, and desktops, and run script. slow
.EXAMPLE
.\rdv-vdi-query.ps1 -activeBroker broker01 -desktopsFile c:\temp\desktops.txt -virtualizationHostsFile c:\temp\hosts.txt
query specific connection broker, specific hosts, specific desktops, and run script. fast.
.EXAMPLE
.\rdv-vdi-query.ps1 -activeBroker broker01 -desktopsFile c:\temp\desktops.txt -virtualizationHostsFile c:\temp\hosts.txt -generatefiles
query specific connection broker, for specific hosts, for specific desktops, write to specified files, and run script. slow.
.EXAMPLE
.\rdv-vdi-query.ps1 -update
check github for updated script file.
.PARAMETER activeBroker
optional parameter to specify active connection broker
.PARAMETER desktopsFile
optional parameter to specify file containing names of vdi desktops to be queried.
if blank and used with -update, it will be used as file for complete vdi list from deployment.
.PARAMETER virtualizationhostsFile
optional parameter to specify file containing names of virtualization hosts to be queried.
if blank and used with -update, it will be used as file for complete virtualizationhosts list from deployment.
.PARAMETER generateFiles
optional parameter to be used with desktopsfile and virtualizationhostsFile to generate new files from deployment. slow
.PARAMETER update
optional parameter to check for new version of this script.
#>
param(
[Parameter(Mandatory=$false)]
[string]$activeBroker,
[string]$virtualizationHostsFile,
[string]$desktopsFile,
[switch]$generateFiles,
[switch]$update
)
Set-StrictMode -Version "Latest"
$ErrorActionPreference = "SilentlyContinue"
$global:desktops = @{}
$global:virtualizationHosts = @{}
$outFile = "rdv-vdi-query.txt"
$error.clear()
$updateUrl = "https://raw.githubusercontent.com/jagilber/powershellScripts/master/rdv-vdi-query.ps1"
$jobName = "vdiquery"
$jobThrottle = 20
#----------------------------------------------------------------------------
function main()
{
Start-Transcript -Path $outFile -Append
write-host "----------------------------------------"
write-host "$(get-date) starting"
set-location $psscriptroot
if(!(Import-Module RemoteDesktop))
{
write-host "error: remotedesktop ps module not available. returning" -ForegroundColor Red
return
}
# clean old jobs
get-job -name $jobName | remove-job -force
if($update)
{
if((get-update -updateUrl $updateUrl -destinationFile $MyInvocation.ScriptName))
{
write-host "script updated. restart"
return
}
else
{
write-host "no update. continuing"
}
}
# check broker
if([string]::IsNullOrEmpty($activeBroker))
{
if(($availability = Get-RDConnectionBrokerHighAvailability))
{
$activeBroker = $availability.ActiveManagementServer
}
if([string]::IsNullOrEmpty($activeBroker))
{
if((Get-RDServer).roles -eq "RDS-CONNECTION-BROKER")
{
$activeBroker = @((Get-RDServer | ? Roles -eq "RDS-CONNECTION-BROKER").Server)[0]
}
else
{
write-host "pass active connection broker name as argument for script. returning"
return
}
}
}
write-host "$(get-date) using active broker: $($activeBroker)"
if(!$generateFiles -and
![string]::IsNullOrEmpty($desktopsFile) -and
(test-path $desktopsFile) -and
![string]::IsNullOrEmpty($virtualiazationHostsFile) -and
(test-path $virtualizationHostsFile))
{
write-host "will use files"
}
else
{
write-host "querying deployment. this make take a while ... use -virtualizationhostsFile and -desktopsFile arguments with file names to process faster"
$desktopCollections = Get-RDVirtualDesktopCollection -ConnectionBroker $activeBroker
}
write-host "$(get-date) checking virtualization"
if(![string]::IsNullOrEmpty($virtualizationHostsFile) -and (test-path $virtualizationHostsFile))
{
# read from file
foreach($virtualizationhost in (Get-Content $virtualizationHostsFile))
{
if(!$global:virtualizationHosts.ContainsKey($virtualizationHost))
{
write-host "adding virtualization host from file $($virtualizationHost)"
$global:virtualizationHosts.Add($virtualizationHost,@{})
}
}
}
else
{
# query deployment
foreach($desktopCollection in $desktopCollections)
{
foreach($virtualizationHostDesktop in (Get-RDVirtualDesktop -CollectionName $desktopCollection.CollectionName -ConnectionBroker $activeBroker))
{
if(!$global:desktops.ContainsKey($virtualizationHostDesktop.VirtualDesktopName))
{
write-host "adding Desktop $($virtualizationHostDesktop.VirtualDesktopName)"
$global:desktops.Add($virtualizationHostDesktop.VirtualDesktopName,@{})
if($generateFiles)
{
out-file -Append -InputObject $virtualizationHostDesktop.VirtualDesktopName -FilePath $desktopsFile
}
}
$virtualizationhost = $virtualizationHostDesktop.HostName
if(!$global:virtualizationHosts.ContainsKey($virtualizationHost))
{
write-host "adding virtualization host $($virtualizationHost)"
$global:virtualizationHosts.Add($virtualizationHost,@{})
if($generateFiles)
{
out-file -Append -InputObject $virtualizationHost -FilePath $virtualizationHostsFile
}
}
}
}
}
write-host "$(get-date) checking desktop"
if(![string]::IsNullOrEmpty($desktopsFile) -and (test-path $desktopsFile))
{
$global:desktops = @{}
foreach($desktop in (Get-Content $desktopsFile))
{
if(!$global:desktops.ContainsKey($desktop))
{
write-host "adding desktop from file $($desktop)"
$global:desktops.Add($desktop,@{})
}
}
}
write-host "$(get-date) querying virtualization hosts"
foreach($vhost in $global:virtualizationHosts.GetEnumerator())
{
$vhost.Value.Available = (Test-NetConnection -ComputerName $vhost.Name -Port 135).TcpTestSucceeded
if($vhost.Value.Available)
{
$vhost.Value.Vms = get-vm -ComputerName $vhost.Name
}
else
{
write-host "error: vhost not available $($vhost.Name)" -ForegroundColor Red
continue
}
write-host "querying desktops on host $($vhost.Name)"
foreach($vm in $vhost.Value.VMs)
{
write-host "checking vm $($vm.Name)"
if($vm.State -eq "Running" -and $global:desktops.ContainsKey($vm.Name))
{
start-bgJob -vmname $vm.Name
}
elseif(!$global:desktops.ContainsKey($vm.Name))
{
write-host "information:skipping vm $($vm.Name) because it is not not part of desktops list." -ForegroundColor DarkGray
}
else
{
write-host "warning:skipping vm $($vm.Name) because it is not in running state. state: $($vm.State)" -ForegroundColor Yellow
continue
}
}
}
while(get-job -name $jobName)
{
$jobs = get-job -name $jobName
foreach($job in $jobs)
{
if($job.State -eq "Completed")
{
$results = $job | receive-job
$job | remove-job -force
if($global:desktops.ContainsKey($results.Name))
{
$Global:desktops.Remove($results.Name)
}
$Global:desktops.Add($results.Name,$results)
}
$job
}
start-sleep -Seconds 1
}
write-host "$(get-date) exporting data"
$global:virtualizationHosts | ConvertTo-Json -Depth 3 | out-file ("$($outFile).virtualizationhosts.txt")
$global:desktops | ConvertTo-Json -Depth 3 | out-file ("$($outFile).desktops.txt")
write-host "$(get-date) finished"
write-host "----------------------------------------"
write-host "log is here:$($outFile)"
Stop-Transcript
}
#----------------------------------------------------------------------------
function start-bgJob([string]$vmname)
{
#throttle
if(@(get-job -Name $jobName).Count -gt 0)
{
while(@((get-job -Name $jobName).State -eq "Running").Count -gt $jobThrottle)
{
write-host "waiting for job resources..."
start-sleep -Seconds 1
}
}
$job = start-job -Name $jobName -ScriptBlock `
{
param($vm)
$desktop = @{}
write-host "$(get-date) querying desktops"
$desktop.Name = $vm
$desktop.ProcessResults = $null
$desktop.ProcessResultsError = $null
$desktop.QwinstaResults = $null
$desktop.QwinstaResultsError = $null
$desktop.RDPAvailable = $false
$desktop.RDPAvailableError = $null
$desktop.RPCAvailable = $false
$desktop.RPCAvailableError = $null
$desktop.ShareResults = $false
$desktop.ShareResultsError = $null
$desktop.RPCAvailable = (Test-NetConnection -ComputerName $vm -Port 135).TcpTestSucceeded
if($desktop.RPCAvailable)
{
# do additional check?
}
else
{
write-host "error: desktop RPC not available $($vm)" -ForegroundColor Red
$desktop.RPCAvailableError = $error
$error.Clear()
}
$desktop.RDPAvailable = (Test-NetConnection -ComputerName $vm -Port 3389).TcpTestSucceeded
if($desktop.RDPAvailable)
{
# do additional check?
}
else
{
write-host "error: desktop RDP not available $($vm)" -ForegroundColor Red
$desktop.RDPAvailableError = $error
$error.Clear()
}
$desktop.QWinstaResults = Invoke-expression "qwinsta.exe /server:$($vm) /VM"
if($desktop.QWinstaResults)
{
# do additional check?
}
else
{
$desktop.QwinstaResultsError = $error
$error.Clear()
write-host "error: desktop qwinsta not available $($vm)" -ForegroundColor Red
}
$desktop.ShareResults = test-path "\\$($vm)\admin$"
if($desktop.ShareResults)
{
# do additional check?
}
else
{
$desktop.ShareResultsError = $error
$error.Clear()
write-host "error: desktop share not available $($vm)" -ForegroundColor Red
}
$desktop.ProcessResults = Invoke-expression "tasklist /s $($vm) /v"
if($desktop.ProcessResults)
{
# do additional check?
}
else
{
$desktop.ProcessResultsError = $error
$error.Clear()
write-host "error: desktop processes not available $($vm)" -ForegroundColor Red
}
$desktop
} -ArgumentList $vmname
return $job
}
#----------------------------------------------------------------------------
function get-update($updateUrl, $destinationFile)
{
write-host "get-update:checking for updated script: $($updateUrl)"
try
{
$git = Invoke-RestMethod -Method Get -Uri $updateUrl
# git may not have carriage return
if ([regex]::Matches($git, "`r").Count -eq 0)
{
$git = [regex]::Replace($git, "`n", "`r`n")
}
if (![IO.File]::Exists($destinationFile))
{
$file = ""
}
else
{
$file = [IO.File]::ReadAllText($destinationFile)
}
if (([string]::Compare($git, $file) -ne 0))
{
log-info "copying script $($destinationFile)"
[IO.File]::WriteAllText($destinationFile, $git)
return $true
}
else
{
log-info "script is up to date"
}
return $false
}
catch [System.Exception]
{
write-host "get-update:exception: $($error)"
$error.Clear()
return $false
}
}
#----------------------------------------------------------------------------
main