Kasper Katzmann
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:
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)
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
ASKER
Hi Jeremy
Thank you for answering.
Unfortunately I still get errors
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
Strange. It worked in my tests. Do you have the correct username and password in your script?
ASKER
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.:
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"
ASKER
Hmm, I actually don’t know if it’s passive or not. I will check with the owner.
ASKER
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.NetworkCredenti al($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)
}
$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.NetworkCredenti
#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
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's an example: https://stackoverflow.com/questions/1867385/upload-files-with-ftp-using-powershell
So for your script try:
Open in new window