Need to move the file from client's ftp to local server using powershell

  • I don't know PowerShell but I have no choice but to use it for what I am trying to achieve. I need to move the backup file which client put on their ftp folder everyday. I am also given a credential which I use. I am using FileZilla to connect to the host, and then moving the file on the server every other day. I googled a PS script and this is what I found. Now when I run it, it throws an error. Can someone please help me with this?
     
    $UserName = 'myusername'
    $Password = 'mypwd'
    $LocalFilePath = 'C:\backup\'
    $RemoteFileName = 'DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK'
    $ServerName = 'ftp.thinkparkweb.com'
    $webclient = New-Object System.Net.WebClient
    $webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)
    $uri = New-Object System.Uri(“ftp.parkweb.com/$(DIFF/DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK)”)
    $webclient.DownloadFile($uri, $LocalFilePath)

    FULL folder contains weekly full backup
    DIFF folder contains daily diff backups.

    Let me know if I can provide with more information.

  • Posting the error you get would be helpful.

  • Gazareth - Thursday, July 6, 2017 9:42 AM

    Posting the error you get would be helpful.

  • Thanks.
    Your URI is incorrectly formatted - what's the path of the remote file?
    Assuming it's at *ftp*\DIFF\*file* I think your uri parameter should be:

    $uri = New-Object System.Uri("$servername/DIFF/$remotefilename")

    Although this will only work for the differential files, you need something to determine if you're grabbing the full or diff.

  • Gazareth - Thursday, July 6, 2017 11:00 AM

    Thanks.
    Your URI is incorrectly formatted - what's the path of the remote file?
    Assuming it's at *ftp*\DIFF\*file* I think your uri parameter should be:

    $uri = New-Object System.Uri("$servername/DIFF/$remotefilename")

    Although this will only work for the differential files, you need something to determine if you're grabbing the full or diff.

    Hmm, Still No LUCK

  • As a thought, are you sure you have the correct server name?

    I see that you have a $servername variable and then a different value in your URI creation string.
    What I would try first is get it working with hard-coded values for the URI and after that works build your URI string to be more dynamic.  The URI you provided was
    "ftp.thinkparkweb.com/DIFF/$DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK)"

    I am willing to bet that last bracket and the $ are not part of the file name.  Try hard-coding it in as:

    "ftp.thinkparkweb.com/DIFF/DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK"

    If that works, then you just need to get the variables working nicer.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • I have the complete path of where the file is on the client's server. But I am still running into the same issue.  

    $UserName = 'myusername'
    $Password = 'mypwd'
    $LocalFilePath = 'C:\backup\'
    $RemoteFileName = 'DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK'
    $ServerName = 'ftp.thinkparkweb.com'
    $webclient = New-Object System.Net.WebClient
    $webclient.Credentials = New-Object System.Net.NetworkCredential($UserName, $Password)
    $uri = New-Object System.Uri(“52.14.80.144\LocalUser\vance\DIFF\DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK”)
    $webclient.DownloadFile($uri, $LocalFilePath)

    New-Object : Exception calling ".ctor" with "1" argument(s): "Invalid URI: The format of the URI could not be determine
    d."
    At line:1 char:18
    + $uri = New-Object <<<< System.Uri("52.14.80.144\LocalUser\vance\DIFF\DC2THKHDB04_VA24138_Live_DIFF_20170710_030000")
      + CategoryInfo    : InvalidOperation: (:) [New-Object], MethodInvocationException
      + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

    PS C:\Users> $webclient.DownloadFile($uri, $LocalFilePath)
    Exception calling "DownloadFile" with "2" argument(s): "Value cannot be null.
    Parameter name: address"
    At line:1 char:24
    + $webclient.DownloadFile <<<< ($uri, $LocalFilePath)
      + CategoryInfo    : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException

  • I think your URI is wrong.

    Was doing a bit more snooping (as I'm not 100% familiar with how that works) and I THINK you are missing the URI type in your URI.  
    Change it from:
    $uri = New-Object System.Uri(“52.14.80.144\LocalUser\vance\DIFF\DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK”)
    to
    $uri = New-Object System.Uri(“ftp://52.14.80.144/LocalUser/vance/DIFF/DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAK”)

    The difference - I added "ftp://" to the start and changed all of your \'s to /'s which is standard for web type requests (including FTP).

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • bmg002 - Monday, July 10, 2017 11:20 AM

    I think your URI is wrong.

    Was doing a bit more snooping (as I'm not 100% familiar with how that works) and I THINK you are missing the URI type in your URI.  
    Change it from:
    $uri = New-Object System.Uri(“52.14.80.144\LocalUser\vance\DIFF\DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAKâ€)
    to
    $uri = New-Object System.Uri(“ftp://52.14.80.144/LocalUser/vance/DIFF/DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAKâ&#8364😉

    The difference - I added "ftp://" to the start and changed all of your \'s to /'s which is standard for web type requests (including FTP).

    I appreciate your help. I did change the uri path but this is the new error.

    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At line:1 char:24
    + $webclient.DownloadFile <<<< ($Uri, $LocalFilePath)
      + CategoryInfo    : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException

  • newdba2017 - Monday, July 10, 2017 11:55 AM

    bmg002 - Monday, July 10, 2017 11:20 AM

    I think your URI is wrong.

    Was doing a bit more snooping (as I'm not 100% familiar with how that works) and I THINK you are missing the URI type in your URI.  
    Change it from:
    $uri = New-Object System.Uri(“52.14.80.144\LocalUser\vance\DIFF\DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAKâ€)
    to
    $uri = New-Object System.Uri(“ftp://52.14.80.144/LocalUser/vance/DIFF/DC2THKHDB04_VA24138_Live_DIFF_20170706_030000.BAKâ&#8364😉

    The difference - I added "ftp://" to the start and changed all of your \'s to /'s which is standard for web type requests (including FTP).

    I appreciate your help. I did change the uri path but this is the new error.

    Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
    At line:1 char:24
    + $webclient.DownloadFile <<<< ($Uri, $LocalFilePath)
      + CategoryInfo    : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : DotNetMethodException

    Cool!  Making progress.  The next error (the one above) is because your $LocalFilePath should actually be a file not a path.  DownloadFile takes a URI and a file path plus file name.
    So if you change $LocalFilePath to be "C:\Backup\bakupfile.bak", I bet it will succeed.

    So, looking at your original code, you'd likely want to assign $LocalFilePath to be "C:\Backup\" + $RemoteFileName (mind you, you'd need to assign $RemoteFileName before $LocalFilePath).

    Doest that help get rid of that last error?

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • This is the new error. I am sure it is going to work, I am getting excited.. 

    Exception calling "DownloadFile" with "2" argument(s): "The remote server returned an error: (534) 534 Policy requires
    SSL.
    ."
    At line:1 char:1
    + $webclient.DownloadFile($Uri, $LocalFilePath)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo    : NotSpecified: (:) [], MethodInvocationException
      + FullyQualifiedErrorId : WebException

  • SSL makes this a whole new beast actually.  You need to encrypt the data before sending it out to the FTP server and decrypt it upon reciept.

    I suggest you take a look at the top most reply from here:
    https://stackoverflow.com/questions/265339/whats-the-best-way-to-automate-secure-ftp-in-powershell

    As that'll give you a bit more help.  But basically, you need to make an FTP Web Request object to connect to the FTP server and pull the data from the FTP into the file using a stream reader (IO.FileStream).
    It is close to what you have, but adds a few extra FTP specific parameters such as "EnableSSL" and "UseBinary".

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

Viewing 12 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic. Login to reply