forked from tomohulk/WinSCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWinSCP.psm1
1232 lines (1121 loc) · 37.1 KB
/
WinSCP.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
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
<#
.SYNOPSIS
Defines information to allow an automatic connection and authentication of the session.
.DESCRIPTION
This object with valid settings is requried when opening a connection to a remote server.
.EXAMPLE
PS C:\> New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp
.EXAMPLE
PS C:\> $session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
.INPUTS
None.
.OUTPUTS
WinSCP.SessionOptions.
.NOTES
If the Sftp/Scp protocols are used, a SshHostKeyFingerprint value is needed or the connection will fail.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_sessionoptions
#>
function New-WinSCPSessionOptions
{
[CmdletBinding()]
[OutputType([WinSCP.SessionOptions])]
param
(
# HostName, Type String, The host to connect WinSCP to.
[Parameter(Mandatory = $true)]
[String]
$HostName,
# UserName, Type String, The Username to authenticate.
[Parameter(Mandatory = $true)]
[String]
$UserName,
# Password, Type String, The Password authenticate.
[Parameter()]
[String]
$Password,
# FtpMode, WinSCP.FtpMode, The mode of ftp to use.
[Parameter()]
[ValidateSet("Passive","Active")]
[WinSCP.FtpMode]
$FtpMode = "Passive",
# FtpSecure, Type WinSCP.FtpSecure, the security type to use with SFTP.
[Parameter()]
[ValidateSet("None","Implicit","ExplicitTls","ExplicitSsl")]
[WinSCP.FtpSecure]
$FtpSecure = "None",
# Protocol, Type WinSCP.Protocol, The protocol to be used.
[Parameter()]
[ValidateSet("Sftp","Scp","Ftp")]
[WinSCP.Protocol]
$Protocol = "Sftp",
# PortNumber, Type Int, The port to use, 0 will use the port default with protocol used.
[Parameter()]
[Int]
$PortNumber = 0,
# SshHostKeyFingerPrint, Type String, the Fingerprint of the SshHost to used, this property is mandatory when using Sftp or Scp protocols.
[Parameter()]
[String[]]
$SshHostKeyFingerprint,
# SshPrivateKeyPath, Type String, The full path to the private key.
[Parameter()]
[ValidateScript({ if (Test-Path -Path $_){ return $true } })]
[String]
$SshPrivateKeyPath,
# TlsHostCertificateFingerprint, Type String, the Tls Certificate Fingerprint, use with Sftp.
[Parameter()]
[String]
$TlsHostCertificateFingerprint,
# Timeout, Type Int, The timeout in seconds for server response. Default value is 15 seconds.
[Parameter()]
[Int]
$Timeout = 15
)
$sessionOptions = New-Object -TypeName WinSCP.SessionOptions -Property @{
FtpMode = $FtpMode
FtpSecure = $FtpSecure
HostName = $HostName
Password = $Password
PortNumber = $PortNumber
Protocol = $Protocol
Timeout = [TimeSpan]::FromSeconds($Timeout)
UserName = $UserName
}
if ($Protocol -eq "Sftp" -or $Protocol -eq "Scp")
{
if ([String]::IsNullOrEmpty($SshHostKeyFingerprint))
{
Read-Host -Prompt "SshHostkeyFingerprint"
}
}
if (-not([String]::IsNullOrEmpty($SshHostKeyFingerprint)))
{
try
{
$sessionOptions.SshHostKeyFingerprint = $SshHostKeyFingerprint
}
catch [System.Exception]
{
Write-Error $_
}
}
if (-not([String]::IsNullOrEmpty($SshPrivateKeyPath)))
{
try
{
$sessionOptions.SshPrivateKeyPath = $SshPrivateKeyPath
}
catch [System.Exception]
{
Write-Error $_
}
}
if (-not([String]::IsNullOrEmpty($TlsHostCertificateFingerprint)))
{
try
{
$sessionOptions.TlsHostCertificateFingerprint = $TlsHostCertificateFingerprint
}
catch [System.Exception]
{
Write-Error $_
}
}
return $sessionOptions
}
<#
.SYNOPSIS
This is the main interface class of the WinSCP assembly.
.DESCRIPTION
Creates a new WINSCP.Session Object with setting specified in the WinSCP.SessionOptions object. Assign this Object to a Variable to easily manipulate actions later.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp)
.EXAMPLE
PS C:\> $session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp | Open-WinSCPSession
.NOTES
If the WinSCPSession is piped into another WinSCP command, the connection will be disposed upon completion of that command.
.INPUTS
WinSCP.SessionOptions.
.OUTPUTS
WinSCP.Session.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session
#>
function Open-WinSCPSession
{
[CmdletBinding()]
[OutputType([WinSCP.Session])]
param
(
# Defines information to allow an automatic connection and authentication of the session.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[WinSCP.SessionOptions]
$SessionOptions,
# Path to store session log file to. Default null means, no session log file is created.
[Parameter()]
[String]
$SessionLogPath = $null
)
$session = New-Object -TypeName WinSCP.Session -Property @{
SessionLogPath = $SessionLogPath
}
try
{
$session.Open($SessionOptions)
return $session
}
catch [System.Exception]
{
Write-Error $_
}
}
<#
.SYNOPSIS
Closes/Disposes an active WinSCP Session Object.
.DESCRIPTION
After a WinSCP Session is no longer needed this function will dispose the COM object.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Close-WinSCPSession
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Close-WinSCPSession -WinSCPSession $session
.INPUTS
WinSCP.Session.
.OUTPUTS
None.
.NOTES
If the WinSCPSession is piped into another WinSCP command, this function will be called to auto dispose th connection upon complete of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session
#>
function Close-WinSCPSession
{
[CmdletBinding()]
[OutputType([Void])]
param
(
#WinSCPSession, Type WinSCP.Session, The active WinSCP Session to close.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession
)
try
{
$WinSCPSession.Dispose()
}
catch [System.Exception]
{
Write-Error $_
}
}
<#
.SYNOPSIS
Sets options for file transfers.
.DESCRIPTION
Sets available options for file transfers between the client and server.
.EXAMPLE
PS C:\> New-WinSCPTransferOptions -PreserveTimeStamp -TransferMode Binary
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Receive-WinSCPItem -WinSCPSession $session -TransferOptions -RemoteItem "rDir/rFile.txt" -LocalItem "C:\lDir\lFile.txt" (New-WinSCPTransferOptions -PreserveTimeStamp -TransferMode Binary)
.INPUTS
None.
.OUTPUTS
WinSCP.TransferOptions.
.NOTES
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_transferoptions
#>
function New-WinSCPTransferOptions
{
[CmdletBinding()]
[OutputType([WinSCP.TransferOptions])]
param
(
# FileMask, Type String, The file mask to be used.
[Parameter()]
[String]
$FileMask,
# PreserveTimeStamp, Type Switch, Preserves the file created timestamp.
[Parameter()]
[Switch]
$PreserveTimeStamp,
# TransferMode, Type WinSCP.TransferMode, The method the file transfer use when transfering.
[Parameter()]
[ValidateSet("Binary","Ascii","Automatic")]
[WinSCP.TransferMode]
$TransferMode = "Binary"
)
$transferOptions = New-Object -TypeName WinSCP.TransferOptions -Property @{
PreserveTimeStamp = $PreserveTimeStamp.IsPresent
TransferMode = $TransferMode
}
return $transferOptions
}
<#
.SYNOPSIS
Revices file(s) from an active WinSCP Session.
.DESCRIPTION
After creating a valid WinSCP Session, this function can be used to receive file(s) and remove the remote files if desired.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Receive-WinSCPItem -RemoteItem "rDir/rFile.txt" -LocalItem "C:\lDir\lFile.txt"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Receive-WinSCPItem -WinSCPSession $session -RemoteItem "rDir/rFile.txt" -LocalItem "C:\lDir\lFile.txt" -RemoveRemoteItem
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.TransferOperationResult.
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_getfiles
#>
function Receive-WinSCPItem
{
[CmdletBinding()]
[OutputType([WinSCP.TransferOperationResult])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteItem, Type String Array, The item to be transfered.
[Parameter(Mandatory = $true)]
[String[]]
$RemoteItem,
# LocalItem, Type String, The local location for the transfered item. Default location is the current working directory.
[Parameter()]
[String]
$LocalItem = "$(Get-Location)\",
# TransferOptions, Type WinSCPtransferOption, The options for the file transfer.
[Parameter()]
[WinSCP.TransferOptions]
$TransferOptions = $(New-WinSCPTransferOptions),
# RemoveRemoteItem, Type Switch, Remove the transfered files from the FTP Host upon completion.
[Parameter()]
[Switch]
$RemoveRemoteItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($item in $RemoteItem.Replace("\","/"))
{
try
{
$WinSCPSession.GetFiles($item, $LocalItem, $RemoveRemoteItem.IsPresent, $TransferOptions)
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Send file(s) to an active WinSCP Session.
.DESCRIPTION
After creating a valid WinSCP Session, this function can be used to send file(s).
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Send-WinSCPItem -LocalItem "C:\lDir\lFile.txt" -RemoteItem "rDir/rFile.txt"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Send-WinSCPItem -WinSCPSession $session -LocalItem "C:\lDir\lFile.txt" -RemoteItem "rDir/rFile.txt" -RemoveLocalItem
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.TransferOperationResult.
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_putfiles
#>
function Send-WinSCPItem
{
[CmdletBinding()]
[OutputType([WinSCP.TransferOperationResult])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# LocalItem, Type String Array, The local location for the item to be transfered.
[Parameter(Mandatory = $true)]
[String[]]
$LocalItem,
# RemoteItem, Type String, The item to be transfered to. Default is the home directory for the user connecting.
[Parameter()]
[String]
$RemoteItem = "./",
# TransferOptions, Type WinSCPtransferOption, The options for the file transfer.
[Parameter()]
[WinSCP.TransferOptions]
$TransferOptions = $(New-WinSCPTransferOptions),
# RemoveLocalItem, Type Switch, Remove the transfered files from the Local Host upon completion.
[Parameter()]
[Switch]
$RemoveLocalItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($item in $LocalItem)
{
try
{
$WinSCPSession.PutFiles($item, $RemoteItem.Replace("\","/"), $RemoveRemoteItem.IsPresent, $TransferOptions)
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Creates a directory on an active WinSCP Session.
.DESCRIPTION
After creating a valid WinSCP Session, this function can be used to create new directory or nested directories.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | New-WinSCPDirectory -DirectoryName "rDir/rSubDir"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
New-WinSCPDirectory -WinSCPSession $session -DirectoryName "rDir/rSubDir"
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.RemoteFileInfo.
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_createdirectory
#>
function New-WinSCPDirectory
{
[CmdletBinding()]
[OutputType([WinSCP.RemoteFileInfo])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# DirectoryName, Type String Array, The path and name the new directory. The working directory is set as the homepath on the FTP Host, all new directories will be made from that starting point.
[Parameter(Mandatory = $true)]
[Alias("Dir")]
[String[]]
$DirectoryName
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach($directory in $DirectoryName.Replace("\","/"))
{
try
{
$WinSCPSession.CreateDirectory($directory)
return ($WinSCPSession.GetFileInfo($directory))
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Test if a remote item exists.
.DESCRIPTION
After creating a valid WinSCP Session, this function can be used to test if a directory or file exists on the remote source.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Test-WinSCPItemExists -RemoteItem "rDir/rSubDir"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Test-WinSCPItemExists -WinSCPSession $session -RemoteItem "rDir/rSubDir"
.INPUTS
WinSCP.Session.
.OUTPUTS
Boolean.
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_fileexists
#>
function Test-WinSCPItemExists
{
[CmdletBinding()]
[OutputType([Bool])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteItem, Type String Array, The full path to the item to be tested.
[Parameter(Mandatory = $true)]
[String[]]
$RemoteItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach($item in $RemoteItem.Replace("\","/"))
{
try
{
$WinSCPSession.FileExists($item)
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Retrives information about a File or Directory from an active WinSCP Session.
.DESCRIPTION
Retreives Name,FileType,Length,LastWriteTime,FilePermissions,IsDirectory Properties on an Item from an Active WinSCP Session.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Get-WinSCPItemInformation -RemoteItem "rDir/rSubDir"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Get-WinSCPItemInformation -WinSCPSession $session -RemoteItem "rDir/rSubDir"
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.RemoteFileInfo
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_getfileinfo
#>
function Get-WinSCPItemInformation
{
[CmdletBinding()]
[OutputType([WinSCP.RemoteFileInfo])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteItem, Type String Array, The path of the item to get information.
[Parameter(Mandatory = $true)]
[String[]]
$RemoteItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($item in $RemoteItem.Replace("\","/"))
{
try
{
$WinSCPSession.GetFileInfo($item)
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Shows the contents of a remote directory.
.DESCRIPTION
Displays the contents within a remote directory, including other directories and files.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Get-WinSCPDirectoryContents -RemoteDirectory "rDir/rSubDir"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Get-WinSCPDirectoryContents -WinSCPSession $session -RemoteDirectory "rDir/rSubDir" -ShowDetails
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.RemoteDirectoryInfo
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_listdirectory
#>
function Get-WinSCPDirectoryContents
{
[CmdletBinding()]
[OutputType([WinSCP.RemoteDirectoryInfo])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if($_.Open){ return $true }else{ throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteDirectory, Type String Array, The remote source path to show contents of.
[Parameter(Mandatory = $true)]
[Alias("Dir")]
[String[]]
$RemoteDirectory,
# ShowDetails, Type Switch, Show details about each item.
[Parameter()]
[Switch]
$ShowDetails
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($directory in $RemoteDirectory.Replace("\","/"))
{
try
{
if ($ShowDetails.IsPresent)
{
$WinSCPSession.ListDirectory($directory).Files
}
else
{
$WinSCPSession.ListDirectory($directory)
}
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Moves an item from one location to another from an active WinSCP Session.
.DESCRIPTION
Once connected to an active WinSCP Session, one or many files can be moved to another location within the same WinSCP Session.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Move-WinSCPItem -RemoteSourceItem "rDir/rFile.txt" -RemoteDestinationItem rDir/rSubDir/rFile.txt
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Move-WinSCPItem -WinSCPSession $session -RemoteSourceItem "rDir/rFile.txt" -RemoteDestinationItem rDir/rSubDir/rFile.txt
.INPUTS
WinSCP.Session.
.OUTPUTS
WinSCP.RemoteFileInfo
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_movefile
#>
function Move-WinSCPItem
{
[CmdletBinding()]
[OutputType([WinSCP.RemoteFileInfo])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteSourceItem, Type String Array, The remote source path of the item to be moved.
[Parameter(Mandatory = $true)]
[Alias("Source")]
[String[]]
$RemoteSourceItem,
# RemoteDestinationItem, Type String, the remote destination for moving the items to.
[Parameter(Mandatory = $true)]
[Alias("Destination")]
[String]
$RemoteDestinationItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($item in $RemoteSourceItem.Replace("\","/"))
{
try
{
$filename = $item.Substring($item.LastIndexOf("/") + 1)
$destination = $RemoteDestinationItem.Replace("\","/")
$WinSCPSession.MoveFile($item, $destination)
if ($destination.EndsWith("/"))
{
return $WinSCPSession.GetFileInfo($destination+ $filename)
}
else
{
return $WinSCPSession.GetFileInfo($destination)
}
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Removes and item from an active WinSCP Session.
.DESCRIPTION
Removes and item, File or Directory from a remote sources. This action will recurse if a the $RemotePath value is a directory.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Remove-WinSCPItem -RemoteItem "rDir/rFile.txt"
.EXAMPLE
$session = New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -SshHostKeyFingerprint "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | Open-WinSCPSession
Remove-WinSCPItem -WinSCPSession $session -RemoteItem "rDir/rFile.txt"
.INPUTS
WinSCP.Session
.OUTPUTS.
WinSCP.RemovalOperationResult.
.NOTES
If the WinSCPSession is piped into this command, the connection will be disposed upon completion of the command.
.LINK
http://dotps1.github.io
.LINK
http://winscp.net/eng/docs/library_session_removefiles
#>
function Remove-WinSCPItem
{
[CmdletBinding()]
[OutputType([WinSCP.RemovalOperationResult])]
param
(
# WinSCPSession, Type WinSCP.Session, A valid open WinSCP.Session, returned from Open-WinSCPSession.
[Parameter(Mandatory = $true,
ValueFromPipeLine = $true)]
[ValidateScript({ if ($_.Open) { return $true } else { throw "The WinSCP Session is not in an Open state." } })]
[Alias("Session")]
[WinSCP.Session]
$WinSCPSession,
# RemoteItem, Type String Array, The item to remove from the remote source.
[Parameter(Mandatory = $true)]
[String[]]
$RemoteItem
)
Begin
{
if ($PSBoundParameters.ContainsKey('WinSCPSession'))
{
$valueFromPipeLine = $false
}
else
{
$valueFromPipeLine = $true
}
}
Process
{
foreach ($item in $RemoteItem.Replace("\","/"))
{
try
{
$WinSCPSession.RemoveFiles($item)
}
catch [System.Exception]
{
Write-Error $_
return
}
}
}
End
{
if ($valueFromPipeLine -eq $true)
{
Close-WinSCPSession -WinSCPSession $WinSCPSession
}
}
}
<#
.SYNOPSIS
Syncronizes directories with an active WinSCP Session.
.DESCRIPTION
Syncronizes a local directory with a remote directory, or vise versa.
.EXAMPLE
PS C:\> Open-WinSCPSession -SessionOptions (New-WinSCPSessionOptions -Hostname myftphost.org -Username ftpuser -password "FtpUserPword" -Protocol Ftp) | Sync-WinSCPDirectory -RemoteDirectory "rDir/" -LocalDirectory "C:\lDir" -SyncMode Remote
.EXAMPLE