0

I have a project here that I am trying to automate. I have some load tests i need to run on a machine and during that time I need to have a constant in and out of FTP traffic. I have been trying to write a Powershell script to do it but have been coming up short. I can get the file to download every time but the upload always fails. I also need to make sure my looping mechanism is correct.

I need this to download and upload a single file until I tell it to stop. Here is the script.

$FTPServer = '172.20.2.124'
$FTPUsername = 'ftp'
$FTPPassword = 'ftpTransfers'
$FTPSecurePassword = ConvertTo-SecureString -String $FTPPassword -asPlainText -Force
$FTPCredential = New-Object System.Management.Automation.PSCredential($FTPUsername,$FTPSecurePassword)
$Path = '/'
$LocalPath = 'C:\Downloads'

#Open session
Set-FTPConnection -Credentials $FTPCredential -Server $FTPServer -Session MySession -UseBinary -KeepAlive 
$Session = Get-FTPConnection -Session MySession 

#download file
Get-FTPItem -Path /File1.mxf -LocalPath $LocalPath -Overwrite -Session $Session | out-null

#Upload the file
Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session $Session | out-null

do {
response = Read-Host "repeat?"
}
while ($response -eq "Y")

This is the error I get whenever I try to do an upload with the script. I have tried to figure out how to close the stream but I have not had any luck.

Add-FTPItem : Exception calling "GetRequestStream" with "0" argument(s): "Object reference not set to an instance of an object." At C:\Powershell Scripts\FTPUploadandDownload.ps1:22 char:1 + Add-FTPItem -LocalPath $LocalPath/File1.mxf -Path $Path -Overwrite -Session ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-FTPItem

Any help would be appreciated, I am fairly new to this level of powershell.

  • Looks like the error is in your `Add-FTPItem` command. Do you have that code? – Jason Boyd Sep 26 '17 at 21:22
  • @JasonBoyd Looks like it's from the [PSFTP module](https://www.powershellgallery.com/packages/PSFTP/1.6.1.2/Content/Add-FTPItem.ps1) – BenH Sep 26 '17 at 21:25
  • Just to clarify, are you downloading the files from the FTP and then uploading them or are the files on the computer and then you upload them? – Persistent13 Sep 26 '17 at 22:09
  • One question per post, please. The upload issue and the looping question are two completely separate questions. + When you are using a 3rd party library, you have to tell us, not make us guess. – Martin Prikryl Sep 27 '17 at 06:05
  • For upload examples that do not need 3rd party library, see [Upload files with FTP using PowerShell](https://stackoverflow.com/q/1867385/850848). – Martin Prikryl Sep 27 '17 at 06:13
  • Sorry, yes I am using the PSFTP module. I do not have the code but I downloaded it from [here](https://gallery.technet.microsoft.com/scriptcenter/PowerShell-FTP-Client-db6fe0cb) I would be downloading the file first then uploading the same file and back around again and again. –  Sep 27 '17 at 16:05

0 Answers0