Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 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. $ftp.Proxy = $null;
  43.  
  44. #Directory where to find pictures to upload
  45. $Dir= 'c:fffmedias'
  46.  
  47. #Directory where to save uploaded pictures
  48. $saveDir = 'c:fffsave'
  49.  
  50. #ftp server params
  51. $ftp = 'ftp://10.0.1.11:21/'
  52. $user = 'user'
  53. $pass = 'pass'
  54.  
  55. #Connect to ftp webclient
  56. $webclient = New-Object System.Net.WebClient
  57. $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  58.  
  59. #Initialize var for infinite loop
  60. $i=0
  61.  
  62. #Infinite loop
  63. while($i -eq 0){
  64.  
  65. #Pause 1 seconde before continue
  66. Start-Sleep -sec 1
  67.  
  68. #Search for pictures in directory
  69. foreach($item in (dir $Dir "*.jpg"))
  70. {
  71. #Set default network status to 1
  72. $onNetwork = "1"
  73.  
  74. #Get picture creation dateTime...
  75. $pictureDateTime = (Get-ChildItem $item.fullName).CreationTime
  76.  
  77. #Convert dateTime to timeStamp
  78. $pictureTimeStamp = (Get-Date $pictureDateTime).ToFileTime()
  79.  
  80. #Get actual timeStamp
  81. $timeStamp = (Get-Date).ToFileTime()
  82.  
  83. #Get picture lifeTime
  84. $pictureLifeTime = $timeStamp - $pictureTimeStamp
  85.  
  86. #We only treat pictures that are fully written on the disk
  87. #So, we put a 2 second delay to ensure even big pictures have been fully wirtten in the disk
  88. if($pictureLifeTime -gt "2") {
  89.  
  90. #If upload fails, we set network status at 0
  91. try{
  92.  
  93. $uri = New-Object System.Uri($ftp+$item.Name)
  94.  
  95. $webclient.UploadFile($uri, $item.FullName)
  96.  
  97. } catch [Exception] {
  98.  
  99. $onNetwork = "0"
  100. write-host $_.Exception.Message;
  101. }
  102.  
  103. #If upload succeeded, we do further actions
  104. if($onNetwork -eq "1"){
  105. "Copying $item..."
  106. Copy-Item -path $item.fullName -destination $saveDir$item
  107.  
  108. "Deleting $item..."
  109. Remove-Item $item.fullName
  110. }
  111.  
  112.  
  113. }
  114. }
  115. }
  116.  
  117. ###########################################
  118. ### upload folder content to FTP server ###
  119. ###########################################
  120.  
  121. #Local folder
  122. $localFolder= 'C:tempfolder1'
  123.  
  124. #remote folder on FTP server. Do not user '/' at begining for root
  125. $remoteFolder = 'www/folder1/'
  126.  
  127. #ftp server info
  128. $ftp = 'ftp://ftp.server.com:21/'
  129. $user = 'username'
  130. $pass = 'password'
  131.  
  132. #Connect to ftp server using webclient
  133. $webclient = New-Object System.Net.WebClient
  134. $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)
  135.  
  136. foreach($file in Get-ChildItem $localFolder)
  137. {
  138. try
  139. {
  140. $uri = New-Object System.Uri($ftp + $remoteFolder + $file.Name)
  141. write-host "uploading: $uri"
  142. $webclient.UploadFile($uri, $file.FullName)
  143. }
  144. catch [Exception]
  145. {
  146. write-host $_.Exception.Message;
  147. }
  148. }
  149.  
  150. $webclient.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement