WinSCP PowerShell 脚本用于下载最新文件,但仅适用于与部分名称匹配的文件

问题描述 投票:0回答:1

我是这个 WinSCP 脚本的新手,但是,我试图找到一个可以下载最新文件的脚本,但它只考虑与部分名称条件匹配的文件。

我找到了这个脚本,但它下载的是最新的脚本,但它占了文件夹中的所有文件。

如有任何帮助,我们将不胜感激。

param (
    $localPath = "c:\downloaded",
    $remotePath = "/home/user"
)
 
try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "example.com"
        UserName = "user"
        Password = "mypassword"
        SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..."
    }
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        # Get list of files in the directory
        $directoryInfo = $session.ListDirectory($remotePath)
 
        # Select the most recent file
        $latest =
            $directoryInfo.Files |
            Where-Object { -Not $_.IsDirectory } |
            Sort-Object LastWriteTime -Descending |
            Select-Object -First 1
 
        # Any file at all?
        if ($latest -eq $Null)
        {
            Write-Host "No file found"
            exit 1
        }
 
        # Download the selected file
        $session.GetFileToDirectory($latest.FullName, $localPath) | Out-Null
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
powershell sftp winscp winscp-net
1个回答
0
投票

“部分名称标准”有点模糊,但这样的事情应该可以:

        $latest =
            $directoryInfo.Files |
            Where-Object { $_.Name -like "*partialname*" } |
            ...
© www.soinside.com 2019 - 2024. All rights reserved.