ftp批处理文件脚本

时间:2017-03-28 12:23:51

标签: windows powershell batch-file ftp

希望有人可以指导我/帮助我。

问题是,我有2台运行Ubuntu的服务器,它有一个网站供客户登录和下载/查看报告。另一个是创建/存储报告的Windows Server 2012 R2。我需要将文件从Windows移动到Ubuntu服务器,以便客户端可以查看。目前数据量很大,为7gb,每年增长3GB。

我需要使用ftp连接批处理文件,然后将文件夹复制到本地文件夹。但是,它只需要复制那些已修改过的文件。

我只编写了一个批处理文件,我似乎无法找到任何只复制修改过的文件的ftp批处理脚本。

我的最后一招,因为我似乎无法找到一个知道批处理脚本的编码器(它是一种节食艺术)。我从未使用过powershell,所以不知道从哪里开始。

任何帮助或建议请告诉我。

由于 约翰

2 个答案:

答案 0 :(得分:2)

您可以使用winscp的PowerShell执行此操作。例如:

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 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}

$session = New-Object WinSCP.Session

try
{
    # Connect
    $session.Open($sessionOptions)

    # Upload files
    $transferOptions = New-Object WinSCP.TransferOptions
    $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

    $transferResult = $session.PutFiles("d:\toupload\*", "/home/user/", $False, $transferOptions)

    # Throw on any error
    $transferResult.Check()

    # Print results
    foreach ($transfer in $transferResult.Transfers)
    {
        Write-Host ("Upload of {0} succeeded" -f $transfer.FileName)
    }
}
finally
{
    # Disconnect, clean up
    $session.Dispose()
}


    exit 0
}
catch [Exception]
{
    Write-Host ("Error: {0}" -f $_.Exception.Message)
    exit 1
}

答案 1 :(得分:0)

这是在PowerShell中执行此操作的一种方法。这将需要31天以上的文件并上传它们。

function FTP-Upload {
    [CmdletBinding()]
    param(
    [Parameter(Mandatory=$true)]
    [string]$Source_File,
    [Parameter(Mandatory=$true)]
    [string]$Target_File,
    [Parameter(Mandatory=$true)]
    [string]$Target_Server,
    [Parameter(Mandatory=$true)]
    [string]$Target_Username,
    [Parameter(Mandatory=$true)]
    [string]$Target_Password
    )

    $FTP = [System.Net.FTPWebRequest]::Create("ftp://$Target_Server/$Target_File")
    $FTP = [System.Net.FTPWebRequest]$FTP
    $FTP.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
    $FTP.Credentials = New-Object System.Net.NetworkCredential($Target_Username,$Target_Password)
    $FTP.UseBinary = $true
    $FTP.UsePassive = $true
    # read in the file to upload as a byte array
    $content = [System.IO.File]::ReadAllBytes($Source_File)
    $FTP.ContentLength = $content.Length
    # get the request stream, and write the bytes into it
    $rs = $FTP.GetRequestStream()
    $rs.Write($content, 0, $content.Length)
    # be sure to clean up after ourselves
    $rs.Close()
    $rs.Dispose()
}

$Upload_Server = "server.network.tld"
$Upload_Location = "/data/"
$Upload_Username = "ftpuser"
$Upload_Password = "ftppassword"

$Files_To_Upload = Get-ChildItem E:\Path\To\Files -Recurse | Where-Object {($_.CreationTime -le (Get-Date).AddDays(-31)) -and (!$_.PSIsContainer)}

Foreach ($File in $Files_To_Upload) {
    FTP-Upload -Source_File $File.FullName -Target_File ($Upload_Location + $File.Name) -Target_Server $Upload_Server -Target_Username $Upload_Username -Target_Password $Upload_Password
}