A few days ago I was working in a project and I had the following situation: I had some SQL server profiler trace files (more than 50), every trace file was around 300MB and I needed to upload all files to an ftp server. Internet connection was not good and I tried to upload a single file and it took a lot of time to upload it. So, I decided to develop a PowerShell script in order to automatize this task, I was not willing to stay in front of the computer uploading all files by hand one by one. I ran this script during the night and the next morning all files were uploaded successfully. Here is the script, I hope you’ll find it useful.
#we specify the directory where all files that we want to upload are contained $Dir="C:/Dir" #ftp server $ftp = "ftp://ftp.server.com/dir/" $user = "user" $pass = "Pass" $webclient = New-Object System.Net.WebClient $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) #list every sql server trace file foreach($item in (dir $Dir "*.trc")){ "Uploading $item..." $uri = New-Object System.Uri($ftp+$item.Name) $webclient.UploadFile($uri, $item.FullName) }
Latest posts by Enrique Puig (see all)
- Powershell for the DBA: Cleaning SQL Server backup files - January 24, 2012
- SQL Server 2008: Managing Resources with Resource Governor - July 20, 2011
- Checking existing files with Powershell and SQL Server Agent - May 17, 2011
thanks , it has worked for me .
be sure that the destination folder has the good permissions to do the upload
The script is not working for me. I am getting the following the following error message:
Exception calling “UploadFile” with “2” argument(s): “The remote server returned an error: (534) 534 Policy requires SSL.
.”
At C:\Branches\ops\PSScripts\UploadWeeklyBackups.ps1:17 char:1
+ $webclient.UploadFile($uri, $latest.FullName)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : WebException
How can it be fixed?