Power shell scripting for push files recursively to ftp server

Hi,

I want to copy all the files ,folders and subfolders to ftp server

I tried with below script

## Automate FTP uploads
## Go to destination
cd d:/test
$location = Get-Location
"We are here: $location"
## Get files
$files = $files=Get-ChildItem -recurse
## Get ftp object
$ftp_address = ftp://test.ftp.azurewebsites.windows.net/
$user = "username"
$pass = "Password"
$ftp_client = New-Object System.Net.WebClient

$webclient.Credentials = New-Object System.Net.NetworkCredential($user.Normalize(),$pass.Normalize()) 
## Make uploads
foreach($file in $files)
{
   
    $directory = "";
    $source = $file.DirectoryName + "\" + $file;

    if ($File.DirectoryName.Length -gt 0)
    {
        $directory = $file.DirectoryName.Replace($Location,"")
    }
    $directory += "/";
    $FtpCommand = $ftp_address + $directory
  
 
    $makeDirectory = [System.Net.FtpWebRequest]::Create($ftp_address+$directory);
    $makeDirectory.Credentials = New-Object System.Net.NetworkCredential($user,$pass);
    $makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory;
    $makeDirectory.GetResponse();

    $uri = New-Object System.Uri($FtpCommand+$file.Name)
    "Command is " + $uri+ " file is $source"
    $ftp_client.UploadFile($uri, $file.FullName)
}

but it throwing the below error

Exception calling "UploadFile" with "2" argument(s): "The remote server returned an error: (530) Not logged in."
At C:\ftp.ps1:39 char:5
+     $ftp_client.UploadFile($uri, $file.FullName)}
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

but it is creating a folder in my ftp server. if i remover the folder creation script code it is moving the files under main folder. it is not coping the subfolder and files.

can any one help me to resolve this issue.

August 31st, 2015 2:41am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics