Guest User

Untitled

a guest
Jul 16th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. # FTP CREDENTIALS
  2. $FTPUsername = "USER"
  3. $FTPPassword = "PASSWORD"
  4. $FTPHost = "ftp://FTPURI.net"
  5. $FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
  6. $FTPCredentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword)
  7.  
  8.  
  9. #Start FTP Webrequest and configure
  10. $ftp=[System.Net.FtpWebRequest]::Create("$FTPHost")
  11. $ftp.Credentials=$FTPCredentials
  12. $ftp.UseBinary = $true
  13. $ftp.UsePassive = $true
  14.  
  15. #Folder with new ESL
  16. $NewESLDir = "\egregorioStagingESLOffice3.17Release CandidateCodebasewwwroot"
  17.  
  18. #FTP Directory to upload Files
  19. $destination = "ftps:/**********@***********/site/TESTING"
  20.  
  21. #Create WebClient
  22. $webclient = New-Object -TypeName System.Net.WebClient
  23. $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword)
  24.  
  25. #Define which files/ folders we are to upload
  26. $Entries = Get-ChildItem $NewESLDir -Recurse
  27. $Folders = $Entries | Where-Object{$_.PSIsContainer}
  28. $Files = $Entries | Where-Object{!$_.PSIsContainer}
  29.  
  30. # Create Directories in FTP if needed (help me please)
  31. foreach ($folder in $Folders)
  32. {
  33. $FolderPath = $NewESLDir -replace "\","\" -replace ":",":"
  34. $DesFolder = $folder.Fullname -replace $FolderPath,$FTPHost
  35. $DesFolder = $DesFolder -replace "\", "/"
  36. # Write-Output $DesFolder
  37.  
  38. try
  39. {
  40. $makeDirectory = [System.Net.WebRequest]::Create($DesFolder);
  41. $makeDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword);
  42. $makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory;
  43. $makeDirectory.GetResponse();
  44. #folder created successfully
  45. }
  46. catch [Net.WebException]
  47. {
  48. try {
  49. #if there was an error returned, check if folder already existed on server
  50. $checkDirectory = [System.Net.WebRequest]::Create($DesFolder);
  51. $checkDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword);
  52. $checkDirectory.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory;
  53. $response = $checkDirectory.GetResponse();
  54. #folder already exists!
  55. }
  56. catch [Net.WebException]
  57. {
  58. #if the folder didn't exist
  59. }
  60. }
  61. }
  62. # Upload Files - Start
  63. foreach($entry in $Files)
  64. {
  65. $Fullname = $entry.fullname
  66. $Name = $entry.Name
  67. $FilePath = $UploadFolder -replace "\","\" -replace ":",":"
  68. $DesFile = $Fullname -replace $FilePath,$FTPHost
  69. $DesFile = $DesFile -replace "\", "/"
  70. # Write-Output $DesFile
  71.  
  72. $uri = New-Object System.Uri($DesFile)
  73. $webclient.UploadFile($uri, $Fullname)
  74. }
  75. # Upload Files - Stop
Add Comment
Please, Sign In to add comment