-
Notifications
You must be signed in to change notification settings - Fork 0
/
SyncplifyServer.psm1
514 lines (380 loc) · 14.7 KB
/
SyncplifyServer.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
function Connect-Syncplify {
<#
.SYNOPSIS
Used to establish an initial connection to the Syncplify REST API
.EXAMPLE
PS>>Connect-Syncplify -Server SyncplifyServer01.MyDomain.Com -Port 4443
.PARAMETER Server
Name of server to connect to
.PARAMETER Username
Username with access to API
.PARAMETER Password
Password for specified username
#>
[CmdletBinding()]
param(
[Parameter(mandatory)]
[ValidateScript({
if (-not (test-Connection $_ -quiet -count 1))
{
throw "The server [$_] is offline. Try again."
}
else
{
$true
}
})]
[string]$Server,
[string]$Port = '4443',
[string]$VirtualServer = 'default',
[string]$User,
[string]$Password
)
## Declares the global variable to store the url of the server to be authenticated with
$global:url = "https://$($server):$($port)/smserver-$($VirtualServer)"
## Checks if the username or password is not present and prompt for secure credentials
if ([string]::IsNullOrEmpty($User) -or [string]::IsNullOrEmpty($Password)){
$credentials = (get-credential)
$user = $credentials.UserName
$password = $credentials.GetNetworkCredential().Password
}
## Concatenates the username and password into a base64 encoded string
$userpass = $user+":"+$Password
$bytes = [System.Text.Encoding]::UTF8.GetBytes($userpass)
$encoded = [system.convert]::ToBase64String($bytes)
## Defines the header section of the API call
$headers = @{
Authorization = "Basic $encoded"
}
## Begins the main call to the server with supplied information
try {
## Sends auth request to the server and stores the result in the variable $SyncplifyAuthResult for use by other functions
Write-Verbose -Message "Invoke-RestMethod -Method get -Uri $($url)/auth -ContentType application/json -Headers $($headers)"
$global:SyncplifyAuthResult = Invoke-RestMethod -Method get -Uri $url"/auth" -ContentType "application/json" -Headers $headers
} ## End try
catch {
## Checks to see if the authentication failed due to incorrect connection details
if ( $($_.Exception.Message) -match "400") { Write-Error -Message "Failed to authenticate with $server. Please verify connection details and try again." } else { $_.Exception.Message }
## If authentication fails the function stops processing
return
} ## End Catch
## Upon successful connection, the following is written to the host
Write-Host -BackgroundColor DarkGreen "Connected to Syncplify Server: $url"
} ## End function Connect-Syncplify
function Disconnect-Syncplify {
<#
.SYNOPSIS
Used to disconnect from the Syncplify REST API
.EXAMPLE
PS>>Disconnect-Syncplify
#>
[CmdletBinding()]
param()
## Begin processing
try {
## Sends disconnect request to $url
Invoke-RestMethod -Method get -Uri $url"/sms.Disconnect" -ContentType "application/json" -Headers @{"Authorization" = "Bearer $($SyncplifyAuthResult.access_token)"}
} catch {
## Checks to see if there were no active connections to $url
if ($($_.Exception.Message) -match "403") { Write-Error -Message "There is not an active connection to $Url" }
## If error is cought the function stops processing
return
}
## Upon successful disconnection, the following is written to the host
Write-Host -BackgroundColor DarkGreen "Disconnected from $url"
} ## End function Disconnect-Syncplify
function Get-SyncplifyConfig {
<#
.SYNOPSIS
Used to retrieve the full configuration of the Syncplify server
.EXAMPLE
PS>>Get-SyncplifyConfig
#>
[CmdletBinding()]
param()
## Declares the $body variable as JSON array containing a single empty object
$body = "[{}]"
## Begin processing
try {
## Send POST to $url
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.LoadConf" -ContentType "application/json" -body $body -Headers @{"Authorization" = "Bearer $($SyncplifyAuthResult.access_token)"}
}
## Checks for any errors
catch {
## If error 403 received, the following is written to the host
if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
## Stops the processing
return
}
## Returns the config
return $Result[0].Result
} ## End function Get-SyncplifyConfig
function Set-SyncplifyConfig {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateScript({ test-path -Path $_ })]
[string]$Path
)
$body = Get-Content -Path $Path | ConvertFrom-Json
$Body = ConvertTo-Json -InputObject @($body)
try {
$result = Invoke-RestMethod -Method POST -uri $url"/sms.SaveConf" -ContentType "application/json" -body $Body -Headers @{"Authorization" = "Bearer $($SyncplifyAuthResult.access_token)"}
} catch {
## If error 403 received, the following is written to the host
if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
## Stops the processing
return
}
Write-Host -BackgroundColor DarkGreen "Configuration has been saved"
} ## End function Set-SyncplifyConfig
function Get-SyncplifyUser {
<#
.SYNOPSIS
Used to retrieve a user or all users from the Syncplify Server
.EXAMPLE
PS>>Get-SyncplifyUser -User '[email protected]'
.EXAMPLE
PS>>Get-SyncplifyUser -All
.PARAMETER User
Username of user to retrieve
.PARAMETER All
Switch used to retrieve all users. Cannot be used in conjunction with the 'User' parameter
#>
[CmdletBinding()]
param(
[Parameter(ParameterSetName='SingleUser')]
[string]$User,
[Parameter(ParameterSetName='AllUsers')]
[switch]$All
)
## Declares the header section
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
try {
## If the -All parameter is used:
if ($PSCmdlet.ParameterSetName -eq 'AllUsers') {
$body = "[{}]"
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.ReadUserList" -ContentType "application/json" -body $body -Headers $headers
}
## If the -User parameter is used:
if ($PSCmdlet.ParameterSetName -eq 'SingleUser') {
$body = ,@{AUName = "$user"}
$body = ConvertTo-Json -InputObject @($body)
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.LoadUser" -ContentType "application/json" -body $body -Headers $headers
}
} catch {
## Checks for errors
if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
## Stops processing
return
}
## Returns the result of the request
return $Result = New-Object PSObject -Verbose $Result[0].Result
} ## End function Get-SyncplifyUser
function Get-SyncplifyVFS {
<#
.SYNOPSIS
Used to retrieve VFS info from the Syncplify server
.EXAMPLE
PS>>Get-SyncplifyVFS -All
.EXAMPLE
PS>>Get-SyncplifyVFS -VFS
.PARAMETER Server
Name of server to connect to
.PARAMETER Username
Username with access to API
.PARAMETER Password
Password for specified username
#>
[CmdletBinding()]
param(
[parameter(ParameterSetName='AllVFS')]
[switch]$All,
[Parameter(ParameterSetName='SingleVFS')]
[String]$VFS
)
## Defines header section
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
try{
## If -All switch is used:
if ($PSCmdlet.ParameterSetName -eq 'AllVFS') {
## Defines body as a JSON array with a single empty item
$body = "[{}]"
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.readVFSlist" -body $body -ContentType "application/json" -Headers $headers
}
## If -SingleVFS is used:
if ($PSCmdlet.ParameterSetName -eq 'SingleVFS') {
## Defines body as a JSON array containing the vfs id
$Body = ,@{Aitem = "$vfs"}
$body = ConvertTo-Json -InputObject @($body)
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.loadVFS" -body $body -ContentType "application/json" -Headers $headers
}
} catch {
## Checks for errors
if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
else {$_.Exception.Message}
## Stops processing
return
}
return $Result = New-Object PSObject -Verbose $Result[0].Result
} ## End function Get-SyncplifyVFS
function Remove-SyncplifyUser {
Param (
[Parameter(Mandatory)]
[string]$User
)
## Define header section
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
## Beging processing
try {
## Define body as a single JSON array containin the Aitem object
$body = ,@{AUName = "$user"}
## Converts the Body to JSON
$Body = ConvertTo-Json -InputObject @($body)
## Sends request to server
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.DeleteUser" -ContentType "application/json" -body $Body -Headers $headers
} catch {
## Checks for auth errors
if ($_.Exception.Message -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
## Stop processing
return
}
## Checks to see if the result is equal to 1 (Success)
if ($Result[1].Result -eq 1) { Write-Host -BackgroundColor DarkGreen "Successfully deleted $user" }
else {Write-Error "Error deleting $user. Does this user exist?" }
} ## End function Delete-SyncplifyUser
function Get-SyncplifyPassUtil {
<#
.SYNOPSIS
Used to generate and verify passwords
.EXAMPLE
PS>>$generated = Get-SyncplifyPassUtil -Password '123' -Command Generate
.EXAMPLE
PS>>Get-SyncplifyPassUtil -salt $generated.salt -PassHash $generated.PassHash -Password '123' -Command Verify
.PARAMETER Password
Password to generate a hash for or verify
.PARAMETER Command
Generate or Verify
.PARAMETER Salt
Salst of password
.PARAMETER PassHash
Hashed password
.PARAMETER VServer
Virtual server to verify password against
#>
[CmdletBinding(DefaultParameterSetName = 'None')]
param(
[parameter(mandatory)]
[string]$Password,
[parameter(mandatory)]
[ValidateSet('Generate','Verify')]
[string]$Command,
[Parameter(ParameterSetName = 'Verify')]
[string]$salt,
[Parameter(ParameterSetName = 'Verify')]
[string]$PassHash,
[Parameter(ParameterSetName = 'Verify')]
[string]$VServer = 'default'
)
## Define header section
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
## Start processing
try {
## Switch based on Command parameter
switch ($Command) {
'Generate' {
$body = ,@{command = "generate";password = "$Password"}
$body = ConvertTo-Json -InputObject @($body)
$global:hash = Invoke-RestMethod -Method POST -uri $url"/sms.PassUtil" -ContentType "application/json" -body $Body -Headers $headers
return $hash[0].result
}
'Verify' {
$body = ,@{command = "verify";password = "$Password";salt = "$Salt";PassHash = "$PassHash";Vserver = "$VServer"}
$body = ConvertTo-Json @($body)
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.PassUtil" -ContentType "application/json" -body $Body -Headers $headers
}
}
} catch {if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
}
return Write-Host -BackgroundColor DarkGreen "$($Result[0].Result.ResultMsg)"
} ## End function Get-SyncplifyPassUtil
function Get-SyncplifyNode {
[CmdletBinding()]
param()
## Define header section
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
## Start processing
try {
$body = ,@{}
$body = ConvertTo-Json -InputObject @($body)
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.ReadNodeList" -ContentType "application/json" -body $Body -Headers $headers
}
## Catch any errors
catch {if ($_.Exception.MEssage -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}
}
## Return result
return $Result = New-Object PSObject -Verbose $Result[0].Result
} ## End function Get-SyncplifyNode
function Get-SyncplifySessions {
[CmdletBinding()]
param(
[switch]$All,
[String]$NodeID
)
$headers = @{
Authorization = "Bearer $($SyncplifyAuthResult.access_token)"
Accept = "*/*"
}
## Begin processing
try {
## Declares the JSON array to be used as the body
$nodelist = @"
[
{
"nodes": [
]
}
]
"@ | Convertfrom-json
## Retrieves a list of current nodes for the virtual server
$nodes = Get-SyncplifyNode
## For each node retrieved it will be added to the $Nodelist JSON array
foreach ($node in $nodes[0]) {
$nodelist[0].nodes += $node._id
}
## Converts the array to JSON
$body = ConvertTo-Json @($nodelist)
## Sends the API request to the server
$Result = Invoke-RestMethod -Method POST -uri $url"/sms.GetSession" -ContentType "application/json" -body $body -Headers $headers
}
## Checks for errors
catch {if ($_.Exception.Message -match "403") {Write-Error "ACCESS DENIED: You must authenticate with the Syncplify server before proceeding."}}
## Returns a list of active sessions
return $Result = New-Object PSObject -Verbose $Result[0].Result
} ## End function Get-SyncplifySessions
## ALIASES
New-Alias -Name Connect-SM -Value Connect-Syncplify
New-Alias -Name Disconnect-SM -Value Disconnect-Syncplify
New-Alias -Name Get-SMConfig -Value Get-SyncplifyConfig
New-Alias -Name Set-SMConfig -Value Set-SyncplifyConfig
New-Alias -Name Get-SMUser -Value Get-SyncplifyUser
New-Alias -Name Get-SMVFS -Value Get-SyncplifyVFS
New-Alias -Name Remove-SMUser -Value Remove-SyncplifyUser
New-Alias -Name Get-SMPassUtil -Value Connect-Syncplify
New-Alias -Name Get-SMNode -Value Connect-Syncplify
New-Alias -Name Get-SMSessions -Value Get-SyncplifySessions