Power shell scripting for push files recursively to ftp server

??? $ftp_client = New-Object System.Net.WebClient

Start here: https://netftp.codeplex.com/

August 31st, 2015 3:46am

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 remove 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.

Free Windows Admin Tool Kit Click here and download it now
August 31st, 2015 6:40am

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

Other recent topics Other recent topics