What's the best way to automate secure FTP in PowerShell?

前端 未结 9 1570
闹比i
闹比i 2020-12-04 14:32

I\'d like to automate the FTP download of a database backup file using PowerShell. The file name includes the date so I can\'t just run the same FTP script every day. Is t

相关标签:
9条回答
  • 2020-12-04 15:23

    Taken from here

    $source = "ftp://ftp.microsoft.com/ResKit/win2000/dureg.zip"
    $target = "c:\temp\dureg.zip"
    $WebClient = New-Object System.Net.WebClient
    $WebClient.DownloadFile($source, $target)
    

    Works for me

    0 讨论(0)
  • 2020-12-04 15:28

    Something like this could work:

    $bkdir = "E:\BackupsPWS" #backups location directory
    $7Zip = 'C:\"Program Files"\7-Zip\7z.exe' #compression utility
    $files_to_transfer = New-Object System.Collections.ArrayList #list of zipped files to be   transferred over FTP
    $ftp_uri="myftpserver"
    $user="myftpusername"
    $pass="myftppassword"
    
    #find .bak files not zipped yet, zip them, add them to the list to be transferrd
    get-childitem -path $bkdir | Sort-Object length |
    where { $_.extension -match ".(bak)" -and
    -not (test-path ($_.fullname -replace "(bak)", "7z")) } |
    foreach {
    $zipfilename = ($_.fullname -replace "bak", "7z")
    Invoke-Expression "$7Zip a $zipfilename $($_.FullName)"
    $files_to_transfer.Add($zipfilename)
    }
    
    #find .bak files, if they've been zipped, delete the .bak file
    get-childitem -path $bkdir |
    where { $_.extension -match ".(bak)" -and
    (test-path ($_.fullname -replace "(bak)", "7z")) } |
    foreach { del $_.fullname }
    
    #transfer each zipped file over FTP
    foreach ($file in $files_to_transfer)
    {
    $webclient = New-Object System.Net.WebClient
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) # FTP credentials
    $ftp_urix = $ftp_uri + "/" + $file.Substring($bkdir.Length + 1) # ftp address where to transfer   the file
    $uri=[system.URI] $ftp_urix
    $webclient.UploadFile($uri, $file) #transfer the file
    }
    

    Check this: Powershell: compress backups and FTP transfer

    0 讨论(0)
  • 2020-12-04 15:29

    The JAMS Job Scheduler offers some cmdlets that would make this task simple. It has a variety of FTP cmdlets for secure sessions as well as date cmdlets for converting natural dates into .Net date objects such as "Last day of month":

    JAMS Job Scheduler Cmdlets

    0 讨论(0)
提交回复
热议问题