[Powershell] FTP Download File
2024-08-20 19:15:52
# Config
$today = Get-Date -UFormat "%Y%m%d"
$LogFilePath = "d:\ftpLog_$today.txt"
$UserName = "ftpuser"
$Password = "Password01!" function REM($Msg){
$now= Get-Date
write-host "$now : $Msg" -foregroundcolor Yellow
Add-Content $LogFilePath "$now : $Msg"
} function DownloadFile($Username,$Password,$RemoteFile,$LocalFile){ try
{
$ErrorActionPreference="Stop"; if($RemoteFile -eq $null){
REM "RemoteFile is null"
return
}
if($LocalFile -eq $null){
REM "LocalFile is null"
return
} $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile)
$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$FTPRequest.UseBinary = $true
$FTPRequest.KeepAlive = $false
# Send the ftp request
$FTPResponse = $FTPRequest.GetResponse()
# Get a download stream from the server response
$ResponseStream = $FTPResponse.GetResponseStream()
# Create the target file on the local system and the download buffer
$LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create)
[byte[]]$ReadBuffer = New-Object byte[] 1024
if($ResponseStream -eq $null){
return
}
# Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength) }
while ($ReadLength -ne 0) REM "$RemoteFile 下载成功。"
}
catch
{
REM("Exception Msg: $_")
} } DownloadFile $UserName $Password "ftp://192.168.1.103/frate.csv" "c:\downloads\frate.csv"
DownloadFile $UserName $Password "ftp://192.168.1.103/bcirfp.csv" "c:\downloads\bcirfp.csv"
连接Linux机器上的ftp时候,切记文件名是区分大小写滴!
最新文章
- Python正则表达式:最短匹配
- java.lang.Comparable<;T>; 接口
- Hibernate反向工程生成DAO
- Certificate、Provisioning Profile、App ID
- android 弹出日期选择框
- virtualbox安装centos6.5碰到的问题
- 修改dbwr后台进程数量
- qt widget设置Qt::FramelessWindowHint和Qt::WA_TranslucentBackground, 会出现一个bug: 在最小化后还原时界面停止刷新
- UVa10635 - Prince and Princess(LCS转LIS)
- 2016沈阳网络赛 odd-even number
- 归并排序Java实现
- python在windows下使用setuptools安装egg文件
- 超强js博客值得学习!!!
- mysql 从一个表中查数据,插入另一个表
- Makefile工程管理器及万能模板
- JavaScript中的原型链原理
- Git中撤销提交
- bootstrap使用之多个弹窗和拖动效果[开发篇]
- process_begin: CreateProcess(NULL,......) make (e=87): 参数错误。
- Expo大作战(三十三)--expo sdk api之MapView(地图),MailComposer(磁力传感计),Lottie(动画)