Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # FTP CREDENTIALS
- $FTPUsername = "USER"
- $FTPPassword = "PASSWORD"
- $FTPHost = "ftp://FTPURI.net"
- $FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
- $FTPCredentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword)
- #Start FTP Webrequest and configure
- $ftp=[System.Net.FtpWebRequest]::Create("$FTPHost")
- $ftp.Credentials=$FTPCredentials
- $ftp.UseBinary = $true
- $ftp.UsePassive = $true
- #Folder with new ESL
- $NewESLDir = "\egregorioStagingESLOffice3.17Release CandidateCodebasewwwroot"
- #FTP Directory to upload Files
- $destination = "ftps:/**********@***********/site/TESTING"
- #Create WebClient
- $webclient = New-Object -TypeName System.Net.WebClient
- $webclient.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword)
- #Define which files/ folders we are to upload
- $Entries = Get-ChildItem $NewESLDir -Recurse
- $Folders = $Entries | Where-Object{$_.PSIsContainer}
- $Files = $Entries | Where-Object{!$_.PSIsContainer}
- # Create Directories in FTP if needed (help me please)
- foreach ($folder in $Folders)
- {
- $FolderPath = $NewESLDir -replace "\","\" -replace ":",":"
- $DesFolder = $folder.Fullname -replace $FolderPath,$FTPHost
- $DesFolder = $DesFolder -replace "\", "/"
- # Write-Output $DesFolder
- try
- {
- $makeDirectory = [System.Net.WebRequest]::Create($DesFolder);
- $makeDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword);
- $makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory;
- $makeDirectory.GetResponse();
- #folder created successfully
- }
- catch [Net.WebException]
- {
- try {
- #if there was an error returned, check if folder already existed on server
- $checkDirectory = [System.Net.WebRequest]::Create($DesFolder);
- $checkDirectory.Credentials = New-Object System.Net.NetworkCredential($FTPUsername,$FTPSecurePassword);
- $checkDirectory.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory;
- $response = $checkDirectory.GetResponse();
- #folder already exists!
- }
- catch [Net.WebException]
- {
- #if the folder didn't exist
- }
- }
- }
- # Upload Files - Start
- foreach($entry in $Files)
- {
- $Fullname = $entry.fullname
- $Name = $entry.Name
- $FilePath = $UploadFolder -replace "\","\" -replace ":",":"
- $DesFile = $Fullname -replace $FilePath,$FTPHost
- $DesFile = $DesFile -replace "\", "/"
- # Write-Output $DesFile
- $uri = New-Object System.Uri($DesFile)
- $webclient.UploadFile($uri, $Fullname)
- }
- # Upload Files - Stop
Add Comment
Please, Sign In to add comment