使用 PowerShell/WinSCP 脚本上传后移动文件 [英] Move files after upload using PowerShell/WinSCP Script

查看:67
本文介绍了使用 PowerShell/WinSCP 脚本上传后移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 脚本上传后移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆