【问题标题】:Is there a way to avoid creating a temp file when uploading SFTP file using powershell?有没有办法避免在使用 powershell 上传 SFTP 文件时创建临时文件?
【发布时间】:2020-04-19 12:04:25
【问题描述】:

我编写了一个 powershell 脚本来将文件上传到远程 FTP 服务器。我成功连接到服务器,但在上传期间,它使用 filepart 的名称创建了一个临时文件,该文件的名称与我的本地文件的确切大小相同,但从不将临时文件重命名回原始本地文件姓名。由于文件名不是服务器期望看到的,因此它被隔离。我想知道是否有办法避免在上传过程中创建这个临时文件?我正在使用最新版本的 Powershell ISE 来运行脚本。

这是我的代码:

Add-Type -Path ("C:\Program Files (x86)\WinSCP\WinSCPnet.dll")

# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "***.***.***.***"
PortNumber = ****
UserName = "***********"
Password = '*******'
SshHostKeyFingerprint = "ssh-rsa 2048 9c:cb:ea:c2:25:50:cf:4e:eb:97:6c:ad:9b:28:a9:25"
GiveUpSecurityAndAcceptAnySshHostKey = "True"
#GiveUpSecurityAndAcceptAnyTlsHostCertificate = "true"
#FtpSecure = "True"
#$ftp.Passive = $false
}
$session = New-Object WinSCP.Session
$session.SessionLogPath = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\UploadLog.log"
# Logging Configuration
$date = Get-Date -Format MM-dd-yy
$timestamp = Get-Date -Format "MM/dd/yyyy h:mm:s"
$logFile = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\moveMRDF_$($date).log"
Add-Content -Path $logFile "Start of logging"
Add-Content -Path $logFile "MRDF File Processing Log"
Add-Content -Path $logFile $timestamp
$date = Get-Date -Format MM-dd-yy

# Setup File Paths 
$prodPath = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\Reports"
$remoteProdPath = "/MRDF/" 
Add-Content -Path $logfile $prodPath
Add-Content -Path  $logfile $remoteProdPath
$transferFile = "\\***.***.***.***\vipsvr\Rancho\MRDF_Report\Reports\12_20_2019_Report.zip"
Add-Content -Path  $logfile $session
Add-Content -Path  $logfile $transferFile

# Connect & Transfer to FTP Server
Try
{
    #echo $sessionOptions
    $session.Open($sessionOptions)
    #echo 'after open'
    Add-Content -Path  $logfile $session
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
    $transferOptions.ResumeSupport.State = [WinSCP.TransferResumeSupportState]::Off
    $session.PutFiles($transferFile, $remoteProdPath)
}
finally
{
    $session.Dispose()
}

【问题讨论】:

  • 你能禁用临时名称功能吗?如果没有,也许您可​​以尝试在上传后手动启动“重命名”?

标签: .net powershell sftp winscp winscp-net


【解决方案1】:

我正在使用PowerShell wrapper,我必须在上传和下载命令期间传递传输选项。

这是我正在使用的:

$TransferOptions = New-WinSCPTransferOption -ResumeSupport $(New-WinSCPTransferResumeSupport -State Off)

Send-WinSCPItem -WinSCPSession $Session -TransferOptions $TransferOptions -Path "C:\test.txt" -Destination "/test/path" -Verbose

没有-TransferOptions 我看到.filepart 被创建但-TransferOptions 没有.filepart。

在调用 .putfile 时尝试传递传输选项。像这样:

$session.PutFiles($MCRline, $remotePath, $False, $transferOptions).Check()

查看此链接:https://winscp.net/forum/viewtopic.php?t=23217

【讨论】:

    【解决方案2】:

    您永远不会使用您的 $transferOptions 变量。您必须将其传递给Session.PutFiles

    $session.PutFiles($transferFile, $remoteProdPath, $False, $transferOptions)
    

    【讨论】:

    • 回复了我发布的相同答案
    • @LT- 真。我实际上并没有读到最后的答案:) 我只阅读了使用 PowerShell 包装器的部分。我会删除我的答案。我已经编辑了您的答案以突出显示实际的解决方案。
    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2018-05-19
    • 2021-09-15
    • 2018-01-18
    • 2012-10-09
    • 1970-01-01
    • 2020-07-27
    相关资源
    最近更新 更多