我使用 Powershell、FileSytemWatcher 和 WinSCP 进行不稳定的文件传输
I Have erratic file transfers with Powershell, FileSytemWatcher and WinSCP
我的 PowerShell 脚本将文件放入 ESB 时出现问题。
通常它可以工作,但并非总是如此,尤其是对于大文件会出错。有没有人看到我的脚本中的错误或 tackle/resolve 我的问题的解决方法?
注:
- 无法继续支持,因为我没有更改权。
- "WinSCP GUI" 我设法成功上传了大文件。 (恢复支持关闭)
- 包含要上传的文件的目录会被监控以创建文件,然后将文件复制到单独的文件夹以供上传。上传成功后,文件被删除(从第二个文件夹)。
- 我是 PowerShell 的初学者,所以可能是错误的原因。英语也不是我的母语,所以请原谅我的任何错误。
脚本在 TaskScheduler 中启动:
Powershell.exe -NoExit -File "script.ps1"
脚本如下:
$folder = 'Original_Folder'
$filter = '*.xml'
$destination = 'CopyTo_Folder'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "$timeStamp The file '$name' was $changeType and copied for SFTP to $destination"
"$timeStamp The file '$name' was $changeType and copied for SFTP to $destination" | Set-Content 'Powershell_Script.log'
Copy-Item $path -Destination $destination -Force -Verbose
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP_5_11_3\WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "ftp-server"
UserName = "User account"
Password = "User password"
SshHostKeyFingerprint = "ssh-rsa bla bla"
}
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
$Session.SessionLogPath="WinSCP.log"
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}
# Upload with transferoptions and delete original when succesfull
$transferResult=$session.PutFiles("CopyTo_Folder\*", "/Upload_Folder on ftp-server/", $True, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
"$timeStamp The file $($transfer.FileName) was successfully uploaded" | Add-Content 'Powershell_Script.log'
}
}
finally
{
$session.Dispose()
}
}
我似乎已经解决了这个问题。
在暂停之间获取复制操作时,它工作正常
所以:
Start-Sleep -s 30
Copy-Item $path -Destination $destination -Force -Verbose
Start-Sleep -s 30
FileSystemWatcher 立即行动,正如预期的那样?因此,文件在未完全创建时被复制。在复制命令后,上传将立即开始,所以我也在那里暂停,以便复制操作可以在上传开始之前完成。
最好也监视这些操作,并在操作完成后让脚本恢复。虽然我不知道用 FSW 创建文件是否可行...
我的 PowerShell 脚本将文件放入 ESB 时出现问题。 通常它可以工作,但并非总是如此,尤其是对于大文件会出错。有没有人看到我的脚本中的错误或 tackle/resolve 我的问题的解决方法?
注:
- 无法继续支持,因为我没有更改权。
- "WinSCP GUI" 我设法成功上传了大文件。 (恢复支持关闭)
- 包含要上传的文件的目录会被监控以创建文件,然后将文件复制到单独的文件夹以供上传。上传成功后,文件被删除(从第二个文件夹)。
- 我是 PowerShell 的初学者,所以可能是错误的原因。英语也不是我的母语,所以请原谅我的任何错误。
脚本在 TaskScheduler 中启动:
Powershell.exe -NoExit -File "script.ps1"
脚本如下:
$folder = 'Original_Folder'
$filter = '*.xml'
$destination = 'CopyTo_Folder'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{
IncludeSubdirectories = $false
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$changeType = $Event.SourceEventArgs.ChangeType
$timeStamp = $Event.TimeGenerated
Write-Host "$timeStamp The file '$name' was $changeType and copied for SFTP to $destination"
"$timeStamp The file '$name' was $changeType and copied for SFTP to $destination" | Set-Content 'Powershell_Script.log'
Copy-Item $path -Destination $destination -Force -Verbose
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP_5_11_3\WinSCPnet.dll"
# Set up session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "ftp-server"
UserName = "User account"
Password = "User password"
SshHostKeyFingerprint = "ssh-rsa bla bla"
}
$sessionOptions.AddRawSettings("FSProtocol", "2")
$session = New-Object WinSCP.Session
$Session.SessionLogPath="WinSCP.log"
try
{
# Connect
$session.Open($sessionOptions)
# Upload files
# Set up transfer options
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
ResumeSupport = New-Object WinSCP.TransferResumeSupport -Property @{ State = [WinSCP.TransferResumeSupportState]::Off }
}
# Upload with transferoptions and delete original when succesfull
$transferResult=$session.PutFiles("CopyTo_Folder\*", "/Upload_Folder on ftp-server/", $True, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
"$timeStamp The file $($transfer.FileName) was successfully uploaded" | Add-Content 'Powershell_Script.log'
}
}
finally
{
$session.Dispose()
}
}
我似乎已经解决了这个问题。
在暂停之间获取复制操作时,它工作正常
所以:
Start-Sleep -s 30
Copy-Item $path -Destination $destination -Force -Verbose
Start-Sleep -s 30
FileSystemWatcher 立即行动,正如预期的那样?因此,文件在未完全创建时被复制。在复制命令后,上传将立即开始,所以我也在那里暂停,以便复制操作可以在上传开始之前完成。
最好也监视这些操作,并在操作完成后让脚本恢复。虽然我不知道用 FSW 创建文件是否可行...