在 PowerShell 中使用 WinSCP.Session 上传一组文件
Upload set of files using WinSCP.Session in PowerShell
我正在尝试上传三个特定文件,并获取对所有三个传输的反馈。
使用下面的代码我只得到
Upload of \xxxxxxxxxxxxxxxxxxx\Customerlocations.txt succeeded
我知道它不是在会话中添加传输,而是每次都替换它。
我只是不知道要调整这个所以我在 $transferResult.Transfers
.
中得到了 3 次传输
有人可以指出正确的方向吗?
# Set up session options
$sessionOptions = New-WinSCPSessionOption -Protocol Ftp `
-FtpSecure Implicit `
-Credential xxxxxxxxxxxxxxxxxxx `
-HostName "xxxxxxxxxxxxxxxxxxx " `
-TlsHostCertificateFingerprint "xxxxxxxxxxxxxxxxxxx "
$session = New-Object WinSCP.Session
$session.ExecutablePath = "C:\xxxxxxxxxxxxxxxxxxx\WinSCP.exe"
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Orders.txt", "/Orders.txt", $False, $transferOptions)
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Assortment.txt", "/Assortment.txt", $False, $transferOptions)
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Customerlocations.txt", "/Customerlocations.txt", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
# Disconnect, clean up
$session.Dispose()
}
finally
{
$session.Dispose()
}
您必须分别检查每个 Session.PutFiles
调用的结果。为避免重复代码,您可以使用这样的循环:
$paths = @("...\Orders.txt", "...\Assortment.txt", "...\Customerlocations.txt")
foreach ($path in $paths)
{
$transferResult = $session.PutFiles($path, "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
我正在尝试上传三个特定文件,并获取对所有三个传输的反馈。 使用下面的代码我只得到
Upload of \xxxxxxxxxxxxxxxxxxx\Customerlocations.txt succeeded
我知道它不是在会话中添加传输,而是每次都替换它。
我只是不知道要调整这个所以我在 $transferResult.Transfers
.
有人可以指出正确的方向吗?
# Set up session options
$sessionOptions = New-WinSCPSessionOption -Protocol Ftp `
-FtpSecure Implicit `
-Credential xxxxxxxxxxxxxxxxxxx `
-HostName "xxxxxxxxxxxxxxxxxxx " `
-TlsHostCertificateFingerprint "xxxxxxxxxxxxxxxxxxx "
$session = New-Object WinSCP.Session
$session.ExecutablePath = "C:\xxxxxxxxxxxxxxxxxxx\WinSCP.exe"
try
{
# Connect
$session.Open($sessionOptions)
# Download files
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Orders.txt", "/Orders.txt", $False, $transferOptions)
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Assortment.txt", "/Assortment.txt", $False, $transferOptions)
$transferResult =
$session.PutFiles("\xxxxxxxxxxxxxxxxxxx\Customerlocations.txt", "/Customerlocations.txt", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
# Disconnect, clean up
$session.Dispose()
}
finally
{
$session.Dispose()
}
您必须分别检查每个 Session.PutFiles
调用的结果。为避免重复代码,您可以使用这样的循环:
$paths = @("...\Orders.txt", "...\Assortment.txt", "...\Customerlocations.txt")
foreach ($path in $paths)
{
$transferResult = $session.PutFiles($path, "/", $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}