【发布时间】: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