Link to home
Start Free TrialLog in
Avatar of Kasper Katzmann
Kasper KatzmannFlag for Denmark

asked on

FTP and Powershell doesn't wotk together

I am trying to upload a file with FTP and when I log on manually with ie. Explorer, I am granted acces. I have cross checked the username and password. But it won't work.
Any ideas why this script:
    #I specify the directory where all files that we want to upload  
    $Dir="C:\Tools\FTP"    
 
    #ftp server 
    $ftp = "ftp://ftp.DOMAIN.com/list_import/" 
    $user = "3213213" 
    $pass = "YthsTYkW"  
 
    $webclient = New-Object System.Net.WebClient 
 
    $webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass)  
    $item = Get-Item "$dir\MyFile.csv"

    "Uploading $item..." 
    $uri = New-Object System.Uri($ftp+$item.Name) 
    $webclient.UploadFile($uri, $item.FullName)  

Open in new window

keeps giving me this error:
Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (530) Not logged in."
At line:15 char:5
+     $webclient.UploadFile($uri, $item.FullName)
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

Open in new window

Avatar of Jeremy Weisinger
Jeremy Weisinger

I think you need to use the System.Net.FtpWebRequest instead of the .WebClient.

Here's an example: https://stackoverflow.com/questions/1867385/upload-files-with-ftp-using-powershell

So for your script try:
# create the FtpWebRequest and configure it
$ftpserver = "ftp://ftp.DOMAIN.com/list_import/"
$file = "MyFile.csv" 
$user = "3213213" 
$pass = "YthsTYkW"  
$URL = $ftpserver + $file
$FilePath = "C:\Tools\FTP\$file" 

$ftp = [System.Net.FtpWebRequest]::Create($URL)
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$ftp.Credentials = new-object System.Net.NetworkCredential($user,$pass)
$ftp.UseBinary = $true
$ftp.UsePassive = $true
# read in the file to upload as a byte array
$content = [System.IO.File]::ReadAllBytes($FilePath)
$ftp.ContentLength = $content.Length
# get the request stream, and write the bytes into it
$rs = $ftp.GetRequestStream()
$rs.Write($content, 0, $content.Length)
# be sure to clean up after ourselves
$rs.Close()
$rs.Dispose()

Open in new window

Avatar of Kasper Katzmann

ASKER

Hi Jeremy
Thank you for answering.

Unfortunately I still get errors
Exception calling "GetRequestStream" with "0" argument(s): "The remote server returned an error: (530) Not logged in."
At line:19 char:1
+ $rs = $ftp.GetRequestStream()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException
 
You cannot call a method on a null-valued expression.
At line:20 char:1
+ $rs.Write($content, 0, $content.Length)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:22 char:1
+ $rs.Close()
+ ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
 
You cannot call a method on a null-valued expression.
At line:23 char:1
+ $rs.Dispose()
+ ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

Open in new window

Strange. It worked in my tests. Do you have the correct username and password in your script?
Yes, they are the same as when I do it manually through Explorer
Do you know if the ftp server is in passive or active mode?
Do you know what FTP server application is being run?

Since you're getting the "The remote server returned an error: (530) Not logged in" error still, it leads me to believe there's an issue with the credentials being passed. If there are any special characters in the password then make sure you use single quotes ( ' ) instead of double quotes ( " ) in your script. E.g.:
# Works:
$pass = 'mypa$$word'

#Doesn't work:
$pass = "mypa$$word"

Open in new window

Hmm, I actually don’t know if it’s passive or not. I will check with the owner.
And there are no special characters in the password. It is in the same format as the example.
#we specify the directory where all files that we want to upload  
$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)
 }
ASKER CERTIFIED SOLUTION
Avatar of Kasper Katzmann
Kasper Katzmann
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial