-
Notifications
You must be signed in to change notification settings - Fork 0
/
Synology-PS.ps1
1152 lines (955 loc) · 38.2 KB
/
Synology-PS.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
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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Synology-PS: Synology API For PowerShell
# © C:Amie 2024 - https://www.c-amie.co.uk/
# If this was useful to you, feel free to buy me a coffee at https://www.c-amie.co.uk/
#
# Include this file in your own script via:
# . .\Synology-PS.ps1
# -or-
# . C:\Folder\Folder\Synology-PS.ps1
#
# API Guide
# https://cndl.synology.cn/download/Document/Software/DeveloperGuide/Package/FileStation/All/enu/Synology_File_Station_API_Guide.pdf
# https://kb.synology.com/en-us/DG/DSM_Login_Web_API_Guide/2
# Windows 10 and lower do not support TLS 1.3 connections via PowerShell. Ensure that TLS 1.2 or lower are enabled in DSM
# https://kb.synology.com/en-global/DSM/help/DSM/AdminCenter/connection_security_advanced?version=7
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;
}
}
"@
<#
.SYNOPSIS
Generate a random string using URI legal ASCII characters
.DESCRIPTION
Generate a random string using URI legal ASCII characters
.PARAMETER Length
Required. The number of characters in the string
.EXAMPLE
PS> Synology-GenerateRandom -Length 5
a4*gU
#>
function Synology-GenerateRandom {
param(
[Parameter(Mandatory=$true)]
[int]$Length
)
$TokenSet = @{
U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
L = [Char[]]'abcdefghijklmnopqrstuvwxyz'
N = [Char[]]'0123456789'
S = [Char[]]'-_~!$&()*'
}
$Upper = Get-Random -Count 5 -InputObject $TokenSet.U
$Lower = Get-Random -Count 5 -InputObject $TokenSet.L
$Number = Get-Random -Count 5 -InputObject $TokenSet.N
$Special = Get-Random -Count 5 -InputObject $TokenSet.S
$StringSet = $Upper + $Lower + $Number + $Special
(Get-Random -Count 15 -InputObject $StringSet) -join ''
}
<#
.SYNOPSIS
Sets the TLS/SSL policy for the current PowerShell session
.DESCRIPTION
Sets the TLS/SSL policy for the current PowerShell session. This should match (or at least include) the TLS setting specified
under Control Panel > Security > Advanced > TLS / SSL Profile Level on the NAS
.PARAMETER Ssl30
Default = $false. Enable SSL 3.0
.PARAMETER Tls10
Default = $false. Enable TLS 1.0
.PARAMETER Tls11
Default = $false. Enable TLS 1.1
.PARAMETER Tls12
Default = $true. Enable TLS 1.2
.PARAMETER Tls13
Default = $true. Enable TLS 1.3
.EXAMPLE
PS> Synology-SetSessionTlsPolicy -Tls12 $true -Tls13 $true
#>
function Synology-SetSessionTlsPolicy {
Param (
[Parameter(Mandatory=$false)]
[bool]$Ssl30=$false,
[Parameter(Mandatory=$false)]
[bool]$Tls10=$false,
[Parameter(Mandatory=$false)]
[bool]$Tls11=$false,
[Parameter(Mandatory=$false)]
[bool]$Tls12=$true,
[Parameter(Mandatory=$false)]
[bool]$Tls13=$true
)
if (-Not $($Tls10 -bor $Tls11 -bor $Tls12 -bor $Tls13)) {
Throw "Invalid TLS Configuration Specified"
}
$level = $null
if ($Ssl30) {
$level = $level -bor [System.Net.SecurityProtocolType]::Ssl3
}
if ($Tls10) {
$level = $level -bor [System.Net.SecurityProtocolType]::Tls #[System.Net.SecurityProtocolType]::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3;
}
if ($Tls11) {
$level = $level -bor [System.Net.SecurityProtocolType]::Tls11
}
if ($Tls12) {
$level = $level -bor [System.Net.SecurityProtocolType]::Tls12
}
if ($Tls13) {
$level = $level -bor [System.Net.SecurityProtocolType]::Tls13
}
[System.Net.ServicePointManager]::SecurityProtocol = $level
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
}
<#
.SYNOPSIS
Attempts to wake the NAS by sending a Wake-on-LAN Magic Packet
.DESCRIPTION
Fires a Wake-on-LAN packet into the current system broadcast domain formatted as a standard WoL Magic Packet
.PARAMETER MacAddress
Required. A hexadecimal MAC Address formatted as xx:xx:xx:xx:xx:xx or xx-xx-xx-xx-xx-xx
.EXAMPLE
PS> Synology-WakeOnLan -MacAddress 'xx:xx:xx:xx:xx:xx'
#>
function Synology-WakeOnLan {
Param (
[Parameter(Mandatory=$true)]
[string]$MacAddress
)
$MacByteArray = $MacAddress -split "[:-]";
[Byte[]] $MagicPacket = (,0xFF * 6)
for ($i = 0; $i -lt 16; $i++) {
ForEach ($strHex in $MacByteArray) {
$byte = [Byte] "0x$strHex";
[Byte[]] $MagicPacket += $byte;
}
}
$UdpClient = New-Object System.Net.Sockets.UdpClient
$UdpClient.Connect(([System.Net.IPAddress]::Broadcast),7)
$UdpClient.Send($MagicPacket,$MagicPacket.Length) | out-null
$UdpClient.Close()
}
<#
.SYNOPSIS
Tests to see if the Synology Web Service is online
.DESCRIPTION
Tests to see if the Synology Web Service is online
.PARAMETER UseHttps
Default = $true. Whether the request should be made using http ($false) or https ($true)
.PARAMETER Hostname
Required. The FQDN or IP Address of the NAS
.PARAMETER Port
Required. The TCP Port to connect to e.g. 443 or 5001
.OUTPUTS
Boolean. $true if the service responsed, $false if it did not
.EXAMPLE
PS> Synology-TestConnection -UseHttps $true -Hostname 'mynas.mydomain.com' -Port 5001
False
#>
function Synology-TestConnection {
Param (
[Parameter(Mandatory=$false)]
$UseHttps=$true,
[Parameter(Mandatory=$true)]
[string]$Hostname,
[Parameter(Mandatory=$true)]
[int]$Port
)
$url = "http$(if($UseHttps) { 's' } else { '' })://$($Hostname):$($Port)"
$url
try {
#[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12 # -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3;
#[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$response = Invoke-WebRequest -Uri $url -Method GET
return $($response.StatusCode -eq 200)
} catch {
return $false
}
}
<#
.SYNOPSIS
Returns a description for a Synology Error Code
.DESCRIPTION
Returns a description for a Synology API Error Code. Note: These error numbers should not be confused with HTTP Status Codes.
.PARAMETER ErrorCode
Required. A numeric error number
.OUTPUTS
String. An expanded error message description
.EXAMPLE
PS> Synology-LookupError -ErrorCode 107
Session interrupted by duplicate login
#>
function Synology-LookupError {
Param (
[Parameter(Mandatory=$true)]
[int]$ErrorCode
)
$dictErrors = @{
100 = "Unknown error"
101 = "No parameter of API, method or version"
102 = "The requested API does not exist"
103 = "The requested method does not exist"
104 = "The requested version does not support the functionality"
105 = "The logged in session does not have permission"
106 = "Session timeout"
107 = "Session interrupted by duplicate login"
119 = "SID not found"
400 = "Invalid parameter of file operation"
401 = "Unknown error of file operation"
402 = "System is too busy"
403 = "Invalid user does this file operation"
404 = "Invalid group does this file operation"
405 = "Invalid user and group does this file operation"
406 = "Can't get user/group information from the account server"
407 = "Operation not permitted"
408 = "No such file or directory"
409 = "Non-supported file system"
410 = "Failed to connect internet-based file system (e.g., CIFS)"
411 = "Read-only file system"
412 = "Filename too long in the non-encrypted file system"
413 = "Filename too long in the encrypted file system"
414 = "File already exists"
415 = "Disk quota exceeded"
416 = "No space left on device"
417 = "Input/OutputFormat error"
418 = "Illegal name or path"
419 = "Illegal file name"
420 = "Illegal file name on FAT file system"
421 = "Device or resource busy"
599 = "No such task of the file operation"
900 = "Failed to delete file(s)/folder(s). More information in <errors> object."
1200 = "Failed to rename it. More information in <errors> object."
1800 = "There is no Content-Length information in the HTTP header or the received size doesn't match the value of Content-Length information in the HTTP header."
1801 = "Wait too long, no date can be received from client (Default maximum wait time is 3600 seconds)."
1802 = "No filename information in the last part of file content."
1803 = "Upload connection is cancelled."
1804 = "Failed to upload oversized file to FAT file system."
1805 = "Can't overwrite or skip the existing file, if no overwrite parameter is given."
}
$return = $dictErrors[$ErrorCode]
if (-Not $return) {
$return = "Unknown error code"
}
$return
}
<#
.SYNOPSIS
Parses a shared folder path
.DESCRIPTION
Parses a shared folder path to ensure that the share/folder path does not end in a /
.PARAMETER Path
Required. A path in the format of /share/{folder}
.OUTPUTS
String. Any trailing / will be dropped from the input
.EXAMPLE
PS> Synology-ParseFolderPath -Path "/share/folder/"
/share/folder
#>
function Synology-ParseFolderPath {
Param (
[Parameter(Mandatory=$true)]
[string]$Path
)
while ($Path[$Path.Length-1] -eq '/') {
$Path = $Path.SubString(0, [math]::min($Path.Length-1,$Path.length))
}
return $Path
}
<#
.SYNOPSIS
Log-on a user and return an Authenticaion Token instance
.DESCRIPTION
Log-on a user and return an Authenticaion Token instance that can be used to make API calls
.PARAMETER UseHttps
Default = $true. Whether the request should be made using http ($false) or https ($true)
.PARAMETER Hostname
Required. The FQDN or IP Address of the NAS
.PARAMETER Port
Required. The TCP Port to connect to e.g. 443 or 5001
.PARAMETER Username
Required. The username of the logging on user
.PARAMETER Password
Required. The clear-text password of the logging on user
.OUTPUTS
An authentication token structure containing the session data necessary to make use of the PowerShell API
.EXAMPLE
PS> $authToken = Synology-Login -Hostname "mynas.mydomain.com" -Port 5001 -UseHttps $true -Username "admin" -Password "secret"
#>
function Synology-Login {
Param (
[Parameter(Mandatory=$false)]
[boolean]$UseHttps=$true,
[Parameter(Mandatory=$true)]
[string]$Hostname,
[Parameter(Mandatory=$true)]
[int]$Port,
[Parameter(Mandatory=$true)]
[string]$Username,
[Parameter(Mandatory=$true)]
[string]$Password
)
$sessionName = Synology-GenerateRandom -Length 16
$url = "http$(if($UseHttps) { 's' } else { '' })://$($Hostname):$Port/webapi/auth.cgi?api=SYNO.API.Auth&version=3&method=login&account=$Username&passwd=$Password&session=$sessionName&format=sid" # or cookie
$dictHeaders = @{
"Content-Type" = "application/x-www-form-urlencoded"
}
#[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12 # -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls -bor [System.Net.SecurityProtocolType]::Ssl3;
#[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$response = Invoke-WebRequest -Headers $dictHeaders -Uri $url -Method Get # Can use -SkipCertificateCheck in PS6+
$json = $response.Content | Out-String | ConvertFrom-Json
$return = [PSCustomObject]@{
PSTypeName = "SynologyAuthToken"
Success = $json.success
Did = $json.data.did
Sid = $json.data.sid
SessionName = $sessionName
UseHttps = $UseHttps
Hostname = $Hostname
Port = $Port
}
$return
}
<#
.SYNOPSIS
End the current PowerShell AuthToken by logging off
.DESCRIPTION
Notifies the NAS to destroy the current PowerShell users security context
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.EXAMPLE
PS> Synology-Logout -AuthToken $authToken
#>
function Synology-Logout {
Param (
[Parameter(Mandatory=$true)]
$AuthToken
)
if (-Not $AuthToken) {
return
}
Synology-InvokeMethod -AuthToken $authToken -Target Auth -API "SYNO.API.Auth" -Method "logout" -Parameters @{"session" = $AuthToken.SessionName}
}
<#
.SYNOPSIS
Turns off the NAS
.DESCRIPTION
Unconditionally signals the NAS to shutdown. It is advised to issue an immediate log-out following running this command.
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.EXAMPLE
PS> Synology-Shutdown -AuthToken $authToken
#>
function Synology-Shutdown {
Param (
[Parameter(Mandatory=$true)]
$AuthToken
)
if (-Not $AuthToken) {
return
}
Synology-InvokeMethod -AuthToken $authToken -Target Auth -API "SYNO.Core.System" -Method "shutdown"
}
<#
.SYNOPSIS
Reboots the NAS
.DESCRIPTION
Unconditionally signals the NAS to restart. It is advised to issue an immediate log-out following running this command.
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.EXAMPLE
PS> Synology-Restart -AuthToken $authToken
#>
function Synology-Restart {
Param (
[Parameter(Mandatory=$true)]
$AuthToken
)
if (-Not $AuthToken) {
return
}
Synology-InvokeMethod -AuthToken $authToken -Target Auth -API "SYNO.Core.System" -Method "reboot"
}
<#
.SYNOPSIS
Returns a list of public API call namespaces available on the NAS
.DESCRIPTION
Returns a list of public API call namespaces available on the NAS. Note, this does not return 'private' API's such as SYNO.Core.xxx
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Query
Default = 'all'. API names, separated by a comma "," or use "all" to get all supported APIs.
.OUTPUTS
JSON representation of the NAS response.
.EXAMPLE
PS> $api = Synology-ListApi -AuthToken $authToken
#>
function Synology-ListApi {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$false)]
[string]$Query='all'
)
if (-Not $AuthToken) {
return
}
return Synology-InvokeMethod -AuthToken $authToken -API 'SYNO.API.Info' -Method 'query' -Parameters @{"query" = $Query} -Return $true -OutputFormat Json
}
<#
.SYNOPSIS
Returns a list of shares available on the NAS
.DESCRIPTION
Returns a list of the shares available on the NAS
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Offset
Optional. Specify how many shared folders are skipped before beginning to return listed shared folders.
.PARAMETER Limit
Optional. Number of shared folders requested. 0 lists all shared folders.
.PARAMETER SortBy
Optional. Specify which file information to sort on [name, user, group, mtime, atime, ctime, crtime or posix].
.PARAMETER SortDirection
Optional. Specify to sort ascending or to sort descending [asc or desc].
.PARAMETER OnlyWritable
Optional.
true : List writable shared folders.
false : List writable and read-only shared folders.
.PARAMETER Additional
Optional. Request additional data in the return response [real_path, owner, time, perm, mount_point_type, sync_share, or volume_status]
.OUTPUTS
JSON representation of the NAS response.
.EXAMPLE
PS> $shares = Synology-ListShares -AuthToken $authToken -OnlyWritable $true -SortBy ctime
#>
function Synology-ListShares {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$false)]
[int]$Offset,
[Parameter(Mandatory=$false)]
[int]$Limit,
[Parameter(Mandatory=$false)]
[ValidateSet('name', 'user', 'group', 'mtime', 'atime', 'ctime', 'crtime', 'posix', IgnoreCase = $false)]
[string]$SortBy,
[Parameter(Mandatory=$false)]
[ValidateSet('asc', 'desc', IgnoreCase = $false)]
[string]$SortDirection='asc',
[Parameter(Mandatory=$false)]
[boolean]$OnlyWritable=$false,
[Parameter(Mandatory=$false)]
[ValidateSet('real_path', 'size', 'owner', 'time', 'perm', 'mount_point_type', 'volume_status', IgnoreCase = $false)]
[string]$Additional
)
if (-Not $AuthToken) {
return
}
$parameters = @{}
if ($Offset -is [int]) {
$parameters.Set_Item("offset", $Offset)
}
if ($Limit -is [int]) {
$parameters.Set_Item("limit", $Limit)
}
if ($SortBy) {
$parameters.Set_Item("sort_by", $SortBy)
}
if ($SortDirection -in "asc", "desc") {
$parameters.Set_Item("sort_direction", $SortDirection)
}
if ($OnlyWritable -is [boolean]) {
$parameters.Set_Item("onlywritable", $([string]$OnlyWritable).ToLower())
}
if ($Additional) {
$parameters.Set_Item("additional", $Additional)
}
return Synology-InvokeMethod -AuthToken $authToken -API 'SYNO.FileStation.List' -Method 'list_share' -Version 2 -Parameters $parameters -Return $true -OutputFormat Json
}
<#
.SYNOPSIS
Returns a list of files/folders at a given path
.DESCRIPTION
Returns a list from the NAS of files/folders at the given path
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER FolderPath
Required. The path on the NAS in which to return the contents list in the format of /share/folder{/folder}
.PARAMETER Offset
Optional. Optional. Specify how many files are skipped before beginning to return listed files.
.PARAMETER Limit
Optional. Number of files requested. 0 indicates to list all files with a given folder.
.PARAMETER SortBy
Optional. Specify which file information to sort on [name, size, user, group, mtime, atime, ctime, crtime, posix or type].
.PARAMETER SortDirection
Optional. Specify to sort ascending or to sort descending [asc or desc].
.PARAMETER Pattern
Optional. Given glob pattern(s) to find files whose names and extensions match a case-insensitive glob pattern.
Note:
1. If the pattern doesn't contain any glob syntax (? and *), * of glob syntax will be added at begin and end
of the string automatically for partially matching the pattern.
2. You can use "," to separate multiple glob patterns.**
.PARAMETER FileType
Optional. "file": only enumerate regular files; "dir": only enumerate folders; "all" enumerate regular files and folders.
.PARAMETER GoToPath
Optional. Folder path starting with a shared folder. Return all files and sub-folders within folder_path path until goto_path path recursively.
Note: GoToPath is only valid with parameter "Additional" contains real_path.
.PARAMETER Additional
Optional. Request additional data in the return response [real_path, size, owner, time, perm, type or mount_point_type]
.OUTPUTS
JSON representation of the NAS response.
.EXAMPLE
PS> $files = Synology-ListFiles -AuthToken $authToken -FolderPath '/share'
#>
function Synology-ListFiles {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$FolderPath,
[Parameter(Mandatory=$false)]
[int]$Offset,
[Parameter(Mandatory=$false)]
[int]$Limit,
[Parameter(Mandatory=$false)]
[ValidateSet('name', 'size', 'user', 'group', 'mtime', 'atime', 'ctime', 'crtime', 'posix', 'type', IgnoreCase = $false)]
[string]$SortBy,
[Parameter(Mandatory=$false)]
[ValidateSet('asc', 'desc', IgnoreCase = $false)]
[string]$SortDirection='asc',
[Parameter(Mandatory=$false)]
[string]$Pattern,
[Parameter(Mandatory=$false)]
[string]$FileType,
[Parameter(Mandatory=$false)]
[string]$GoToPath,
[Parameter(Mandatory=$false)]
[ValidateSet('real_path', 'size', 'owner', 'time', 'perm', 'mount_point_type', 'type', IgnoreCase = $false)]
[string]$Additional
)
if (-Not $AuthToken) {
return
}
$parameters = @{}
$parameters.Set_Item("folder_path", $(Synology-ParseFolderPath -Path $FolderPath))
if ($Offset -is [int]) {
$parameters.Set_Item("offset", $Offset)
}
if ($Limit -is [int]) {
$parameters.Set_Item("limit", $Limit)
}
if ($SortBy) {
$parameters.Set_Item("sort_by", $SortBy)
}
if ($SortDirection -in "asc", "desc") {
$parameters.Set_Item("sort_direction", $SortDirection)
}
if ($Pattern) {
$parameters.Set_Item("pattern", $Pattern)
}
if ($FileType) {
$parameters.Set_Item("filetype", $FileType)
}
if ($GoToPath) {
$parameters.Set_Item("goto_path", $GoToPath)
}
if ($Additional) {
$parameters.Set_Item("additional", $Additional)
}
return Synology-InvokeMethod -AuthToken $authToken -API 'SYNO.FileStation.List' -Method 'list' -Version 2 -Parameters $parameters -Return $true -OutputFormat Json
}
<#
.SYNOPSIS
Download a file from the NAS
.DESCRIPTION
Downloads a file on the NAS to the local computer
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Path
Required. The path on the NAS of the file to be downloaded e.g. /share/folder/folder/MyFile.txt or /share/folder/folder
If /share/path is used, the folder will be zipped before being downloaded
.PARAMETER DestinationPath
Required. The folder on the local computer in which to save the file e.g. C:\Users\User\Documents or ~/Documents
If a folder path is given, the script will attempt to use the last value in the source Path
parameter a the filename. If Path is a folder and no DestinationPath filename is specified then the
download file will be a ZIP file in the name of the folder, but lacking the .zip file extension
.OUTPUTS
The binary file requested or an error
.EXAMPLE
PS> Synology-DownloadFile -AuthToken $authToken -Path '/share/folder/MyFile.txt' -DestinationPath 'C:\Users\User\Documents'
#>
function Synology-DownloadFile {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$DestinationPath
)
if (-Not $AuthToken) {
return
}
$apiPath = 'webapi/entry.cgi'
$arrSrcPath = $($Path -Split '/')
$filename = $arrSrcPath[-1]
$Path = $(Synology-ParseFolderPath -Path $Path).Trim()
$Path = $([uri]::EscapeDataString($Path)) # URL Encode
$DestinationPath = $DestinationPath.Trim()
if (Test-Path -Path $DestinationPath -PathType Container) { # If the DestinationPath is a folder, attempt to append a source filename
if ($DestinationPath[-1] -NotIn $('\', '/')) {
$DestinationPath = "$($DestinationPath)\"
}
$DestinationPath = "$($DestinationPath)$($filename)"
}
$url = "http$(if($AuthToken.UseHttps) { 's' } else { '' })://$($AuthToken.Hostname):$($AuthToken.Port)/$($apiPath)?api=SYNO.FileStation.Download&method=download&version=2&_sid=$($AuthToken.Sid)"
$url = "$($url)&path=$($Path)&mode=download"
try {
Invoke-WebRequest -Uri $url -Method GET -OutFile $DestinationPath
} catch [System.Net.WebException] {
Throw "HTTP Error $($_.Exception.Message)"
}
}
<#
.SYNOPSIS
Upload a file to the NAS
.DESCRIPTION
Uploads a file to the specified file system path on the NAS
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER SourcePath
Required. The path on the local computer of the file to be uploaded e.g. C:\Users\User\Documents\MyFile.txt or ~/Documents/MyFile.txt
.PARAMETER DestinationPath
Required. The root relative file system path in the format of /share/directory{/directory}
.PARAMETER CreateParentFolders
Default = $true. If a folder path beneath a legal share is specified that does not exist on the NAS, attempt to create it as part of the write
.PARAMETER Overwrite
Default = $false. If the file already exists, overwrite it.
.PARAMETER LastModified
Optional. Override the file/folders last modified date/time using a UNIX epoch timestamp
.PARAMETER Created
Optional. Override the file/folders creation date/time using a UNIX epoch timestamp
.PARAMETER LastAccessed
Optional. Override the file/folders last accessed date/time using a UNIX epoch timestamp
.EXAMPLE
PS> Synology-UploadFile -AuthToken $authToken -SourcePath "C:\Users\User\Desktop\MyFile.txt" -DestinationPath "/share/folder/folder"
#>
function Synology-UploadFile {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$SourcePath,
[Parameter(Mandatory=$true)]
[string]$DestinationPath,
[Parameter(Mandatory=$false)]
[boolean]$CreateParentFolders=$true,
[Parameter(Mandatory=$false)]
[boolean]$Overwrite=$false,
[Parameter(Mandatory=$false)]
[int]$LastModified=$null,
[Parameter(Mandatory=$false)]
[int]$Created=$null,
[Parameter(Mandatory=$false)]
[int]$LastAccessed=$null
)
if (-Not $AuthToken) {
return
}
if (-Not $(Test-Path $SourcePath -PathType Leaf)) {
return
}
$dictHeaders = @{
"Content-Type" = "text/plain; charset=`"UTF-8`""
}
$parameters = @{}
$parameters.Set_Item("api", "SYNO.FileStation.Upload")
$parameters.Set_Item("version", 2)
$parameters.Set_Item("method", "upload")
#$parameters.Set_Item("_sid", $($AuthToken.Sid))
$parameters.Set_Item("create_parents", $([string]$CreateParentFolders).toLower())
if ($Overwrite) {
$parameters.Set_Item("overwrite", 'true') # true in v2, overwrite in v3
} else {
$parameters.Set_Item("overwrite", 'false') # false in v2, skip in v3
}
$parameters.Set_Item("path", $(Synology-ParseFolderPath -Path $DestinationPath)) # DestinationPath must have a leading / and no trailing /
if ($LastModified -is [int]) {
$parameters.Set_Item("mtime", $LastModified)
}
if ($Created -is [int]) {
$parameters.Set_Item("crtime", $Created)
}
if ($Created -is [int]) {
$parameters.Set_Item("atime", $LastAccessed)
}
$file = Get-Item $SourcePath
$fileBytes = [System.IO.File]::ReadAllBytes($file.FullName);
$fileEnc = [System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($fileBytes);
$boundary = [System.Guid]::NewGuid().ToString().Replace('-', '');
$boundaryPrefix = '-----------------------'
$LF = "`r`n";
$parameters.Set_Item("size", $($file.length))
$parameters
$bodyLines = ''
foreach ($parm in $parameters.keys) {
$bodyLines = $bodyLines + "$boundaryPrefix--$boundary$LF"
$bodyLines = $bodyLines + "Content-Disposition: form-data; name=`"$parm`"$LF$LF"
$bodyLines = $bodyLines + "$($parameters[$parm])$LF"
}
$bodyLines = $bodyLines + "$boundaryPrefix--$boundary$LF"
$bodyLines = $bodyLines + "Content-Disposition: form-data; name=`"file`"; filename=`"$($file.Name)`"$LF"
$bodyLines = $bodyLines + "Content-Type: application/octet-stream$LF$LF"
$bodyLines = $bodyLines + $fileEnc
$bodyLines = $bodyLines + $LF
$bodyLines = $bodyLines + "$boundaryPrefix--$boundary--$LF"
$url = "http$(if($AuthToken.UseHttps) { 's' } else { '' })://$($AuthToken.Hostname):$($AuthToken.Port)/webapi/entry.cgi?api=SYNO.FileStation.Upload&method=upload&version=2&_sid=$($AuthToken.Sid)"
# NOTE: Binary data > MUST < be the LAST part of the request body
Invoke-RestMethod -Uri $Url -Method Post -ContentType "multipart/form-data; boundary=$boundary" -Body $bodyLines -Headers $dictHeaders # NOTE: bouondary=$boundary has NO QUOTES
}
<#
.SYNOPSIS
Delete a file or folder/folder hierarchy
.DESCRIPTION
Deletes a file or folder/folder hierarchy on the NAS
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Path
Required. The root relative file system path in the format of /share/directory{/directory/}{filename.ext}
.PARAMETER Recursive
Default = $false. If a folder is specified, whether to delete the folder and all sub-folders and items ($true) or to fial the operation unless it is empty ($false)
.OUTPUTS
Boolean. $true if no error occured, $false if it did. Will return $true even if the file did not exist
.EXAMPLE
PS> Synology-DeleteFile -AuthToken $authToken -Path '/share/folder' -Recursive $true
.EXAMPLE
PS> Synology-DeleteFile -AuthToken $authToken -Path '/share/folder/file.ext'
#>
function Synology-DeleteFile {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$false)]
[boolean]$Recursive=$false
)
$Path = $(Synology-ParseFolderPath -Path $Path)
$parameters = @{}
$parameters.Set_Item("path", $Path)
$parameters.Set_Item("recursive", $([string]$Recursive).ToLower())
$return = Synology-InvokeMethod -AuthToken $authToken -API 'SYNO.FileStation.Delete' -Method 'delete' -Version 2 -Parameters $parameters -Return $true -OutputFormat HttpStatusCode
if ($return) {
return $true
} else {
return $false
}
}
<#
.SYNOPSIS
Rename a file or folder
.DESCRIPTION
Renames a file or folder on the NAS
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Path
Required. The root relative file system path in the format of /share/directory/directory/{filename.ext}
.PARAMETER NewName
Required. The name of the folder or file (excluding the path)
.PARAMETER Additional
Optional. Request additional data in the return response [real_path, size, owner, time,perm or type]
.OUTPUTS
JSON representation of the NAS response.
.EXAMPLE
PS> Synology-RenameFile -AuthToken $authToken -Path '/Shared/old-filename.txt' -NewName 'new-filename.txt'
#>
function Synology-RenameFile {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$NewName,
[Parameter(Mandatory=$false)]
[ValidateSet('real_path', 'size', 'owner', 'time', 'perm', 'type', IgnoreCase = $false)]
[string]$Additional
)
$Path = $(Synology-ParseFolderPath -Path $Path)
$parameters = @{}
$parameters.Set_Item("path", $Path)
$parameters.Set_Item("name", $NewName)
if ($Additional) {
$parameters.Set_Item("additional", $Additional)
}
return Synology-InvokeMethod -AuthToken $authToken -API 'SYNO.FileStation.Rename' -Method 'rename' -Version 2 -Parameters $parameters -Return $true -OutputFormat Json
}
<#
.SYNOPSIS
Create a folder
.DESCRIPTION
Creates a folder or folder hierarchy on the NAS
.PARAMETER AuthToken
Required. The credential structure used to pass the server address and authentication token to methods
.PARAMETER Path
Required. The root relative file system path in the format of /share/directory/directory
.PARAMETER Name
Required. The name of the lowest level new folder (excluding the path)
.PARAMETER ForceParent
Default = $false. If the Path value specified to place the new folder in does not exist, create it
.PARAMETER Additional
Optional. Request additional data in the return response [real_path, size, owner, time,perm or type]
.OUTPUTS
JSON representation of the NAS response.
.EXAMPLE
PS> Synology-CreateFolder -AuthToken $authToken -Path '/Shared/myfolder' -Name 'my-data' -ForceParent $true
#>
function Synology-CreateFolder {
Param (
[Parameter(Mandatory=$true)]
$AuthToken,
[Parameter(Mandatory=$true)]
[string]$Path,
[Parameter(Mandatory=$true)]
[string]$Name,
[Parameter(Mandatory=$false)]
[boolean]$ForceParent,
[Parameter(Mandatory=$false)]
[ValidateSet('real_path', 'size', 'owner', 'time', 'perm', 'type', IgnoreCase = $false)]
[string]$Additional
)
$Path = $(Synology-ParseFolderPath -Path $Path)
$parameters = @{}
$parameters.Set_Item("folder_path", $Path)
$parameters.Set_Item("name", $Name)