使用 PowerShell/WinSCP 脚本上传后移动文件

编程入门 行业动态 更新时间:2024-10-26 14:41:20
本文介绍了使用 PowerShell/WinSCP 脚本上传后移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 WinSCP 中生成的 PowerShell 脚本来 sftp 某个文件夹中的文件.它每周五早上运行,但我需要它在上传文件后将文件移动到另一个文件夹.我尝试了 MoveFiles 和 PutFiles 命令,但它们似乎不起作用.任何帮助表示赞赏.代码如下.

I am using a PowerShell script generated in WinSCP to sftp files in a certain folder. It runs every Friday morning, but I need it to move the files to another folder after they are uploaded. I tried the MoveFiles and PutFiles command but they don't seem to work. Any help is appreciated. Code below.

# Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Set up session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "xxxx" UserName = "xxxxx" Password = "xxxx" SshHostKeyFingerprint = "xxxxx" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) # Transfer files $session.PutFiles("xxxxx", "xxxxxx*").Check() } finally { $session.Dispose() }

推荐答案

WinSCP 站点示例中有一个示例可以解决您的确切问题:上传成功后将本地文件移动到不同位置.

There's an example on WinSCP site example for your exact question: Moving local files to different location after successful upload.

这恰好是您的问题标题的第一个谷歌搜索!

相关代码片段是:

# Iterate over every transfer foreach ($transfer in $transferResult.Transfers) { # Success or error? if ($transfer.Error -eq $Null) { Write-Host "Upload of $($transfer.FileName) succeeded, moving to backup" # Upload succeeded, move source file to backup Move-Item $transfer.FileName $backupPath } else { Write-Host "Upload of $($transfer.FileName) failed: $($transfer.Error.Message)" } }

更多推荐

使用 PowerShell/WinSCP 脚本上传后移动文件

本文发布于:2023-10-07 12:41:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1469411.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   上传   文件   PowerShell   WinSCP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!