[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ftp using powershell

Posted on 2016-10-21
11
Medium Priority
?
125 Views
Last Modified: 2016-10-26
I've tried a couple of different scripts I've found online, and I thought I had one working, but find myself back at square one.
I landed on this one today, ran it from my workstation, pointing to the remote file, and it worked.
I placed the script on the server, where it will be automated, and it writes the data, but once the rs closes, the file is gone...I have no idea why?
$ftpFile = 'ftp://destination/folder/filename.csv'
$ftp = [System.Net.FtpWebRequest]::Create($ftpFile)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential('xxxxxxx',xxxxxx') 
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$content = [System.IO.File]::ReadAllBytes($file)
$ftp.ContentLength = $content.Length
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
$rs.Close()
$rs.Dispose()

Open in new window

0
Comment
Question by:sirbounty
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 4
11 Comments
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41854332
Where are you defining $file?
$content = [System.IO.File]::ReadAllBytes($file)

Open in new window

0
 
LVL 67

Author Comment

by:sirbounty
ID: 41854415
via a parameter in my function.
It's a string value of the source file's location on the local server.
0
 
LVL 13

Accepted Solution

by:
Dustin Saunders earned 2000 total points
ID: 41854465
Your code works fine for me.  Are you sure it's being deleted (actually in the FTP folder)?  Some FTP rights might not let you see the files that exist depending on permissions.
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 67

Author Comment

by:sirbounty
ID: 41854512
Yeah, I'm using the same credentials to the ftp, from both devices.
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41854528
Hmm...  I can't get it to replicate.  What OS is it running on when having the issue?  As a scheduled task?

So you can see the file with the correct file size then it vanishes. correct?  Does it delete itself after .dispose or after .close (can you add 'start-sleep -s 10' between each of those commands to see at what point it does the deletion?)
0
 
LVL 67

Author Comment

by:sirbounty
ID: 41856811
It runs as a task, yes, on a 2k8r2 server.  But running it manually on that server still fails.
On my Win7 desktop, it worked.  However, I need to ensure I'm using the exact code in both environments - there may have been some slight tweaks when transferring.
I'll run again and see.

As to the last question - I think it was at the dispose - I'll step through the code waiting 10 seconds and see what happens.  Thanks for the suggestions.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 41856820
Nope, it happened after close() was sent...weird.  I'll try waiting longer.
At the $rs.write... the file is currently 'there', but with 0 bytes.  I'm going to let it sit here for a while and check it later.
0
 
LVL 67

Author Comment

by:sirbounty
ID: 41856844
At $rs.write...
ftp> ls -l
200 PORT command successful.
150 Opening ASCII mode data connection for file list.
total 2
-rw-rw-rw- 1   root            root    0       Oct 24 07:03 filename.csv
226 Transfer complete.
ftp: 102 bytes received in 0.01Seconds 11.33Kbytes/sec.

Open in new window

After waiting a bit...
ftp> ls -l
200 PORT command successful.
150 Opening ASCII mode data connection for file list.
total 16834
-rw-rw-rw- 1   root            root    13680039 Oct 24 07:04 filename.csv
226 Transfer complete.
ftp: 107 bytes received in 0.02Seconds 4.65Kbytes/sec.

Open in new window

After waiting a bit longer...
ftp> ls -l
200 PORT command successful.
150 Opening ASCII mode data connection for file list.
total 16834
-rwxr-xr-x 1   30001           30000   13680268 Oct 24 07:08 filename.csv
226 Transfer complete.
ftp: 107 bytes received in 0.03Seconds 4.12Kbytes/sec.

Open in new window

At this point, if I try $rs.close(), it throws an exception (remote server returned Page type unknown)
0
 
LVL 13

Expert Comment

by:Dustin Saunders
ID: 41857022
What about the FTP server, what platform/software is it?  Do you know if there is a file size limit?

If you test with a small 1kb text file does it work?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 41857382
I don't know much about the target server.  I'll need to contact that area to find out more, but yes I can try with a 1k file.
0
 
LVL 67

Author Closing Comment

by:sirbounty
ID: 41860661
I'll just add the sleep to the process - thanks for the help
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question