无法理解如何在GetFiles()
方法中使用APPEND功能。我必须附加多个文件并将其作为单个文件下载到我的本地计算机中。这个APPEND功能会起作用吗?
代码是:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Reading and Storing from configuration File
$csv = Import-Csv "Configuration.csv"
$csv | ForEach-Object {
$Hostname = $_.hostname
$username = $_.username
$Password = $_.Password
$hostkey = $_.hostkey
$Existingfilepath = $_.Existingfilepath
$newfilepath = $_.newfilepath
$localpath = $_.localpath
}
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $Hostname
UserName = $username
Password = $Password
SshHostKeyFingerprint = $hostkey
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Download files
$session.GetFiles($Existingfilepath, $localpath).Check()
# Move Files from Upload to Archive Folder
$session.MoveFile($Existingfilepath,$newfilepath)
} finally {
$session.Dispose()
}
编辑:我把Get-Files
放在$csv | ForEach-Object
下。
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
#Reading and Storing from configuration File
$csv = Import-Csv "Configuration.csv"
$opts = New-Object WinSCP.TransferOptions
$opts.TransferMode = [WinSCP.TransferMode]::Binary
$opts.OverwriteMode = [WinSCP.OverwriteMode]::Append
$csv | ForEach-Object {
$Hostname = $_.hostname
$username =$_.username
$Password = $_.Password
$hostkey = $_.hostkey
$Existingfilepath = $_.Existingfilepath
$newfilepath = $_.newfilepath
$localpath= $_.localpath
# Download files
$session.GetFiles($Existingfilepath, $localpath, $false, $opts).Check()
}
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $Hostname
UserName = $username
Password = $Password
SshHostKeyFingerprint = $hostkey
}
$session = New-Object WinSCP.Session
try {
# Connect
$session.Open($sessionOptions)
# Move Files from Upload to Archive Folder
$session.MoveFile($Existingfilepath,$newfilepath)
} finally {
$session.Dispose()
}
编辑:合并的文件日志
. 2017-10-25 17:04:35.570 --------------------------------------------------------------------------
. 2017-10-25 17:04:35.570 Using SFTP protocol.
. 2017-10-25 17:04:35.570 Doing startup conversation with host.
> 2017-10-25 17:04:35.570 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2017-10-25 17:04:36.085 Type: SSH_FXP_VERSION, Size: 114, Number: -1
. 2017-10-25 17:04:36.085 SFTP version 3 negotiated.
. 2017-10-25 17:04:36.085 SFTP versions supported by the server: 2,3
. 2017-10-25 17:04:36.085 Unknown server extension [email protected]="1"
. 2017-10-25 17:04:36.085 Supports [email protected] extension version "2"
. 2017-10-25 17:04:36.085 Unknown server extension [email protected]="2"
. 2017-10-25 17:04:36.085 We believe the server has signed timestamps bug
. 2017-10-25 17:04:36.085 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
. 2017-10-25 17:04:36.085 Getting current directory name.
. 2017-10-25 17:04:36.085 Getting real path for '.'
> 2017-10-25 17:04:36.085 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2017-10-25 17:04:36.709 Type: SSH_FXP_NAME, Size: 107, Number: 16
. 2017-10-25 17:04:36.709 Real path is '/'
. 2017-10-25 17:04:36.709 Startup conversation with host finished.
< 2017-10-25 17:04:36.709 Script: Active session: [1] [email protected]
> 2017-10-25 17:04:36.756 Script: pwd
< 2017-10-25 17:04:36.756 Script: /
> 2017-10-25 17:04:36.818 Script: get -nopermissions -preservetime -transfer="binary" -append -- "/upload/attendance*.csv" "C:\Ratnesh\Projects\abc-Bank\Transfer Files\abc Bank Attendance Files\attendance.csv"
. 2017-10-25 17:04:36.818 Listing directory "/upload".
> 2017-10-25 17:04:36.818 Type: SSH_FXP_OPENDIR, Size: 16, Number: 267
< 2017-10-25 17:04:37.411 Type: SSH_FXP_HANDLE, Size: 15, Number: 267
> 2017-10-25 17:04:37.411 Type: SSH_FXP_READDIR, Size: 15, Number: 524
< 2017-10-25 17:04:37.832 Type: SSH_FXP_NAME, Size: 673, Number: 524
> 2017-10-25 17:04:37.832 Type: SSH_FXP_READDIR, Size: 15, Number: 780
< 2017-10-25 17:04:38.144 Type: SSH_FXP_STATUS, Size: 33, Number: 780
< 2017-10-25 17:04:38.144 Status code: 1
> 2017-10-25 17:04:38.144 Type: SSH_FXP_CLOSE, Size: 15, Number: 1028
. 2017-10-25 17:04:38.160 Archive;d;0;2017-10-25T11:34:14.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:38.160 attendance v2-201709220930.csv;-;8815;2017-10-05T14:26:50.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:38.160 attendance v2-201709220931.csv;-;8512;2017-10-18T08:16:06.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:38.160 bulkloader;d;0;2017-09-20T11:54:37.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:38.160 userIntegration;d;0;2017-07-26T16:51:13.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:38.160 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
< 2017-10-25 17:04:38.160 Script: Are you sure you want to transfer multiple files to a single file 'attendance.csv' in a directory 'C:\Ratnesh\Projects\abc-Bank\Transfer Files\abc Bank Attendance Files\'?
< 2017-10-25 17:04:38.160 The files will overwrite one another.
< 2017-10-25 17:04:38.160 If you actually want to transfer all files to a directory 'C:\Ratnesh\Projects\abc-Bank\Transfer Files\abc Bank Attendance Files\attendance.csv\', keeping their name, make sure you terminate the path with a slash.
. 2017-10-25 17:04:38.160 Copying 2 files/directories to local directory "C:\Ratnesh\Projects\abc-Bank\Transfer Files\abc Bank Attendance Files\" - total size: 17,327
. 2017-10-25 17:04:38.160 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: attendance.csv
. 2017-10-25 17:04:38.160 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2017-10-25 17:04:38.160 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2017-10-25 17:04:38.160 File: '/upload/attendance v2-201709220930.csv' [2017-10-05T14:26:50.000Z] [8815]
. 2017-10-25 17:04:38.160 Copying "/upload/attendance v2-201709220930.csv" to local directory started.
. 2017-10-25 17:04:38.160 Binary transfer mode selected.
. 2017-10-25 17:04:38.160 Opening remote file.
> 2017-10-25 17:04:38.160 Type: SSH_FXP_OPEN, Size: 55, Number: 1283
< 2017-10-25 17:04:38.550 Type: SSH_FXP_STATUS, Size: 24, Number: 1028
. 2017-10-25 17:04:38.550 Discarding reserved response
< 2017-10-25 17:04:38.955 Type: SSH_FXP_HANDLE, Size: 15, Number: 1283
> 2017-10-25 17:04:38.955 Type: SSH_FXP_FSTAT, Size: 15, Number: 1544
< 2017-10-25 17:04:39.564 Type: SSH_FXP_ATTRS, Size: 37, Number: 1544
> 2017-10-25 17:04:39.579 Type: SSH_FXP_READ, Size: 27, Number: 1797
< 2017-10-25 17:04:41.066 Status code: 1
. 2017-10-25 17:04:41.066 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-25 17:04:41.066 Type: SSH_FXP_CLOSE, Size: 15, Number: 2308
. 2017-10-25 17:04:41.066 Preserving timestamp [2017-10-05T14:26:50.000Z]
. 2017-10-25 17:04:41.066 Transfer done: '/upload/attendance v2-201709220930.csv' => 'C:\Ratnesh\Projects\abc\Transfer Files\Attendance Files\attendance.csv' [8815]
. 2017-10-25 17:04:41.066 File: '/upload/attendance v2-201709220931.csv' [2017-10-18T08:16:06.000Z] [8512]
. 2017-10-25 17:04:41.066 Copying "/upload/attendance v2-201709220931.csv" to local directory started.
. 2017-10-25 17:04:41.066 Binary transfer mode selected.
. 2017-10-25 17:04:41.066 Opening remote file.
> 2017-10-25 17:04:41.066 Type: SSH_FXP_OPEN, Size: 55, Number: 2563
< 2017-10-25 17:04:41.722 Type: SSH_FXP_STATUS, Size: 24, Number: 2308
. 2017-10-25 17:04:41.722 Discarding reserved response
< 2017-10-25 17:04:42.034 Type: SSH_FXP_HANDLE, Size: 15, Number: 2563
> 2017-10-25 17:04:42.034 Type: SSH_FXP_FSTAT, Size: 15, Number: 2824
< 2017-10-25 17:04:42.689 Type: SSH_FXP_ATTRS, Size: 37, Number: 2824
. 2017-10-25 17:04:42.689 Confirming overwriting of file.
. 2017-10-25 17:04:42.689 Appending to file.
> 2017-10-25 17:04:42.689 Type: SSH_FXP_READ, Size: 27, Number: 3077
< 2017-10-25 17:04:43.672 Status code: 1
. 2017-10-25 17:04:43.672 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-25 17:04:43.672 Type: SSH_FXP_CLOSE, Size: 15, Number: 3588
. 2017-10-25 17:04:43.672 Preserving timestamp [2017-10-18T08:16:06.000Z]
. 2017-10-25 17:04:43.672 Transfer done: '/upload/attendance v2-201709220931.csv' => 'C:\Ratnesh\Projects\abc\Transfer Files\abc\attendance.csv' [8512]
. 2017-10-25 17:04:43.672 Copying finished: Transferred: 17,327, Elapsed: 0:00:05, CPS: 2,687/s
> 2017-10-25 17:04:44.124 Script: mv "/upload/attendance*.csv" "/upload/Archive/attendance*.csv"
. 2017-10-25 17:04:44.124 Listing directory "/upload".
> 2017-10-25 17:04:44.124 Type: SSH_FXP_OPENDIR, Size: 16, Number: 3851
< 2017-10-25 17:04:44.483 Type: SSH_FXP_STATUS, Size: 24, Number: 3588
. 2017-10-25 17:04:44.483 Discarding reserved response
< 2017-10-25 17:04:45.403 Type: SSH_FXP_HANDLE, Size: 15, Number: 3851
> 2017-10-25 17:04:45.403 Type: SSH_FXP_READDIR, Size: 15, Number: 4108
< 2017-10-25 17:04:45.809 Type: SSH_FXP_NAME, Size: 673, Number: 4108
> 2017-10-25 17:04:45.809 Type: SSH_FXP_READDIR, Size: 15, Number: 4364
< 2017-10-25 17:04:46.230 Type: SSH_FXP_STATUS, Size: 33, Number: 4364
< 2017-10-25 17:04:46.230 Status code: 1
> 2017-10-25 17:04:46.230 Type: SSH_FXP_CLOSE, Size: 15, Number: 4612
. 2017-10-25 17:04:46.230 Archive;d;0;2017-10-25T11:34:14.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:46.230 attendance v2-201709220930.csv;-;8815;2017-10-05T14:26:50.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:46.230 attendance v2-201709220931.csv;-;8512;2017-10-18T08:16:06.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:46.230 bulkloader;d;0;2017-09-20T11:54:37.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:46.230 userIntegration;d;0;2017-07-26T16:51:13.000Z;3;"abcbank" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:04:46.230 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2017-10-25 17:04:46.230 Moving file "/upload/attendance v2-201709220930.csv" to "/upload/Archive/attendance v2-201709220930.csv".
> 2017-10-25 17:04:46.230 Type: SSH_FXP_RENAME, Size: 97, Number: 4882
< 2017-10-25 17:04:46.948 Type: SSH_FXP_STATUS, Size: 24, Number: 4612
. 2017-10-25 17:04:46.948 Discarding reserved response
< 2017-10-25 17:04:47.244 Type: SSH_FXP_STATUS, Size: 24, Number: 4882
< 2017-10-25 17:04:47.244 Status code: 0
< 2017-10-25 17:04:47.244 Script: /upload/attendance v2-201709220930.csv
. 2017-10-25 17:04:47.244 Moving file "/upload/attendance v2-201709220931.csv" to "/upload/Archive/attendance v2-201709220931.csv".
> 2017-10-25 17:04:47.244 Type: SSH_FXP_RENAME, Size: 97, Number: 5138
< 2017-10-25 17:04:47.868 Type: SSH_FXP_STATUS, Size: 24, Number: 5138
< 2017-10-25 17:04:47.868 Status code: 0
< 2017-10-25 17:04:47.868 Script: /upload/attendance v2-201709220931.csv
> 2017-10-25 17:04:49.553 Script: exit
. 2017-10-25 17:04:49.553 Script: Exit code: 0
. 2017-10-25 17:04:49.553 Closing connection.
. 2017-10-25 17:04:49.553 Sending special code: 12
. 2017-10-25 17:04:49.553 Sent EOF message
单独的文件记录
. 2017-10-25 17:06:10.986 --------------------------------------------------------------------------
. 2017-10-25 17:06:10.986 Using SFTP protocol.
. 2017-10-25 17:06:10.986 Doing startup conversation with host.
> 2017-10-25 17:06:10.986 Type: SSH_FXP_INIT, Size: 5, Number: -1
< 2017-10-25 17:06:11.735 Type: SSH_FXP_VERSION, Size: 114, Number: -1
. 2017-10-25 17:06:11.735 SFTP version 3 negotiated.
. 2017-10-25 17:06:11.735 SFTP versions supported by the server: 2,3
. 2017-10-25 17:06:11.735 Unknown server extension [email protected]="1"
. 2017-10-25 17:06:11.735 Supports [email protected] extension version "2"
. 2017-10-25 17:06:11.735 Unknown server extension [email protected]="2"
. 2017-10-25 17:06:11.735 We believe the server has signed timestamps bug
. 2017-10-25 17:06:11.735 We will use UTF-8 strings until server sends an invalid UTF-8 string as with SFTP version 3 and older UTF-8 strings are not mandatory
. 2017-10-25 17:06:11.735 Getting current directory name.
. 2017-10-25 17:06:11.735 Getting real path for '.'
> 2017-10-25 17:06:11.735 Type: SSH_FXP_REALPATH, Size: 10, Number: 16
< 2017-10-25 17:06:12.374 Type: SSH_FXP_NAME, Size: 107, Number: 16
. 2017-10-25 17:06:12.374 Real path is '/'
. 2017-10-25 17:06:12.374 Startup conversation with host finished.
< 2017-10-25 17:06:12.374 Script: Active session: [1] abc.net
> 2017-10-25 17:06:12.874 Script: pwd
< 2017-10-25 17:06:12.874 Script: /
> 2017-10-25 17:06:12.936 Script: get -nopermissions -preservetime -transfer="binary" -append -- "/upload/attendance*.csv" "C:\Ratnesh\Projects\abc\Transfer Files\abc\attendance*.csv"
. 2017-10-25 17:06:12.936 Listing directory "/upload".
> 2017-10-25 17:06:12.936 Type: SSH_FXP_OPENDIR, Size: 16, Number: 267
< 2017-10-25 17:06:13.576 Type: SSH_FXP_HANDLE, Size: 15, Number: 267
> 2017-10-25 17:06:13.576 Type: SSH_FXP_READDIR, Size: 15, Number: 524
< 2017-10-25 17:06:14.028 Type: SSH_FXP_NAME, Size: 673, Number: 524
> 2017-10-25 17:06:14.028 Type: SSH_FXP_READDIR, Size: 15, Number: 780
< 2017-10-25 17:06:14.387 Type: SSH_FXP_STATUS, Size: 33, Number: 780
< 2017-10-25 17:06:14.387 Status code: 1
> 2017-10-25 17:06:14.387 Type: SSH_FXP_CLOSE, Size: 15, Number: 1028
. 2017-10-25 17:06:14.387 Archive;d;0;2017-10-25T11:35:40.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:14.387 attendance v2-201709220930.csv;-;8815;2017-10-05T14:26:50.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:14.387 attendance v2-201709220931.csv;-;8512;2017-10-18T08:16:06.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:14.387 bulkloader;d;0;2017-09-20T11:54:37.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:14.387 userIntegration;d;0;2017-07-26T16:51:13.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:14.387 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2017-10-25 17:06:14.387 Copying 2 files/directories to local directory "C:\Ratnesh\Projects\abc\Transfer Files\abc\" - total size: 17,327
. 2017-10-25 17:06:14.387 PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: attendance*.csv
. 2017-10-25 17:06:14.402 TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2017-10-25 17:06:14.402 AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2017-10-25 17:06:14.402 File: '/upload/attendance v2-201709220930.csv' [2017-10-05T14:26:50.000Z] [8815]
. 2017-10-25 17:06:14.402 Copying "/upload/attendance v2-201709220930.csv" to local directory started.
. 2017-10-25 17:06:14.402 Binary transfer mode selected.
. 2017-10-25 17:06:14.402 Opening remote file.
> 2017-10-25 17:06:14.402 Type: SSH_FXP_OPEN, Size: 55, Number: 1283
< 2017-10-25 17:06:14.699 Type: SSH_FXP_STATUS, Size: 24, Number: 1028
. 2017-10-25 17:06:14.699 Discarding reserved response
< 2017-10-25 17:06:14.995 Type: SSH_FXP_HANDLE, Size: 15, Number: 1283
> 2017-10-25 17:06:14.995 Type: SSH_FXP_FSTAT, Size: 15, Number: 1544
< 2017-10-25 17:06:15.588 Type: SSH_FXP_ATTRS, Size: 37, Number: 1544
> 2017-10-25 17:06:15.588 Type: SSH_FXP_READ, Size: 27, Number: 1797
< 2017-10-25 17:06:16.540 Status code: 1
. 2017-10-25 17:06:16.540 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-25 17:06:16.540 Type: SSH_FXP_CLOSE, Size: 15, Number: 2308
. 2017-10-25 17:06:16.540 Preserving timestamp [2017-10-05T14:26:50.000Z]
. 2017-10-25 17:06:16.540 Transfer done: '/upload/attendance v2-201709220930.csv' => 'C:\Ratnesh\Projects\abc\Transfer Files\abc\attendance v2-201709220930.csv' [8815]
. 2017-10-25 17:06:16.540 File: '/upload/attendance v2-201709220931.csv' [2017-10-18T08:16:06.000Z] [8512]
. 2017-10-25 17:06:16.540 Copying "/upload/attendance v2-201709220931.csv" to local directory started.
. 2017-10-25 17:06:16.540 Binary transfer mode selected.
. 2017-10-25 17:06:16.540 Opening remote file.
> 2017-10-25 17:06:16.540 Type: SSH_FXP_OPEN, Size: 55, Number: 2563
< 2017-10-25 17:06:16.836 Type: SSH_FXP_STATUS, Size: 24, Number: 2308
. 2017-10-25 17:06:16.836 Discarding reserved response
< 2017-10-25 17:06:17.164 Type: SSH_FXP_HANDLE, Size: 15, Number: 2563
> 2017-10-25 17:06:17.164 Type: SSH_FXP_FSTAT, Size: 15, Number: 2824
< 2017-10-25 17:06:17.866 Type: SSH_FXP_ATTRS, Size: 37, Number: 2824
> 2017-10-25 17:06:17.881 Type: SSH_FXP_READ, Size: 27, Number: 3077
< 2017-10-25 17:06:19.004 Status code: 1
. 2017-10-25 17:06:19.004 3 skipped SSH_FXP_WRITE, SSH_FXP_READ, SSH_FXP_DATA and SSH_FXP_STATUS packets.
> 2017-10-25 17:06:19.004 Type: SSH_FXP_CLOSE, Size: 15, Number: 3588
. 2017-10-25 17:06:19.004 Preserving timestamp [2017-10-18T08:16:06.000Z]
. 2017-10-25 17:06:19.004 Transfer done: '/upload/attendance v2-201709220931.csv' => 'C:\Ratnesh\Projects\abc\Transfer Files\abc\attendance v2-201709220931.csv' [8512]
. 2017-10-25 17:06:19.004 Copying finished: Transferred: 17,327, Elapsed: 0:00:04, CPS: 3,082/s
> 2017-10-25 17:06:19.426 Script: mv "/upload/attendance*.csv" "/upload/Archive/attendance*.csv"
. 2017-10-25 17:06:19.426 Listing directory "/upload".
> 2017-10-25 17:06:19.426 Type: SSH_FXP_OPENDIR, Size: 16, Number: 3851
< 2017-10-25 17:06:19.613 Type: SSH_FXP_STATUS, Size: 24, Number: 3588
. 2017-10-25 17:06:19.613 Discarding reserved response
< 2017-10-25 17:06:19.909 Type: SSH_FXP_HANDLE, Size: 15, Number: 3851
> 2017-10-25 17:06:19.909 Type: SSH_FXP_READDIR, Size: 15, Number: 4108
< 2017-10-25 17:06:20.330 Type: SSH_FXP_NAME, Size: 673, Number: 4108
> 2017-10-25 17:06:20.330 Type: SSH_FXP_READDIR, Size: 15, Number: 4364
< 2017-10-25 17:06:20.674 Type: SSH_FXP_STATUS, Size: 33, Number: 4364
< 2017-10-25 17:06:20.674 Status code: 1
> 2017-10-25 17:06:20.674 Type: SSH_FXP_CLOSE, Size: 15, Number: 4612
. 2017-10-25 17:06:20.674 Archive;d;0;2017-10-25T11:35:40.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:20.674 attendance v2-201709220930.csv;-;8815;2017-10-05T14:26:50.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:20.674 attendance v2-201709220931.csv;-;8512;2017-10-18T08:16:06.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:20.674 bulkloader;d;0;2017-09-20T11:54:37.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:20.674 userIntegration;d;0;2017-07-26T16:51:13.000Z;3;"abc" [2222];"ftp" [2222];rwxrwx---;0
. 2017-10-25 17:06:20.674 ..;D;0;1899-12-30T05:30:00.000Z;0;"" [0];"" [0];---------;0
. 2017-10-25 17:06:20.674 Moving file "/upload/attendance v2-201709220930.csv" to "/upload/Archive/attendance v2-201709220930.csv".
> 2017-10-25 17:06:20.674 Type: SSH_FXP_RENAME, Size: 97, Number: 4882
< 2017-10-25 17:06:21.251 Type: SSH_FXP_STATUS, Size: 24, Number: 4612
. 2017-10-25 17:06:21.251 Discarding reserved response
< 2017-10-25 17:06:21.625 Type: SSH_FXP_STATUS, Size: 24, Number: 4882
< 2017-10-25 17:06:21.625 Status code: 0
< 2017-10-25 17:06:21.625 Script: /upload/attendance v2-201709220930.csv
. 2017-10-25 17:06:21.625 Moving file "/upload/attendance v2-201709220931.csv" to "/upload/Archive/attendance v2-201709220931.csv".
> 2017-10-25 17:06:21.625 Type: SSH_FXP_RENAME, Size: 97, Number: 5138
< 2017-10-25 17:06:22.174 Type: SSH_FXP_STATUS, Size: 24, Number: 5138
< 2017-10-25 17:06:22.174 Status code: 0
< 2017-10-25 17:06:22.174 Script: /upload/attendance v2-201709220931.csv
> 2017-10-25 17:06:22.267 Script: exit
. 2017-10-25 17:06:22.267 Script: Exit code: 0
. 2017-10-25 17:06:22.267 Closing connection.
. 2017-10-25 17:06:22.267 Sending special code: 12
. 2017-10-25 17:06:22.267 Sent EOF message
如有疑问,请阅读documentation。 GetFiles()
方法最多需要4个参数:
public TransferOperationResult GetFiles( string remotePath, string localPath, bool remove = false, TransferOptions options = null )
您需要传递Append
传输选项作为第4个参数。如果您的CSV包含多条记录,则还需要将GetFiles()
调用放入处理CSV数据的循环中。
这样的东西应该工作(虽然未经测试):
$opts = New-Object WinSCP.TransferOptions
$opts.TransferMode = [WinSCP.TransferMode]::Binary
$opts.OverwriteMode = [WinSCP.OverwriteMode]::Append
$csv | ForEach-Object {
...
$session.GetFiles($Existingfilepath, $localpath, $false, $opts).Check()
}
请注意,这只会附加到本地文件。在远程主机上,您仍将拥有多个文件。
正如@Ansgar已经建议的那样,你可以使用OverwriteMode.Append
来连接文件。
完整代码如下:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Reading from configuration File
$csv = Import-Csv "Configuration.csv"
$csv | ForEach-Object {
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = $_.HostName
UserName = $_.UserName
Password = $_.Password
SshHostKeyFingerprint = $_.HostKey
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Download and append file
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.OverwriteMode = [WinSCP.OverwriteMode]::Append
$session.GetFiles(
$_.ExistingFilePath, $_.LocalPath, $False, $transferOptions).Check()
# Move Files from Upload to Archive Folder
$session.MoveFile($_.ExistingFilePath, $_.NewFilePath)
}
finally
{
$session.Dispose()
}
}
虽然注意这种方式,但您将为每个文件打开一个新连接。如果所有文件实际位于同一服务器上,则应考虑将服务器配置与文件列表分开,以允许使用单个连接。