forked from guyrleech/Citrix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet Citrix OData data.ps1
More file actions
571 lines (487 loc) · 18.8 KB
/
Get Citrix OData data.ps1
File metadata and controls
571 lines (487 loc) · 18.8 KB
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
#requires -version 3
<#
Get Director data from a Delivery controller
Modification History:
@guyrleech 04/03/20 Added Citrix Cloud capability
@guyrleech 05/03/20 Made -join run recursive. Fixed bug with date ranges
#>
<#
.SYNOPSIS
Send queries to a Citrix Delivery Controller or Citrix Cloud and present the results back as PowerShell objects
.PARAMETER ddc
The Delivery Controller to query
.PARAMETER customerId
The Citrix Cloud customerid to query
.PARAMETER authToken
The Citrix cloud authentication token to use. If not specified will prompt for credentials
.PARAMETER credential
A credential object to use to connect to the specific Delivery Controller
.PARAMETER query
The item to query. If not specified, all services/queries available will be returned
.PARAMETER join
Where there are id's in the retrieved query, look up the objects for the id's and substitute them in the output
.PARAMETER last
Only retrieve items such as sessions or connections which have been created in the last specified period
.PARAMETER username
The username to use when querying the Delivery Controller. If not specified the user running the script will be used
.PARAMETER password
The password for the account used to query the Delivery Controller. If the %RandomKey% environment variable is set, its contents will be used as the password
.PARAMETER protocol
The protocol to use to query the Delivery Controller
.PARAMETER oDataVersion
The version of OData to use in the query. If not specified then the script will work out which is the latest available and use that
.EXAMPLE
'.\Get Citrix OData data.ps1' -ddc ctxddc01
Send a web request to the Delivery Controller ctxddc01 and retrieve the list of all available services
.EXAMPLE
'.\Get Citrix OData data.ps1' -ddc ctxddc01 -query Users
Send a web request to the Delivery Controller ctxddc01 to retrieve the list of all users
.EXAMPLE
'.\Get Citrix OData data.ps1' -ddc ctxddc01 -query connections -join -last 7d
Send a web request to the Delivery Controller ctxddc01 to retrieve the list of all connections created within the last 7 days and cross reference any id's returned
.EXAMPLE
'.\Get Citrix OData data.ps1' -customerid yourcloudid -query connections -join -last 7d
Prompt for credentials for the Citrix Cloud customer with id yourcloudid, send a web request to the Citrix Cloud to retrieve the list of all connections created within the last 7 days and cross reference any id's returned
.NOTES
https://developer-docs.citrix.com/projects/monitor-service-odata-api/en/latest/
If an auth token is not passed, the Citrix Remote PowerShell SDK must be available in order to get an auth token - https://www.citrix.com/downloads/citrix-cloud/product-software/xenapp-and-xendesktop-service.html
#>
Param
(
[Parameter(ParameterSetName='ddc',Mandatory=$true)]
[string]$ddc ,
[Parameter(ParameterSetName='cloud',Mandatory=$true)]
[string]$customerid ,
[Parameter(ParameterSetName='cloud',Mandatory=$false)]
[string]$authtoken ,
[string]$query ,
[switch]$join ,
[string]$last ,
[datetime]$from ,
[datetime]$to ,
[System.Management.Automation.PSCredential]$credential ,
[string]$username ,
[string]$password ,
[Parameter(ParameterSetName='ddc',Mandatory=$false)]
[ValidateSet('http','https')]
[string]$protocol = 'http' ,
[int]$oDataVersion = 4
)
## map tables to the date stamp we will filter on
[hashtable]$dateFields = @{
'Session' = 'StartDate'
'Connection' = 'BrokeringDate'
'ConnectionFailureLog' = 'FailureDate'
}
## Modified from code at https://jasonconger.com/2013/10/11/using-powershell-to-retrieve-citrix-monitor-data-via-odata/
Function Invoke-ODataTransform
{
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
$records
)
Begin
{
$propertyNames = $null
[int]$timeOffset = if( (Get-Date).IsDaylightSavingTime() ) { 1 } else { 0 }
}
Process
{
if( $records -is [array] )
{
if( ! $propertyNames )
{
$properties = ($records | Select -First 1).content.properties
if( $properties )
{
$propertyNames = $properties | Get-Member -MemberType Properties | Select -ExpandProperty name
}
else
{
// v4+
$propertyNames = 'NA' -as [string]
}
}
if( $propertyNames -is [string] )
{
$records | Select -ExpandProperty value
}
else
{
ForEach( $record in $records )
{
$h = @{ 'ID' = $record.ID }
$properties = $record.content.properties
ForEach( $propertyName in $propertyNames )
{
$targetProperty = $properties.$propertyName
if($targetProperty -is [Xml.XmlElement])
{
try
{
$h.$propertyName = $targetProperty.'#text'
## see if we need to adjust for daylight savings
if( $timeOffset -and ! [string]::IsNullOrEmpty( $h.$propertyName ) -and $targetProperty.type -match 'DateTime' )
{
$h.$propertyName = (Get-Date -Date $h.$propertyName).AddHours( $timeOffset )
}
}
catch
{
##$_
}
}
else
{
$h.$propertyName = $targetProperty
}
}
[PSCustomObject]$h
}
}
}
elseif( $records -and $records.PSObject.Properties[ 'value' ] ) ##JSON
{
$records.value
}
}
}
Function Get-DateRanges
{
Param
(
[string]$query ,
$from ,
$to ,
[switch]$selective ,
[int]$oDataVersion
)
$field = $dateFields[ ($query -replace 's$' , '') ]
if( ! $field )
{
if( $selective )
{
return $null ## only want specific ones
}
$field = 'CreatedDate'
}
if( $oDataVersion -ge 4 )
{
if( $from )
{
"()?`$filter=$field ge $(Get-Date -date $from -format s)Z"
}
if( $to )
{
"and $field le $(Get-Date -date $to -format s)Z"
}
}
else
{
if( $from )
{
"()?`$filter=$field ge datetime'$(Get-Date -date $from -format s)'"
}
if( $to )
{
"and $field le datetime'$(Get-Date -date $to -format s)'"
}
}
}
Function Resolve-CrossReferences
{
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
$properties ,
[switch]$cloud
)
Process
{
$properties | Where-Object { ( $_.Name -match '^(.*)Id$' -or $_.Name -match '^(SessionKey)$' ) -and ! [string]::IsNullOrEmpty( $Matches[1] ) } | Select-Object -Property Name | ForEach-Object `
{
[string]$id = $Matches[1]
[bool]$current = $false
if( $id -match '^Current(.*)$' )
{
$current = $true
$id = $Matches[1]
}
elseif( $id -eq 'SessionKey' )
{
$id = 'Session'
}
if( ! $tables[ $id ] -and ! $alreadyFetched[ $id ] )
{
if( $cloud )
{
$params.uri = ( "{0}://{1}.xendesktop.net/Citrix/Monitor/OData/v{2}/Data/{3}s" -f $protocol , $customerid , $version , $id ) ## + (Get-DateRanges -query $id -from $from -to $to -selective -oDataVersion $oDataVersion)
}
else
{
$params.uri = ( "{0}://{1}/Citrix/Monitor/OData/v{2}/Data/{3}s" -f $protocol , $ddc , $version , $id ) ## + (Get-DateRanges -query $id -from $from -to $to -selective -oDataVersion $oDataVersion)
}
## save looking up again, especially if it errors as we are not looking up anything valid
$alreadyFetched.Add( $id , $id )
[hashtable]$table = @{}
try
{
Invoke-RestMethod @params | Invoke-ODataTransform | ForEach-Object `
{
## add to hash table keyed on its id
## ToDo we need to go recursive to see if any of these have Ids that we need to resolve without going infintely recursive
$object = $_
[string]$thisId = $null
[string]$keyName = $null
if( $object.PSObject.Properties[ 'id' ] )
{
$thisId = $object.Id
$keyName = 'id'
}
elseif( $object.PSObject.Properties[ 'SessionKey' ] )
{
$thisId = $object.SessionKey
$keyname = 'SessionKey'
}
if( $thisId )
{
[string]$key = $(if( $thisId -match '\(guid''(.*)''\)$' )
{
$Matches[ 1 ]
}
else
{
$thisId
})
$object.PSObject.properties.remove( $key )
$table.Add( $key , $object )
}
## Look at other properties to figure if it too is an id and grab that table too if we don't have it already
ForEach( $property in $object.PSObject.Properties )
{
if( $property.MemberType -eq 'NoteProperty' -and $property.Name -ne $keyName -and $property.Name -ne 'sid' -and $property.Name -match '(.*)Id$' )
{
$property | Resolve-CrossReferences -cloud:$cloud
}
}
}
if( $table.Count )
{
Write-Verbose -Message "Adding table $id with $($table.Count) entries"
$tables.Add( $id , $table )
}
}
catch
{
$nop = $null
}
}
}
}
}
Function Resolve-NestedProperties
{
Param
(
[Parameter(ValueFromPipelineByPropertyName=$true,ValueFromPipeline=$true)]
$properties ,
$previousProperties
)
Process
{
$properties | Where-Object { $_.Name -ne 'sid' -and ( $_.Name -match '^(.*)Id$' -or $_.Name -match '^(Session)Key$' ) -and ! [string]::IsNullOrEmpty( $Matches[1] ) } | ForEach-Object `
{
$property = $_
if( ! [string]::IsNullOrEmpty( ( $id = ( $Matches[1] -replace '^Current' , '')) ))
{
if ( $table = $tables[ $id ] )
{
if( $property.Value -and ( $item = $table[ ($property.Value -as [string]) ]))
{
$datum.PSObject.properties.remove( $property )
$item.PSObject.Properties | ForEach-Object `
{
[pscustomobject]@{ "$id.$($_.Name)" = $_.Value }
if( $_.Name -ne $property.Name -and ( ! $previousProperties -or ! ( $previousProperties | Where-Object Name -eq $_.Name ))) ## don't lookup self or a key if it was one we previously looked up
{
Resolve-NestedProperties -properties $_ -previousProperties $properties
}
}
}
}
}
}
}
}
[hashtable]$params = @{ 'ErrorAction' = 'SilentlyContinue' }
[hashtable]$alreadyFetched = @{}
if( $PSBoundParameters[ 'username' ] )
{
if( ! $PSBoundParameters[ 'password' ] )
{
$password = $env:randomkey
}
if( ! [string]::IsNullOrEmpty( $password ) )
{
$credential = New-Object System.Management.Automation.PSCredential( $username , ( ConvertTo-SecureString -AsPlainText -String $password -Force ) )
}
else
{
Throw "Must specify password when using -username either via -password or %RandomKey%"
}
}
if( $credential )
{
$params.Add( 'Credential' , $credential )
}
else
{
$params.Add( 'UseDefaultCredentials' , $true )
}
## used to try and figure out the highest supported oData version but proved problematic
[int]$highestVersion = $oDataVersion ## if( $oDataVersion -le 0 ) { 10 } else { -1 }
$fatalException = $null
[int]$version = $oDataVersion
$startDate = $null
if( ! [string]::IsNullOrEmpty( $last ) )
{
## see what last character is as will tell us what units to work with
[int]$multiplier = 0
switch( $last[-1] )
{
"s" { $multiplier = 1 }
"m" { $multiplier = 60 }
"h" { $multiplier = 3600 }
"d" { $multiplier = 86400 }
"w" { $multiplier = 86400 * 7 }
"y" { $multiplier = 86400 * 365 }
default { Write-Error "Unknown multiplier `"$($last[-1])`"" ; Exit }
}
$endDate = [datetime]::Now
if( $last.Length -le 1 )
{
$from = $endDate.AddSeconds( -$multiplier )
}
else
{
$from = $endDate.AddSeconds( - ( ( $last.Substring( 0 ,$last.Length - 1 ) -as [int] ) * $multiplier ) )
}
}
$services = $null
## queries are case sensitive so help people who don't know this but don't do it for everything as would break items like DesktopGroups
if( $query -cmatch '^[a-z]' )
{
$TextInfo = (Get-Culture).TextInfo
$query = $TextInfo.ToTitleCase( $query ).ToString()
}
if( $PsCmdlet.ParameterSetName -eq 'cloud' )
{
if( ! $PSBoundParameters[ 'authtoken' ] )
{
Add-PSSnapin -Name Citrix.Sdk.Proxy.*
if( ! ( Get-Command -Name Get-XDAuthentication -ErrorAction SilentlyContinue ) )
{
Throw "Unable to find the Get-XDAuthentication cmdlet - is the Virtual Apps and Desktops Remote PowerShell SDK installed ?"
}
Get-XDAuthentication -CustomerId $customerid
if( ! $? )
{
Throw "Failed to get authentication token for Cloud customer id $customerid"
}
$authtoken = $GLOBAL:XDAuthToken
}
$params.Add( 'Headers' , @{ 'Customer' = $customerid ; 'Authorization' = $authtoken } )
$protocol = 'https'
}
[bool]$cloud = $false
[array]$data = @( do
{
if( $oDataVersion -le 0 )
{
## Figure out what the latest OData version supported is. Could get via remoting but remoting may not be enabled
if( $highestVersion -le 0 )
{
break
}
$version = $highestVersion--
}
if( $PsCmdlet.ParameterSetName -eq 'cloud' )
{
$params[ 'Uri' ] = ( "{0}://{1}.xendesktop.net/Citrix/Monitor/OData/v{2}/Data/{3}" -f $protocol , $customerid , $version , $query ) + (Get-DateRanges -query $query -from $from -to $to -oDataVersion $oDataVersion)
$cloud = $true
}
else
{
$params[ 'Uri' ] = ( "{0}://{1}/Citrix/Monitor/OData/v{2}/Data/{3}" -f $protocol , $ddc , $version , $query ) + (Get-DateRanges -query $query -from $from -to $to -oDataVersion $oDataVersion)
}
Write-Verbose "URL : $($params.Uri)"
try
{
if( ! $PSBoundParameters[ 'query' ] )
{
$services = Invoke-RestMethod @params
}
else
{
Invoke-RestMethod @params | Invoke-ODataTransform
}
$fatalException = $null
break ## since call succeeded so that we don't report for lower versions
}
catch
{
$fatalException = $_
}
} while ( $highestVersion -gt 0 ) )
if( $fatalException )
{
Throw $fatalException
}
if( $services )
{
if( $services.PSObject.Properties[ 'service' ] )
{
$services.service.workspace.collection | Select-Object -Property 'title' | Sort-Object -Property 'title'
}
else
{
$services.value | Sort-Object -Property 'name'
}
}
elseif( $data -and $data.Count )
{
if( $PSBoundParameters[ 'join' ] )
{
[hashtable]$tables = @{}
## now figure out what other tables we need in order to satisfy these ids (not interested in id on it's own)
$data[0].PSObject.Properties | Resolve-CrossReferences -cloud:$cloud
[int]$originalPropertyCount = $data[0].PSObject.Properties.GetEnumerator()|Measure-Object |Select-Object -ExpandProperty Count
[int]$finalPropertyCount = -1
## now we need to add these cross referenced items
ForEach( $datum in $data )
{
$datum.PSObject.Properties | Where-Object { $_.Name -ne 'sid' -and ( $_.Name -match '^(.*)Id$' -or $_.Name -match '^(Session)Key$' ) -and ! [string]::IsNullOrEmpty( $Matches[1] ) } | ForEach-Object `
{
$property = $_
Resolve-NestedProperties $property | ForEach-Object `
{
$_.PSObject.Properties | Where-Object MemberType -eq 'NoteProperty' | ForEach-Object `
{
Add-Member -InputObject $datum -MemberType NoteProperty -Name $_.Name -Value $_.Value
}
}
}
if( $finalPropertyCount -lt 0 )
{
$finalPropertyCount = $datum.PSObject.Properties.GetEnumerator()|Measure-Object |Select-Object -ExpandProperty Count
Write-Verbose -Message "Expanded from $originalPropertyCount properties to $finalPropertyCount"
}
$datum
}
}
}
else
{
Write-Warning "No data returned"
}