• [Powershell] FTP Download File


     1 # Config
     2 $today = Get-Date -UFormat "%Y%m%d"
     3 $LogFilePath = "d:ftpLog_$today.txt"
     4 $UserName = "ftpuser"
     5 $Password = "Password01!"
     6 
     7 
     8 function REM($Msg){
     9     $now= Get-Date
    10     write-host "$now : $Msg" -foregroundcolor Yellow
    11     Add-Content $LogFilePath "$now : $Msg"
    12 }
    13 
    14 function DownloadFile($Username,$Password,$RemoteFile,$LocalFile){
    15 
    16     try
    17     {
    18         $ErrorActionPreference="Stop";
    19 
    20         if($RemoteFile -eq $null){ 
    21             REM "RemoteFile is null"
    22             return 
    23         }
    24         if($LocalFile -eq $null){ 
    25             REM "LocalFile is null"
    26             return 
    27         }
    28     
    29         $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
    30         $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) 
    31         $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile 
    32         $FTPRequest.UseBinary = $true
    33         $FTPRequest.KeepAlive = $false
    34         # Send the ftp request
    35         $FTPResponse = $FTPRequest.GetResponse() 
    36         # Get a download stream from the server response 
    37         $ResponseStream = $FTPResponse.GetResponseStream() 
    38         # Create the target file on the local system and the download buffer 
    39         $LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) 
    40         [byte[]]$ReadBuffer = New-Object byte[] 1024 
    41         if($ResponseStream -eq $null){
    42             return
    43         }
    44         # Loop through the download 
    45         do { 
    46             $ReadLength = $ResponseStream.Read($ReadBuffer,0,1024) 
    47             $LocalFileFile.Write($ReadBuffer,0,$ReadLength) 
    48         
    49         } 
    50         while ($ReadLength -ne 0)
    51 
    52         REM "$RemoteFile 下载成功。"
    53     }
    54     catch
    55     {
    56         REM("Exception Msg: $_")
    57     }
    58 
    59 }
    60 
    61 
    62 
    63 DownloadFile $UserName $Password "ftp://192.168.1.103/frate.csv" "c:downloadsfrate.csv"
    64 DownloadFile $UserName $Password "ftp://192.168.1.103/bcirfp.csv" "c:downloadscirfp.csv" 

    连接Linux机器上的ftp时候,切记文件名是区分大小写滴!

  • 相关阅读:
    工坊第五天
    工坊第四天
    工坊第三天
    工坊第二天
    工坊第一天
    莫队 优雅暴力出奇迹
    状压 DP 总结
    关于MatlabGUI清除WorkSpace的用法
    ArduinoNano卡在上传,无法烧录
    两轮差速驱动机器人的坐标轨迹计算
  • 原文地址:https://www.cnblogs.com/wpsl5168/p/3196990.html
Copyright © 2020-2023  润新知