使用WinSCP从通配符子目录中的远程SFTP服务器中的PowerShell列表文件

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

我正在PowerShell中与WinSCP一起使用,以列出通配符子目录中的文件。但是,如果我在子目录之间放置通配符/掩码,则$remotepath无法正常工作。

这是我到目前为止所拥有的:

param (
    ##not working with mask, need to put full path
    $remotePath = "/ftpdata/*/infile/$currmon/",   
    $wildcard = "*$yesterday.*"
)

try
{   Add-Type -Path "WinSCPnet.dll"

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "Hostnamme" UserName = "UserName" Password = "Password"
        SshHostKeyFingerprint = "ssh-rsa 2048 ............................"
    } 
    $session = New-Object WinSCP.Session

    try
    {   $session.Open($sessionOptions)
        $files = $session.EnumerateRemoteFiles(
            $remotePath, $wildcard, [WinSCP.EnumerationOptions]::None ) 

        if ($files.Count -gt 0)
        {
            foreach ($fileInfo in $files)
            {
                Write-Host ("$($fileInfo.Name) with size $($fileInfo.Length), " +
                    "last modification at $($fileInfo.LastWriteTime)")
            }
        }
        else
        {
            Write-Host "No files matching $wildcard found"
        }    
    }
    finally
    {
        $session.Dispose()
    }

    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
powershell sftp wildcard winscp
1个回答
0
投票

实际上,您只能在最后一个路径组件中使用通配符。

要实现所需的功能,必须首先在/ftpdata中列出所有文件夹。然后循环浏览文件夹列表,并在其infile/$currmon子文件夹中列出文件。

© www.soinside.com 2019 - 2024. All rights reserved.