Advertisement
Guest User

Untitled

a guest
Sep 27th, 2017
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. # create the FtpWebRequest and configure it
  2. $ftp = [System.Net.FtpWebRequest]::Create("ftp://localhost/me.png")
  3. $ftp = [System.Net.FtpWebRequest]$ftp
  4. $ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
  5. $ftp.Credentials = new-object System.Net.NetworkCredential("anonymous","anonymous@localhost")
  6. $ftp.UseBinary = $true
  7. $ftp.UsePassive = $true
  8. # read in the file to upload as a byte array
  9. $content = [System.IO.File]::ReadAllBytes("C:me.png")
  10. $ftp.ContentLength = $content.Length
  11. # get the request stream, and write the bytes into it
  12. $rs = $ftp.GetRequestStream()
  13. $rs.Write($content, 0, $content.Length)
  14. # be sure to clean up after ourselves
  15. $rs.Close()
  16. $rs.Dispose()
  17.  
  18. $File = "D:Devsomefilename.zip";
  19. $ftp = "ftp://username:password@example.com/pub/incoming/somefilename.zip";
  20.  
  21. Write-Host -Object "ftp url: $ftp";
  22.  
  23. $webclient = New-Object -TypeName System.Net.WebClient;
  24. $uri = New-Object -TypeName System.Uri -ArgumentList $ftp;
  25.  
  26. Write-Host -Object "Uploading $File...";
  27.  
  28. $webclient.UploadFile($uri, $File);
  29.  
  30. ftp -s:script.txt
  31.  
  32. $server = "ftp.lolcats.com"
  33. $filelist = "file1.txt file2.txt"
  34.  
  35. "open $server
  36. user $user $password
  37. binary
  38. cd $dir
  39. " +
  40. ($filelist.split(' ') | %{ "put ""$_""`n" }) | ftp -i -in
  41.  
  42. #Directory where to find pictures to upload
  43. $Dir= 'c:fffmedias'
  44.  
  45. #Directory where to save uploaded pictures
  46. $saveDir = 'c:fffsave'
  47.  
  48. #ftp server params
  49. $ftp = 'ftp://10.0.1.11:21/'
  50. $user = 'user'
  51. $pass = 'pass'
  52.  
  53. #Connect to ftp webclient
  54. $webclient = New-Object System.Net.WebClient
  55. $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  56.  
  57. #Initialize var for infinite loop
  58. $i=0
  59.  
  60. #Infinite loop
  61. while($i -eq 0){
  62.  
  63. #Pause 1 seconde before continue
  64. Start-Sleep -sec 1
  65.  
  66. #Search for pictures in directory
  67. foreach($item in (dir $Dir "*.jpg"))
  68. {
  69. #Set default network status to 1
  70. $onNetwork = "1"
  71.  
  72. #Get picture creation dateTime...
  73. $pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
  74.  
  75. #Convert dateTime to timeStamp
  76. $pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
  77.  
  78. #Get actual timeStamp
  79. $timeStamp = (Get-Date).ToFileTime()
  80.  
  81. #Get picture lifeTime
  82. $pictureLifeTime = $timeStamp - $pictureTimeStamp
  83.  
  84. #We only treat pictures that are fully written on the disk
  85. #So, we put a 2 second delay to ensure even big pictures have been fully wirtten in the disk
  86. if($pictureLifeTime -gt "2") {
  87.  
  88. #If upload fails, we set network status at 0
  89. try{
  90.  
  91. $uri = New-Object System.Uri($ftp+$item.Name)
  92.  
  93. $webclient.UploadFile($uri, $item.FullName)
  94.  
  95. } catch [Exception] {
  96.  
  97. $onNetwork = "0"
  98. write-host $_.Exception.Message;
  99. }
  100.  
  101. #If upload succeeded, we do further actions
  102. if($onNetwork -eq "1"){
  103. "Copying $item..."
  104. Copy-Item -path $item.fullName -destination $saveDir$item
  105.  
  106. "Deleting $item..."
  107. Remove-Item $item.fullName
  108. }
  109.  
  110.  
  111. }
  112. }
  113. }
  114.  
  115. #Add-FtpFile -ftpFilePath "ftp://myHost.com/folder/somewhere/uploaded.txt" -localFile "C:tempfile.txt" -userName "User" -password "pw"
  116. function Add-FtpFile($ftpFilePath, $localFile, $username, $password) {
  117. $ftprequest = New-FtpRequest -sourceUri $ftpFilePath -method ([System.Net.WebRequestMethods+Ftp]::UploadFile) -username $username -password $password
  118. Write-Host "$($ftpRequest.Method) for '$($ftpRequest.RequestUri)' complete'"
  119. $content = $content = [System.IO.File]::ReadAllBytes($localFile)
  120. $ftprequest.ContentLength = $content.Length
  121. $requestStream = $ftprequest.GetRequestStream()
  122. $requestStream.Write($content, 0, $content.Length)
  123. $requestStream.Close()
  124. $requestStream.Dispose()
  125. }
  126.  
  127. #Add-FtpFolderWithFiles -sourceFolder "C:temp" -destinationFolder "ftp://myHost.com/folder/somewhere/" -userName "User" -password "pw"
  128. function Add-FtpFolderWithFiles($sourceFolder, $destinationFolder, $userName, $password) {
  129. Add-FtpDirectory $destinationFolder $userName $password
  130. $files = Get-ChildItem $sourceFolder -File
  131. foreach($file in $files) {
  132. $uploadUrl ="$destinationFolder/$($file.Name)"
  133. Add-FtpFile -ftpFilePath $uploadUrl -localFile $file.FullName -username $userName -password $password
  134. }
  135. }
  136.  
  137. #Add-FtpFolderWithFilesRecursive -sourceFolder "C:temp" -destinationFolder "ftp://myHost.com/folder/" -userName "User" -password "pw"
  138. function Add-FtpFolderWithFilesRecursive($sourceFolder, $destinationFolder, $userName, $password) {
  139. Add-FtpFolderWithFiles -sourceFolder $sourceFolder -destinationFolder $destinationFolder -userName $userName -password $password
  140. $subDirectories = Get-ChildItem $sourceFolder -Directory
  141. $fromUri = new-object System.Uri($sourceFolder)
  142. foreach($subDirectory in $subDirectories) {
  143. $toUri = new-object System.Uri($subDirectory.FullName)
  144. $relativeUrl = $fromUri.MakeRelativeUri($toUri)
  145. $relativePath = [System.Uri]::UnescapeDataString($relativeUrl.ToString())
  146. $lastFolder = $relativePath.Substring($relativePath.LastIndexOf("/")+1)
  147. Add-FtpFolderWithFilesRecursive -sourceFolder $subDirectory.FullName -destinationFolder "$destinationFolder/$lastFolder" -userName $userName -password $password
  148. }
  149.  
  150. $ftp.Proxy = $null;
  151.  
  152. $webclient = New-Object System.Net.WebClient
  153. Register-ObjectEvent -InputObject $webclient -EventName "UploadProgressChanged" -Action { Write-Progress -Activity "Upload progress..." -Status "Uploading" -PercentComplete $EventArgs.ProgressPercentage } > $null
  154.  
  155. $File = "filename.zip"
  156. $ftp = "ftp://user:password@server/filename.zip"
  157. $uri = New-Object System.Uri($ftp)
  158. try{
  159. $webclient.UploadFileAsync($uri, $File)
  160. }
  161. catch [Net.WebException]
  162. {
  163. Write-Host $_.Exception.ToString() -foregroundcolor red
  164. }
  165. while ($webclient.IsBusy) { continue }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement