Power FTP Upload not working

I have powershell script to upload the file to FTP server.

$Now = Get-Date
$Dirs = Get-ChildItem "C:\Program Files\Microsoft\Exchange Server\TransportRoles\Logs\MessageTracking\*.log" -Recurse |Where-Object {(!($_.PSIsContainer))} 
foreach ( $Dir in $Dirs)

{

#$Now - $Dir.LastWriteTime to get "TotalDays,TotalHours...ect)

if ((($Now - $Dir.LastWriteTime).TotalMinutes -lt 40)-and (($Now - $Dir.LastWriteTime).TotalMinutes -ge 0)) {

$Dir.FullName

#we specify the directory where all files that we want to upload  
#$Dir="C:/Dir"    
 
#ftp server 
$ftp = "ftp://1.1.1.1/data/Exchange" 
$user = "poc" 
$pass = "F1"  
 
$webclient = New-Object System.Net.WebClient 
 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
 
    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$dir.Name) 
    $webclient.UploadFile($uri,$Dir.FullName) 

}
}

Open in new window


I keep getting error:
Exception calling "UploadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At C:\Temp\Monitoring\FTP_PowerShell_Upload.ps1:27 char:26
+     $webclient.UploadFile <<<< ($uri,$Dir.FullName)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException
LVL 19
suriyaehnopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Randy DownsOWNERCommented:
You may want to try this code.

function Send-FtpFile {
    [CmdletBinding()]
    param (
          [ValidateScript({ Test-Path -Path $_; })]
          [string] $Path
        , [string] $Destination
        , [string] $Username
        , [string] $Password
    )
    $Credential = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $Username,$Password;

    # Create the FTP request and upload the file
    $FtpRequest = [System.Net.FtpWebRequest][System.Net.WebRequest]::Create($Destination);
    $FtpRequest.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile;
    $FtpRequest.Credentials = $Credential;

    # Get the request stream, and write the file bytes to the stream
    $RequestStream = $FtpRequest.GetRequestStream();
    Get-Content -Path $Path -Encoding Byte | % { $RequestStream.WriteByte($_); };
    $RequestStream.Close();

    # Get the FTP response
    [System.Net.FtpWebResponse]$FtpRequest.GetResponse();
}

Send-FtpFile -Path 'C:\Users\Trevor\Downloads\asdf.jpg' `
    -Destination 'ftp://google.com/folder/asdf.jpg' `
    -Username MyUsername -Password MyPassword;

Open in new window

0
suriyaehnopAuthor Commented:
Hi Randy,

I didn't try your code yet, there is requirement whereby the only file which "totalminutes" less than 20 are allow to upload.

That why i use :

if ((($Now - $Dir.LastWriteTime).TotalMinutes -lt 40)-and (($Now - $Dir.LastWriteTime).TotalMinutes -ge 0))

Open in new window

0
Randy DownsOWNERCommented:
Maybe you can put the FTP code above in your if loop.

$Dir.FullName

#we specify the directory where all files that we want to upload  
#$Dir="C:/Dir"

if ((($Now - $Dir.LastWriteTime).TotalMinutes -lt 40)-and (($Now - $Dir.LastWriteTime).TotalMinutes -ge 0)) {
Send-FtpFile -Path +$dir.Name `
    -Destination 'ftp://google.com/folder/asdf.jpg' `
    -Username MyUsername -Password MyPassword;
}

Open in new window

0
ON-DEMAND: 10 Easy Ways to Lose a Password

Learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees in this on-demand webinar. We cover the importance of multi-factor authentication and how these solutions can better protect your business!

suriyaehnopAuthor Commented:
My script seem working now. Not use if "New-Object System.Net.WebClient" doesn't like the long souce path or space. The log is in "C:\Program Files\Microsoft\Exchange Server\TransportRoles\Logs\MessageTracking\*.log".

When I copy the log that meet the criteria to another shorter path and no space, the log is uploading to ftp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Randy DownsOWNERCommented:
Cool, make the log path extra short like C:\logs & you shouldn't have any more issues
0
suriyaehnopAuthor Commented:
Please close this question. I had solve the issue by myself
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.