PowerShell을 사용하여 FTP로 파일 업로드
PowerShell을 사용하여 FTP로 파일을 익명 FTP 서버로 전송하고 싶습니다. 나는 추가 패키지를 사용하지 않을 것입니다. 어떻게?
스크립트가 중단되거나 충돌 할 위험이 없어야합니다.
제어 할 수없는 것들이 있기 때문에 스크립트가 중단되거나 충돌하지 않도록 100 % 방탄 할 수 있을지 확신 할 수 없습니다 (업로드 중에 서버가 전원을 잃는 경우 어떻게됩니까?). 이것은 시작을위한 견고한 기반을 제공해야합니다.
# create the FtpWebRequest and configure it
$ftp = [System.Net.FtpWebRequest]::Create("ftp://localhost/me.png")
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("anonymous","anonymous@localhost")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes("C:\me.png")
$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()
다른 방법도 있습니다. 다음 스크립트를 사용했습니다.
$File = "D:\Dev\somefilename.zip";
$ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip";
Write-Host -Object "ftp url: $ftp";
$webclient = New-Object -TypeName System.Net.WebClient;
$uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
Write-Host -Object "Uploading $File...";
$webclient.UploadFile($uri, $File);
그리고 다음 명령을 사용하여 Windows FTP 명령 줄 유틸리티에 대해 스크립트를 실행할 수 있습니다.
ftp -s:script.txt
( 이 기사 확인 )
SO에 대한 다음 질문도 이에 대한 답변입니다. FTP 업로드 및 다운로드 스크립트 방법?
나는 이것이 가장 높은 득표를 한 솔루션보다 더 우아하다고 주장하지 않을 것입니다 ...하지만 이것은 자체 방식으로 멋지다 (적어도 내 마음에는 LOL).
$server = "ftp.lolcats.com"
$filelist = "file1.txt file2.txt"
"open $server
user $user $password
binary
cd $dir
" +
($filelist.split(' ') | %{ "put ""$_""`n" }) | ftp -i -in
보시다시피, 내장 Windows FTP 클라이언트를 사용합니다. 훨씬 더 짧고 간단합니다. 예, 실제로 이것을 사용했으며 작동합니다!
나는 최근에 FTP와 통신하기위한 몇 가지 기능을 powershell에 썼습니다 . https://github.com/AstralisSomnium/PowerShell-No-Library-Just-Functions/blob/master/FTPModule.ps1을 참조 하십시오 . 아래 두 번째 기능은 전체 로컬 폴더를 FTP로 보낼 수 있습니다. 모듈에는 폴더와 파일을 재귀 적으로 제거 / 추가 / 읽는 기능도 있습니다.
#Add-FtpFile -ftpFilePath "ftp://myHost.com/folder/somewhere/uploaded.txt" -localFile "C:\temp\file.txt" -userName "User" -password "pw"
function Add-FtpFile($ftpFilePath, $localFile, $username, $password) {
$ftprequest = New-FtpRequest -sourceUri $ftpFilePath -method ([System.Net.WebRequestMethods+Ftp]::UploadFile) -username $username -password $password
Write-Host "$($ftpRequest.Method) for '$($ftpRequest.RequestUri)' complete'"
$content = $content = [System.IO.File]::ReadAllBytes($localFile)
$ftprequest.ContentLength = $content.Length
$requestStream = $ftprequest.GetRequestStream()
$requestStream.Write($content, 0, $content.Length)
$requestStream.Close()
$requestStream.Dispose()
}
#Add-FtpFolderWithFiles -sourceFolder "C:\temp\" -destinationFolder "ftp://myHost.com/folder/somewhere/" -userName "User" -password "pw"
function Add-FtpFolderWithFiles($sourceFolder, $destinationFolder, $userName, $password) {
Add-FtpDirectory $destinationFolder $userName $password
$files = Get-ChildItem $sourceFolder -File
foreach($file in $files) {
$uploadUrl ="$destinationFolder/$($file.Name)"
Add-FtpFile -ftpFilePath $uploadUrl -localFile $file.FullName -username $userName -password $password
}
}
#Add-FtpFolderWithFilesRecursive -sourceFolder "C:\temp\" -destinationFolder "ftp://myHost.com/folder/" -userName "User" -password "pw"
function Add-FtpFolderWithFilesRecursive($sourceFolder, $destinationFolder, $userName, $password) {
Add-FtpFolderWithFiles -sourceFolder $sourceFolder -destinationFolder $destinationFolder -userName $userName -password $password
$subDirectories = Get-ChildItem $sourceFolder -Directory
$fromUri = new-object System.Uri($sourceFolder)
foreach($subDirectory in $subDirectories) {
$toUri = new-object System.Uri($subDirectory.FullName)
$relativeUrl = $fromUri.MakeRelativeUri($toUri)
$relativePath = [System.Uri]::UnescapeDataString($relativeUrl.ToString())
$lastFolder = $relativePath.Substring($relativePath.LastIndexOf("/")+1)
Add-FtpFolderWithFilesRecursive -sourceFolder $subDirectory.FullName -destinationFolder "$destinationFolder/$lastFolder" -userName $userName -password $password
}
}
가장 쉬운 방법
PowerShell을 사용하여 FTP 서버에 바이너리 파일을 업로드하는 가장 간단한 방법은 다음을 사용하는 것입니다 WebClient.UploadFile
.
$client = New-Object System.Net.WebClient
$client.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$client.UploadFile("ftp://ftp.example.com/remote/path/file.zip", "C:\local\path\file.zip")
고급 옵션
더 많은 제어가 필요 WebClient
하지만 TLS / SSL 암호화 등을 제공하지 않는 경우 FtpWebRequest
. 쉬운 방법은 다음을 FileStream
사용하여 FTP 스트림에 복사하는 것입니다 Stream.CopyTo
.
$request = [Net.WebRequest]::Create("ftp://ftp.example.com/remote/path/file.zip")
$request.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$fileStream = [System.IO.File]::OpenRead("C:\local\path\file.zip")
$ftpStream = $request.GetRequestStream()
$fileStream.CopyTo($ftpStream)
$ftpStream.Dispose()
$fileStream.Dispose()
진행 상황 모니터링
업로드 진행 상황을 모니터링해야하는 경우 콘텐츠를 청크별로 직접 복사해야합니다.
$request = [Net.WebRequest]::Create("ftp://ftp.example.com/remote/path/file.zip")
$request.Credentials = New-Object System.Net.NetworkCredential("username", "password")
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$fileStream = [System.IO.File]::OpenRead("C:\local\path\file.zip")
$ftpStream = $request.GetRequestStream()
$buffer = New-Object Byte[] 10240
while (($read = $fileStream.Read($buffer, 0, $buffer.Length)) -gt 0)
{
$ftpStream.Write($buffer, 0, $read)
$pct = ($fileStream.Position / $fileStream.Length)
Write-Progress `
-Activity "Uploading" -Status ("{0:P0} complete:" -f $pct) `
-PercentComplete ($pct * 100)
}
$fileStream.CopyTo($ftpStream)
$ftpStream.Dispose()
$fileStream.Dispose()
폴더 업로드
폴더의 모든 파일 을 업로드하려면 PowerShell 스크립트를 참조
하여 전체 폴더를 FTP에 업로드하세요.
진행률 표시 줄이 있기 때문에 내 멋진 버전이 있습니다. :-)
완전히 쓸모없는 기능이지만 여전히 멋지게 보입니다 \ m / \ m /
$webclient = New-Object System.Net.WebClient
Register-ObjectEvent -InputObject $webclient -EventName "UploadProgressChanged" -Action { Write-Progress -Activity "Upload progress..." -Status "Uploading" -PercentComplete $EventArgs.ProgressPercentage } > $null
$File = "filename.zip"
$ftp = "ftp://user:password@server/filename.zip"
$uri = New-Object System.Uri($ftp)
try{
$webclient.UploadFileAsync($uri, $File)
}
catch [Net.WebException]
{
Write-Host $_.Exception.ToString() -foregroundcolor red
}
while ($webclient.IsBusy) { continue }
추신. "작동이 중지 되었습니까, 아니면 저속 ASDL 연결입니까?"라고 궁금해 할 때 많은 도움이됩니다.
이와 같이 PowerShell을 통해 파일 업로드를 간단히 처리 할 수 있습니다. 전체 프로젝트는 여기 Github에서 사용할 수 있습니다. https://github.com/edouardkombo/PowerShellFtp
#Directory where to find pictures to upload
$Dir= 'c:\fff\medias\'
#Directory where to save uploaded pictures
$saveDir = 'c:\fff\save\'
#ftp server params
$ftp = 'ftp://10.0.1.11:21/'
$user = 'user'
$pass = 'pass'
#Connect to ftp webclient
$webclient = New-Object System.Net.WebClient
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
#Initialize var for infinite loop
$i=0
#Infinite loop
while($i -eq 0){
#Pause 1 seconde before continue
Start-Sleep -sec 1
#Search for pictures in directory
foreach($item in (dir $Dir "*.jpg"))
{
#Set default network status to 1
$onNetwork = "1"
#Get picture creation dateTime...
$pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
#Convert dateTime to timeStamp
$pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
#Get actual timeStamp
$timeStamp = (Get-Date).ToFileTime()
#Get picture lifeTime
$pictureLifeTime = $timeStamp - $pictureTimeStamp
#We only treat pictures that are fully written on the disk
#So, we put a 2 second delay to ensure even big pictures have been fully wirtten in the disk
if($pictureLifeTime -gt "2") {
#If upload fails, we set network status at 0
try{
$uri = New-Object System.Uri($ftp+$item.Name)
$webclient.UploadFile($uri, $item.FullName)
} catch [Exception] {
$onNetwork = "0"
write-host $_.Exception.Message;
}
#If upload succeeded, we do further actions
if($onNetwork -eq "1"){
"Copying $item..."
Copy-Item -path $item.fullName -destination $saveDir$item
"Deleting $item..."
Remove-Item $item.fullName
}
}
}
}
Goyuix의 솔루션 은 훌륭하게 작동하지만 제시된대로 "요청 된 FTP 명령은 HTTP 프록시를 사용할 때 지원되지 않습니다."라는 오류가 발생합니다.
$ftp.UsePassive = $true
나를 위해 문제를 해결 한 후 다음 줄을 추가하십시오 .
$ftp.Proxy = $null;
이 기능을 사용할 수 있습니다.
function SendByFTP {
param (
$userFTP = "anonymous",
$passFTP = "anonymous",
[Parameter(Mandatory=$True)]$serverFTP,
[Parameter(Mandatory=$True)]$localFile,
[Parameter(Mandatory=$True)]$remotePath
)
if(Test-Path $localFile){
$remoteFile = $localFile.Split("\")[-1]
$remotePath = Join-Path -Path $remotePath -ChildPath $remoteFile
$ftpAddr = "ftp://${userFTP}:${passFTP}@${serverFTP}/$remotePath"
$browser = New-Object System.Net.WebClient
$url = New-Object System.Uri($ftpAddr)
$browser.UploadFile($url, $localFile)
}
else{
Return "Unable to find $localFile"
}
}
이 기능은 지정된 파일을 FTP로 전송합니다 . 다음 매개 변수를 사용하여 함수를 호출해야합니다.
- userFTP = 기본적으로 "익명"또는 사용자 이름
- passFTP = 기본적으로 "익명"또는 비밀번호
- serverFTP = FTP 서버의 IP 주소
- localFile = 보낼 파일
- remotePath = FTP 서버의 경로
예 :
SendByFTP -userFTP "USERNAME" -passFTP "PASSWORD" -serverFTP "MYSERVER" -localFile "toto.zip" -remotePath "path/on/the/FTP/"
참고URL : https://stackoverflow.com/questions/1867385/upload-files-with-ftp-using-powershell
'Programing' 카테고리의 다른 글
절대 위치 지정을 사용하는 것이 나쁜 습관으로 간주됩니까? (0) | 2020.11.07 |
---|---|
Lua-현재 시간 (밀리 초) (0) | 2020.11.07 |
노드가 'N'인 경우 가능한 이진 및 이진 검색 트리는 몇 개입니까? (0) | 2020.11.07 |
Scala-배열 인쇄 (0) | 2020.11.07 |
Java에서 for 루프를 1이 아닌 증분으로 늘리는 방법 (0) | 2020.11.07 |