powershell 查看本地文件夹中的更改并将其上载到SFTP服务器

kmynzznz  于 2023-02-04  发布在  Shell
关注(0)|答案(1)|浏览(134)

这里有一个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}
c0vxltue

c0vxltue1#

只需获取新文件的路径,并使用RemotePath.TranslateLocalPathToRemote将其Map到服务器路径,然后将文件上载到那里。
代码会简单很多,你所要做的就是确保新文件夹在服务器上重新创建。

# Watches a directory for new files and
# fires off the batch file to push to connection

$remotePath = "/data"
$localPath = "C:\pathTo\watchfolder"
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = $localPath
$watcher.Filter  = "*.csv"
$watcher.IncludeSubdirectories = $True
$watcher.EnableRaisingEvents = $True

### LISTEN FOR CREATE
Register-ObjectEvent $watcher Created -SourceIdentifier FileCreated -Action { 
    try
    {
        $localFilePath = $event.SourceEventArgs.FullPath
        Write-Host "Local path: $localFilePath"

        $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 = "example.com"
            UserName = "username"
            Password = "password"
            SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx...="
        }

        $session = New-Object WinSCP.Session

        try
        {
            $remoteFilePath =
                [WinSCP.RemotePath]::TranslateLocalPathToRemote(
                    $localFilePath, $localPath, $remotePath)
            Write-Host "Remote path: $remoteFilePath"

            # Connect
            $session.Open($sessionOptions)

            # Check if corresponding remote directory exists, if not, create it
            $i = $remoteFilePath.LastIndexOf("/")
            $remoteDirPath = $remoteFilePath.SubString(0, $i)
            if (($remoteDirPath.Length -gt 0) -and
                !$session.FileExists($remoteDirPath))
            {
                Write-Host "New subdirectory, creating $remoteDirPath on server"
                $session.CreateDirectory($remoteDirPath)
            }

            $session.PutFiles($localFilePath, $remoteFilePath).Check()

            Write-Host "Upload of $localFilePath succeeded"
        }
        finally
        {
            # Disconnect, clean up
            $session.Dispose()
        }

    } #end of first try 
    catch
    {
        Write-Host "Error: $($_.Exception.Message)"
    }
} #end of action

while ($True) { sleep 5 }

相关问题