-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMobiControl-cmdlets.psm1
520 lines (367 loc) · 15.8 KB
/
MobiControl-cmdlets.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
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
function Set-TrustAllCertsPolicy {
if (([System.Net.ServicePointManager]::CertificatePolicy).ToString() -eq "System.Net.DefaultCertPolicy"){
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}
}
function Get-MCToken {
<#
.SYNOPSIS
Creates the access Token for the MobiControl API
.DESCRIPTION
Creates the access Token for the MobiControl API. There must e a local user in MobiControl and the CLientID and ClientSecret must be created on the MobiControl server with MobiControl Administration Utility (MCAdmin)
.PARAMETER MCUsername
Username of the local MobiControl account with administrator rights
.PARAMETER MCPassword
Password of the local MobiControl account
.PARAMETER MCFQDN
Full Qualified Domain Name of the MobiControl Server
.PARAMETER ClientID
ClientID Generated on the MobiControl Server with MobiControl Administration Utility (MCAdmin)
.PARAMETER ClientSecret
ClientSecret Generated on the MobiControl Server with MobiControl Administration Utility (MCAdmin)
.OUTPUTS
Access Token
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 10.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Get-MCToken.ps1 User Password MCServer
.EXAMPLE
Get-MCToken User Password MCServer
.EXAMPLE
Get-MCToken -MCUsername PowerShell -MCPassword Password -MCFQDN Servername
#>
Param (
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter MobiControl Username", Position = 0)]
[string]$MCUsername,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter MobiControl Password", Position = 1)]
[string]$MCPassword,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter MobiControl FQDN", Position = 2)]
[string]$MCFQDN,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter MobiControl FQDN", Position = 3)]
[string]$ClientID,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter MobiControl FQDN", Position = 4)]
[string]$ClientSecret
)
#Encoding of the client data
$IDSecret = $ClientID + ":" + $ClientSecret
$EncodedIDSecret = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($IDSecret))
#Body message
$Body = @{}
$Body["grant_type"]="password"
$Body["username"]=$MCUsername
$Body["password"]=$MCPassword
#Header message
$Header = @{}
$Header["Authorization"] = "Basic " + $EncodedIDSecret
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/token -Method POST -Headers $Header -Body $Body
$Token = $response.access_token
return $Token
}
function Get-MCDeviceGroup {
<#
.SYNOPSIS
Gets a single MobiControl device group
.DESCRIPTION
Gets a single MobiControl the device group
.PARAMETER Token
Create the token with the script or function Get-MCToken and pass it to this function to authenticate with the MobiControl server. Leave it blank and the function will be called to enter the details.
.PARAMETER Path
Specify the group
Without a backslash on the end
.OUTPUTS
Specified device group details
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 11.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Get-MCDeviceGroup.ps1 Token Path
.EXAMPLE
Get-MCDeviceGroup Token Path
.EXAMPLE
Get-MCDeviceGroup -Token jdfdönbvlkö34nlk -Path \\root\subgroup
#>
Param (
[parameter(valuefrompipeline = $true, HelpMessage = "Enter Authentication Token", Position = 0)]
[string]$Token,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter parentPath", Position = 1)]
[string]$Path
)
if($Token -eq ""){
$Token = Get-MCToken
}
$Header = @{}
$Header["Authorization"] = "Bearer " + $Token
#Double Encodes the Path
$EncodedPath = [System.Web.HttpUtility]::UrlEncode($Path)
$doubleEncodedPath = [System.Web.HttpUtility]::UrlEncode($EncodedPath)
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups/$doubleEncodedPath -ContentType "application/json" -Method GET -Headers $Header
return $response
}
function Get-MCDeviceGroups {
<#
.SYNOPSIS
Gets all MobiControl device groups or selected subgroups from parent folder
.DESCRIPTION
Gets all MobiControl the device groups if no path is specified. If a path of a parent groups is specified it gets the child groups.
.PARAMETER Token
Create the token with the script or function Get-MCToken and pass it to this function to authenticate with the MobiControl server. Leave it blank and the function will be called to enter the details.
.PARAMETER parentPath
Specify the parent group to show the subgroups
If not specified all device groups will be listed recursively
.OUTPUTS
all Device group or sub groups of the specified parent
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 10.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Get-MCDeviceGroups.ps1 Token Path
.EXAMPLE
Get-MCDeviceGroups Token Path
.EXAMPLE
Get-MCDeviceGroup -Token jdfdönbvlkö34nlk -Path \\root\subgroup
#>
Param (
[parameter(valuefrompipeline = $true, HelpMessage = "Enter Authentication Token", Position = 0)]
[string]$Token,
[parameter(valuefrompipeline = $true, HelpMessage = "Enter parentPath", Position = 1)]
[string]$parentPath
)
if($Token -eq ""){
$Token = Get-MCToken
}
$Header = @{}
$Header["Authorization"] = "Bearer " + $Token
if($parentPath -ne $null){
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups?parentPath=$parentpath -ContentType "application/json" -Method GET -Headers $Header
}
else{
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups -ContentType "application/json" -Method GET -Headers $Header
}
return $response
}
function Add-MCDeviceGroup {
<#
.SYNOPSIS
Adds a MobiControl device group
.DESCRIPTION
Adds a MobiControl device group
.PARAMETER Token
Create the token with the script or function Get-MCToken and pass it to this function to authenticate with the MobiControl server
.PARAMETER Name
Specify the name of the new group
.PARAMETER Path
Specify the path of the new group
.PARAMETER Icon
Specify what Icon the group should have in the web interface
.PARAMETER Kind
Specify the kind/type of the group
.OUTPUTS
Device group or groups
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 10.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Add-MCDeviceGroup.ps1 Token Name Path Icon Kind
.EXAMPLE
Add-MCDeviceGroup Token Name Path Icon Kind
.EXAMPLE
Add-MCDeviceGroup -Token jdfdönbvlkö34nlk -Name "Example1" -Path "\\root\subgroup\ -Icon None -Kind Regular
#>
Param (
[parameter(valuefrompipeline = $true, HelpMessage = "Enter Authentication Token", Position = 0)]
[string]$Token,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter group name", Position = 1)]
[string]$Name,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter Path", Position = 2)]
[string]$Path,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Choose Icon", Position = 3)]
[ValidateSet("None", "Yellow", "Red", "Green", "Blue", "Purple", "Cyan")]
[string]$Icon,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Choose groups kind/type", Position = 4)]
[ValidateSet("Regular", "Virtual")]
[string]$Kind
)
if($Token -eq ""){
$Token = Get-MCToken
}
#Header with Token Authorization
$Header = @{}
$Header["Authorization"] = "Bearer " + $Token
#JSON Request to the API
$Body = @{}
$Body["Name"]=$Name
$Body["Path"]=$Path+$Name
#Device Group Icon ['Yellow', 'Red', 'Green', 'Blue', 'Purple', 'Cyan', 'None']
$Body["Icon"]=$Icon
#Device group kind ['Regular', 'Virtual']
$Body["Kind"]=$Kind
#Convert hash table to JSON
$Body = $Body | ConvertTo-Json
#API Request
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups -ContentType "application/json" -Method POST -Headers $Header -Body $Body
return $response
}
function Remove-MCDeviceGroup {
<#
.SYNOPSIS
Removes a MobiControl device group
.DESCRIPTION
Removes a MobiControl device group
.PARAMETER Token
Create the token with the script or function Get-MCToken and pass it to this function to authenticate with the MobiControl server
.PARAMETER Name
Specify the name of the group that should be deleted
.PARAMETER Path
Specify the path of the parent group
.OUTPUTS
Device group or groups
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 10.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Add-MCDeviceGroup.ps1 Token Name Path Icon Kind
.EXAMPLE
Add-MCDeviceGroup Token Name Path Icon Kind
.EXAMPLE
Add-MCDeviceGroup -Token jdfdönbvlkö34nlk -Name "Example1" -Path "\\root\subgroup\ -Icon None -Kind Regular
#>
Param (
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter Authentication Token", Position = 0)]
[string]$Token,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter group name", Position = 1)]
[string]$Name,
[parameter(valuefrompipeline = $true, mandatory = $true, HelpMessage = "Enter Path", Position = 2)]
[string]$Path
)
if($Token -eq ""){
$Token = Get-MCToken
}
#Header with Token Authorization
$Header = @{}
$Header["Authorization"] = "Bearer " + $Token
#Concactinates the Path and Name
$Path=$Path+$Name
#Double Encodes the Path
$EncodedPath = [System.Web.HttpUtility]::UrlEncode($Path)
$doubleEncodedPath = [System.Web.HttpUtility]::UrlEncode($EncodedPath)
#API Request
Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/devicegroups/$doubleEncodedPath -ContentType "application/json" -Method DELETE -Headers $Header
return $response
}
function Get-MCProfiles {
<#
.SYNOPSIS
Gets a list of profiles
.DESCRIPTION
Gets a list of profiles
.PARAMETER Token
Create the token with the script or function Get-MCToken and pass it to this function to authenticate with the MobiControl server. Leave it blank and the function will be called to enter the details.
.PARAMETER nameContains
Only return profiles whose name contains this value
.PARAMETER withStatuses
Only return profiles that have statuses that match one of the values in this list. Provided as a comma-separated list of ProfileVersionStatus values.
.PARAMETER forFamilies
Only return profiles that are targeting one of the families in this list. Provided as a comma-separated list of DeviceFamily values
.PARAMETER hasDraft
Only return profiles that have a current draft. When false, only return profiles that do not have a draft. If null, then do not take draft status into account
.PARAMETER hasSchedule
Only return profiles that currently have a schedule. When false, only return profiles that do not have a schedule. If null, then do not take schedule status into account
.PARAMETER order
Determines the order. Example: +property1,-property2
.PARAMETER skip
Determines how many entities to skip
.PARAMETER take
Determines how many entities to take
.OUTPUTS
list of profiles
.NOTES
Version: 1.0
Author: Noah Li Wan Po
Creation Date: 24.06.2020
Purpose/Change: Initial function development
.EXAMPLE
.\Get-MCProfiles.ps1
.EXAMPLE
Get-MCProfiles
.EXAMPLE
Get-MCProfiles
#>
Param (
[parameter(valuefrompipeline = $true, HelpMessage = "Enter Authentication Token", Position = 0)]
[string]$Token,
[parameter(valuefrompipeline = $true, HelpMessage = "Enter a string which the name should contain", Position = 1)]
[string]$nameContains,
[parameter(valuefrompipeline = $true, HelpMessage = "Enter the status of the profiles to be shown", Position = 2)]
[ValidateSet("Disabled", "Assigned", "Draft")]
[string]$withStatuses,
[ValidateSet("AndroidWork", "AndroidPlus")]
[parameter(valuefrompipeline = $true, HelpMessage = "Enter the family of the profiles to be shown", Position = 3)]
[string]$forFamilies,
[parameter(valuefrompipeline = $true, HelpMessage = "Show only profiles with a draft", Position = 4)]
[string]$hasDraft,
[parameter(valuefrompipeline = $true, HelpMessage = "Show only profiles with a shedule", Position = 5)]
[string]$hasSchedule,
[parameter(valuefrompipeline = $true, HelpMessage = "Show only profiles with a autoinstall", Position = 6)]
[string]$autoInstallOnly,
[parameter(valuefrompipeline = $true, HelpMessage = "Determines the order. Example: +property1,-property2", Position = 7)]
[string]$order,
[parameter(valuefrompipeline = $true, HelpMessage = "Determines how many entities to skip", Position = 8)]
[string]$skip,
[parameter(valuefrompipeline = $true, HelpMessage = "Determines how many entities to take", Position = 9)]
[string]$take
)
if($Token -eq ""){
$Token = Get-MCToken
}
$Header = @{}
$Header["Authorization"] = "Bearer " + $Token
#hashtable of all variables
$hash = @{}
$hash["nameContains"]=$nameContains
$hash["withStatuses"]=$withStatuses
$hash["forFamilies"]=$forFamilies
$hash["hasDraft"]=$hasDraft
$hash["hasSchedule"]=$hasSchedule
$hash["autoInstallOnly"]=$autoInstallOnly
$hash["order"]=$order
$hash["skip"]=$skip
$hash["take"]=$take
#hashtable for the API request
$body = @{}
#gets rid of emtpy values
foreach($line in $hash.GetEnumerator()){
if($line.Value -ne ""){
$body["$($line.name)"]=$line.value
}
}
#API Request
if($body.count -eq 0){
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/profiles -ContentType "application/json" -Method GET -Headers $Header
}
else{
$response = Invoke-restmethod -Uri https://$MCFQDN/mobicontrol/api/profiles -ContentType "application/json" -Method GET -Headers $Header -Body $Body
}
return $response
}