这里有一个Linux用户尝试在PowerShell中编写一个脚本来自动执行从Windows服务器到远程服务器的SFTP传输。(虽然我还有一些测试要做)。我当前的问题是在脚本运行一次之后完成。我需要FileSystemWatcher
在每次发现新文件创建时运行。我意识到这是因为exit
,但当我删除该部分,用户放入一个目录时,它会循环遍历目录中的每个文件,并一遍又一遍地上传目录中的每个文件。我需要它做的是运行一次该过程,然后保持活动状态以侦听另一个文件创建事件。我的问题可能是我在Windows中编码的工作有限,所以任何帮助都是感激的!
是的,我意识到在处理我的文件和我的文件夹时重复的代码是多余的。我很可能会把这部分代码做成一个函数,它接受的参数取决于它是被给定的文件还是文件夹。
我张贴我的整个代码的情况下,它可以帮助别人以后
###Watches a directory for new files and fires off the batch file to push to Connexxion
$folder = "C:\pathTo\watchfolder"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $folder
$watcher.Filter = "*.csv"
$watcher.IncludeSubdirectories = $true
$watcher.EnableRaisingEvents = $true
### LISTEN FOR CREATE
Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action {
$folderpath = $(split-path -LiteralPath $event.SourceEventArgs.FullPath)
$filename = $event.SourceEventArgs.Name
$filepath = (Join-Path $folderpath $filename.split("\")[-1])
Write-Host folderpath: $folderpath
Write-Host filename: $filename
Write-Host filepath: $filepath
$remotePath = "/data/"
If($filename.contains("\")){
Write-Host "Here is directory"
try
{
#Set path to winscp
$assemblyPath = "C:\Program Files (x86)\WinSCP"
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "hostname"
UserName = "username"
Password = "passwd"
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles($folderpath, $remotePath, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}#end of first try
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
}
Else{
Write-Host "Here is a file"
try
{
#Set path to winscp
$assemblyPath = "C:\Program Files (x86)\WinSCP"
Add-Type -Path (Join-Path $assemblyPath "WinSCPnet.dll")
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Sftp
HostName = "hostname"
UserName = "username"
Password = "passwd"
SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
# Upload files, collect results
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$transferResult =
$session.PutFiles($filepath, $remotePath, $False, $transferOptions)
# Throw on any error
$transferResult.Check()
# Print results
foreach ($transfer in $transferResult.Transfers)
{
Write-Host "Upload of $($transfer.FileName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
exit 0
}#end of first try
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
}
}#end of action
while ($true) {sleep 5}
1条答案
按热度按时间c0vxltue1#
只需获取新文件的路径,并使用
RemotePath.TranslateLocalPathToRemote
将其Map到服务器路径,然后将文件上载到那里。代码会简单很多,你所要做的就是确保新文件夹在服务器上重新创建。