使用PowerShell中的WinSCP.NET程序集将X天以上的日志/文件从Unix SFTP服务器复制到Windows

使用PowerShell中的WinSCP.NET程序集将X天以上的日志/文件从Unix SFTP服务器复制到Windows,windows,powershell,sftp,winscp,winscp-net,Windows,Powershell,Sftp,Winscp,Winscp Net,此代码使用WinSCP.NET程序集将文件从远程Unix服务器下载到Windows服务器。但是,我不希望将所有日志文件从源Unix服务器上的logs文件夹复制到目标Windows服务器,而是希望它复制过去30天的日志。原因是我不希望它填充目标窗口服务器上的C驱动器 param ( $localPath = "d:\Logs\OnlineLogs", $remotePath = "/application/oracle", $fileName = "int_access*

此代码使用WinSCP.NET程序集将文件从远程Unix服务器下载到Windows服务器。但是,我不希望将所有日志文件从源Unix服务器上的logs文件夹复制到目标Windows服务器,而是希望它复制过去30天的日志。原因是我不希望它填充目标窗口服务器上的C驱动器

param (
    $localPath = "d:\Logs\OnlineLogs",
    $remotePath = "/application/oracle",
    $fileName =  "int_access*.*"
)

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"

    # Set up session options
    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = ""
        UserName = ""
        Password = ""
        SshHostKeyFingerprint = ""
    }

    $session = New-Object WinSCP.Session 

    try
    {
        $session.Open($sessionOptions)

        $session.GetFiles(($remotePath + $fileName), ($localPath + $fileName)).Check()
    }
    finally
    {
        #Disconnect, clean up
        $session.Dispose()
    }

    exit 0
}
catch
{
    Write-Host "Error: $($_.Exception.Message)"
    exit 1
}
您可以使用:


(路径和文件名之间也缺少斜杠和反斜杠,这就是为什么我添加了
连接路径
和调用)。

在上述修改脚本完成后,但文件现在不会被传输到日志文件后(set
$session.SessionLogPath=“winscp.log”
)。不是$session.open()与上面的行相同,我不理解您的注释。运行脚本时出现错误“无法将参数绑定到参数“Name”,因为它为null”。抱歉,我是powershell的新手,谷歌搜索此错误无助于匹配
$transferOptions = New-Object WinSCP.TransferOptions -Property @{
    FileMask = "*>=30D"
}

$session.GetFiles(
    (Join-Path $remotePath $fileName),
    [WinSCP.RemotePath]::Combine($localPath, "*.*"),
    $False, $transferOptions).Check()